Skip to content

Commit 598b08f

Browse files
authored
test: mark Assert* functions as test helpers (#7380)
1 parent 374b7a6 commit 598b08f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

caddytest/caddytest.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ func CreateTestingTransport() *http.Transport {
362362

363363
// AssertLoadError will load a config and expect an error
364364
func AssertLoadError(t *testing.T, rawConfig string, configType string, expectedError string) {
365+
t.Helper()
366+
365367
tc := NewTester(t)
366368

367369
err := tc.initServer(rawConfig, configType)
@@ -372,6 +374,8 @@ func AssertLoadError(t *testing.T, rawConfig string, configType string, expected
372374

373375
// AssertRedirect makes a request and asserts the redirection happens
374376
func (tc *Tester) AssertRedirect(requestURI string, expectedToLocation string, expectedStatusCode int) *http.Response {
377+
tc.t.Helper()
378+
375379
redirectPolicyFunc := func(req *http.Request, via []*http.Request) error {
376380
return http.ErrUseLastResponse
377381
}
@@ -409,6 +413,8 @@ func (tc *Tester) AssertRedirect(requestURI string, expectedToLocation string, e
409413

410414
// CompareAdapt adapts a config and then compares it against an expected result
411415
func CompareAdapt(t testing.TB, filename, rawConfig string, adapterName string, expectedResponse string) bool {
416+
t.Helper()
417+
412418
cfgAdapter := caddyconfig.GetAdapter(adapterName)
413419
if cfgAdapter == nil {
414420
t.Logf("unrecognized config adapter '%s'", adapterName)
@@ -468,6 +474,8 @@ func CompareAdapt(t testing.TB, filename, rawConfig string, adapterName string,
468474

469475
// AssertAdapt adapts a config and then tests it against an expected result
470476
func AssertAdapt(t testing.TB, rawConfig string, adapterName string, expectedResponse string) {
477+
t.Helper()
478+
471479
ok := CompareAdapt(t, "Caddyfile", rawConfig, adapterName, expectedResponse)
472480
if !ok {
473481
t.Fail()
@@ -496,6 +504,8 @@ func applyHeaders(t testing.TB, req *http.Request, requestHeaders []string) {
496504

497505
// AssertResponseCode will execute the request and verify the status code, returns a response for additional assertions
498506
func (tc *Tester) AssertResponseCode(req *http.Request, expectedStatusCode int) *http.Response {
507+
tc.t.Helper()
508+
499509
resp, err := tc.Client.Do(req)
500510
if err != nil {
501511
tc.t.Fatalf("failed to call server %s", err)
@@ -510,6 +520,8 @@ func (tc *Tester) AssertResponseCode(req *http.Request, expectedStatusCode int)
510520

511521
// AssertResponse request a URI and assert the status code and the body contains a string
512522
func (tc *Tester) AssertResponse(req *http.Request, expectedStatusCode int, expectedBody string) (*http.Response, string) {
523+
tc.t.Helper()
524+
513525
resp := tc.AssertResponseCode(req, expectedStatusCode)
514526

515527
defer resp.Body.Close()
@@ -531,6 +543,8 @@ func (tc *Tester) AssertResponse(req *http.Request, expectedStatusCode int, expe
531543

532544
// AssertGetResponse GET a URI and expect a statusCode and body text
533545
func (tc *Tester) AssertGetResponse(requestURI string, expectedStatusCode int, expectedBody string) (*http.Response, string) {
546+
tc.t.Helper()
547+
534548
req, err := http.NewRequest("GET", requestURI, nil)
535549
if err != nil {
536550
tc.t.Fatalf("unable to create request %s", err)
@@ -541,6 +555,8 @@ func (tc *Tester) AssertGetResponse(requestURI string, expectedStatusCode int, e
541555

542556
// AssertDeleteResponse request a URI and expect a statusCode and body text
543557
func (tc *Tester) AssertDeleteResponse(requestURI string, expectedStatusCode int, expectedBody string) (*http.Response, string) {
558+
tc.t.Helper()
559+
544560
req, err := http.NewRequest("DELETE", requestURI, nil)
545561
if err != nil {
546562
tc.t.Fatalf("unable to create request %s", err)
@@ -551,6 +567,8 @@ func (tc *Tester) AssertDeleteResponse(requestURI string, expectedStatusCode int
551567

552568
// AssertPostResponseBody POST to a URI and assert the response code and body
553569
func (tc *Tester) AssertPostResponseBody(requestURI string, requestHeaders []string, requestBody *bytes.Buffer, expectedStatusCode int, expectedBody string) (*http.Response, string) {
570+
tc.t.Helper()
571+
554572
req, err := http.NewRequest("POST", requestURI, requestBody)
555573
if err != nil {
556574
tc.t.Errorf("failed to create request %s", err)
@@ -564,6 +582,8 @@ func (tc *Tester) AssertPostResponseBody(requestURI string, requestHeaders []str
564582

565583
// AssertPutResponseBody PUT to a URI and assert the response code and body
566584
func (tc *Tester) AssertPutResponseBody(requestURI string, requestHeaders []string, requestBody *bytes.Buffer, expectedStatusCode int, expectedBody string) (*http.Response, string) {
585+
tc.t.Helper()
586+
567587
req, err := http.NewRequest("PUT", requestURI, requestBody)
568588
if err != nil {
569589
tc.t.Errorf("failed to create request %s", err)
@@ -577,6 +597,8 @@ func (tc *Tester) AssertPutResponseBody(requestURI string, requestHeaders []stri
577597

578598
// AssertPatchResponseBody PATCH to a URI and assert the response code and body
579599
func (tc *Tester) AssertPatchResponseBody(requestURI string, requestHeaders []string, requestBody *bytes.Buffer, expectedStatusCode int, expectedBody string) (*http.Response, string) {
600+
tc.t.Helper()
601+
580602
req, err := http.NewRequest("PATCH", requestURI, requestBody)
581603
if err != nil {
582604
tc.t.Errorf("failed to create request %s", err)

0 commit comments

Comments
 (0)