From 935999c40715026b5c38555ac0050d6b3dd62518 Mon Sep 17 00:00:00 2001 From: WendelHime <6754291+WendelHime@users.noreply.github.com> Date: Mon, 28 Oct 2024 10:54:16 -0300 Subject: [PATCH] fix: adding test names to TestCustomValidators --- fronted_test.go | 57 +++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/fronted_test.go b/fronted_test.go index 910a5ee..d8d9986 100644 --- a/fronted_test.go +++ b/fronted_test.go @@ -562,32 +562,38 @@ func TestCustomValidators(t *testing.T) { masqueradesExhausted := fmt.Sprintf(`Get "%v": could not complete request even with retries`, testURL) tests := []struct { + name string responseCode int validator ResponseValidator expectedError string }{ // with the default validator, only 403s are rejected { + name: "it should return masquerades exhausted error when providing nil validator and returning 403", responseCode: http.StatusForbidden, validator: nil, expectedError: masqueradesExhausted, }, { + name: "it should return no errors when providing nil validator and receiving a 202", responseCode: http.StatusAccepted, validator: nil, expectedError: "", }, { + name: "it should return no errors when providing nil validator and receiving 402", responseCode: http.StatusPaymentRequired, validator: nil, expectedError: "", }, { + name: "it should return no errors when providing nil validator and receiving 418", responseCode: http.StatusTeapot, validator: nil, expectedError: "", }, { + name: "it should return no errors when providing nil validator and receiving 502", responseCode: http.StatusBadGateway, validator: nil, expectedError: "", @@ -595,26 +601,31 @@ func TestCustomValidators(t *testing.T) { // with the custom validator, 403 is allowed, listed codes are rejected { + name: "it should return no errors when providing validator that accepts 403", responseCode: http.StatusForbidden, validator: sadCloudValidator, expectedError: "", }, { + name: "it should return no errors when providing validator that accepts 202", responseCode: http.StatusAccepted, validator: sadCloudValidator, expectedError: "", }, { + name: "it should return masquerades exhausted when validator receives a 402", responseCode: http.StatusPaymentRequired, validator: sadCloudValidator, expectedError: masqueradesExhausted, }, { + name: "it should return masquerades exhausted when validator receives a 418", responseCode: http.StatusTeapot, validator: sadCloudValidator, expectedError: masqueradesExhausted, }, { + name: "it should return masquerades exhausted when validator receives a 502", responseCode: http.StatusBadGateway, validator: sadCloudValidator, expectedError: masqueradesExhausted, @@ -622,34 +633,30 @@ func TestCustomValidators(t *testing.T) { } for _, test := range tests { - setup(test.validator) - direct, ok := testContext.NewFronted(30 * time.Second) - if !assert.True(t, ok) { - return - } - client := &http.Client{ - Transport: direct, - } + t.Run(test.name, func(t *testing.T) { + setup(test.validator) + direct, ok := testContext.NewFronted(30 * time.Second) + require.True(t, ok) + client := &http.Client{ + Transport: direct, + } - req, err := http.NewRequest(http.MethodGet, testURL, nil) - if !assert.NoError(t, err) { - return - } - if test.responseCode != http.StatusAccepted { - val := strconv.Itoa(test.responseCode) - log.Debugf("requesting forced response code %s", val) - req.Header.Set(CDNForceFail, val) - } + req, err := http.NewRequest(http.MethodGet, testURL, nil) + require.NoError(t, err) + if test.responseCode != http.StatusAccepted { + val := strconv.Itoa(test.responseCode) + log.Debugf("requesting forced response code %s", val) + req.Header.Set(CDNForceFail, val) + } - res, err := client.Do(req) - if test.expectedError == "" { - if !assert.NoError(t, err) { - continue + res, err := client.Do(req) + if test.expectedError == "" { + require.NoError(t, err) + assert.Equal(t, test.responseCode, res.StatusCode, "Failed to force response status code") + } else { + assert.EqualError(t, err, test.expectedError) } - assert.Equal(t, test.responseCode, res.StatusCode, "Failed to force response status code") - } else { - assert.EqualError(t, err, test.expectedError) - } + }) } }