@marcel91200 / caddy Mirror · read-only / commits / 598b08f9
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 17:15 UTC

test: mark `Assert*` functions as test helpers (#7380)

Kévin Dunglas committed Dec 8, 2025 at 23:32 UTC 598b08f9ae6c6ab9d19142bc3992d07fa3155628
1 file changed +22
caddytest/caddytest.go
+22
@@ -362,6 +362,8 @@ func CreateTestingTransport() *http.Transport {
362
363 // AssertLoadError will load a config and expect an error
364 func AssertLoadError(t *testing.T, rawConfig string, configType string, expectedError string) {
365 + t.Helper()
366 +
367 tc := NewTester(t)
368
369 err := tc.initServer(rawConfig, configType)
@@ -372,6 +374,8 @@ func AssertLoadError(t *testing.T, rawConfig string, configType string, expected
374
375 // AssertRedirect makes a request and asserts the redirection happens
376 func (tc *Tester) AssertRedirect(requestURI string, expectedToLocation string, expectedStatusCode int) *http.Response {
377 + tc.t.Helper()
378 +
379 redirectPolicyFunc := func(req *http.Request, via []*http.Request) error {
380 return http.ErrUseLastResponse
381 }
@@ -409,6 +413,8 @@ func (tc *Tester) AssertRedirect(requestURI string, expectedToLocation string, e
413
414 // CompareAdapt adapts a config and then compares it against an expected result
415 func CompareAdapt(t testing.TB, filename, rawConfig string, adapterName string, expectedResponse string) bool {
416 + t.Helper()
417 +
418 cfgAdapter := caddyconfig.GetAdapter(adapterName)
419 if cfgAdapter == nil {
420 t.Logf("unrecognized config adapter '%s'", adapterName)
@@ -468,6 +474,8 @@ func CompareAdapt(t testing.TB, filename, rawConfig string, adapterName string,
474
475 // AssertAdapt adapts a config and then tests it against an expected result
476 func AssertAdapt(t testing.TB, rawConfig string, adapterName string, expectedResponse string) {
477 + t.Helper()
478 +
479 ok := CompareAdapt(t, "Caddyfile", rawConfig, adapterName, expectedResponse)
480 if !ok {
481 t.Fail()
@@ -496,6 +504,8 @@ func applyHeaders(t testing.TB, req *http.Request, requestHeaders []string) {
504
505 // AssertResponseCode will execute the request and verify the status code, returns a response for additional assertions
506 func (tc *Tester) AssertResponseCode(req *http.Request, expectedStatusCode int) *http.Response {
507 + tc.t.Helper()
508 +
509 resp, err := tc.Client.Do(req)
510 if err != nil {
511 tc.t.Fatalf("failed to call server %s", err)
@@ -510,6 +520,8 @@ func (tc *Tester) AssertResponseCode(req *http.Request, expectedStatusCode int)
520
521 // AssertResponse request a URI and assert the status code and the body contains a string
522 func (tc *Tester) AssertResponse(req *http.Request, expectedStatusCode int, expectedBody string) (*http.Response, string) {
523 + tc.t.Helper()
524 +
525 resp := tc.AssertResponseCode(req, expectedStatusCode)
526
527 defer resp.Body.Close()
@@ -531,6 +543,8 @@ func (tc *Tester) AssertResponse(req *http.Request, expectedStatusCode int, expe
543
544 // AssertGetResponse GET a URI and expect a statusCode and body text
545 func (tc *Tester) AssertGetResponse(requestURI string, expectedStatusCode int, expectedBody string) (*http.Response, string) {
546 + tc.t.Helper()
547 +
548 req, err := http.NewRequest("GET", requestURI, nil)
549 if err != nil {
550 tc.t.Fatalf("unable to create request %s", err)
@@ -541,6 +555,8 @@ func (tc *Tester) AssertGetResponse(requestURI string, expectedStatusCode int, e
555
556 // AssertDeleteResponse request a URI and expect a statusCode and body text
557 func (tc *Tester) AssertDeleteResponse(requestURI string, expectedStatusCode int, expectedBody string) (*http.Response, string) {
558 + tc.t.Helper()
559 +
560 req, err := http.NewRequest("DELETE", requestURI, nil)
561 if err != nil {
562 tc.t.Fatalf("unable to create request %s", err)
@@ -551,6 +567,8 @@ func (tc *Tester) AssertDeleteResponse(requestURI string, expectedStatusCode int
567
568 // AssertPostResponseBody POST to a URI and assert the response code and body
569 func (tc *Tester) AssertPostResponseBody(requestURI string, requestHeaders []string, requestBody *bytes.Buffer, expectedStatusCode int, expectedBody string) (*http.Response, string) {
570 + tc.t.Helper()
571 +
572 req, err := http.NewRequest("POST", requestURI, requestBody)
573 if err != nil {
574 tc.t.Errorf("failed to create request %s", err)
@@ -564,6 +582,8 @@ func (tc *Tester) AssertPostResponseBody(requestURI string, requestHeaders []str
582
583 // AssertPutResponseBody PUT to a URI and assert the response code and body
584 func (tc *Tester) AssertPutResponseBody(requestURI string, requestHeaders []string, requestBody *bytes.Buffer, expectedStatusCode int, expectedBody string) (*http.Response, string) {
585 + tc.t.Helper()
586 +
587 req, err := http.NewRequest("PUT", requestURI, requestBody)
588 if err != nil {
589 tc.t.Errorf("failed to create request %s", err)
@@ -577,6 +597,8 @@ func (tc *Tester) AssertPutResponseBody(requestURI string, requestHeaders []stri
597
598 // AssertPatchResponseBody PATCH to a URI and assert the response code and body
599 func (tc *Tester) AssertPatchResponseBody(requestURI string, requestHeaders []string, requestBody *bytes.Buffer, expectedStatusCode int, expectedBody string) (*http.Response, string) {
600 + tc.t.Helper()
601 +
602 req, err := http.NewRequest("PATCH", requestURI, requestBody)
603 if err != nil {
604 tc.t.Errorf("failed to create request %s", err)