diff options
author | Stan Hu <stanhu@gmail.com> | 2021-09-17 05:40:34 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2021-09-19 20:52:28 +0300 |
commit | 9bef1869a5a3580afb616ca940ed7c23aea47f5f (patch) | |
tree | d2f79df444daebcb758647739835169eaefdd4c3 /internal/handlers | |
parent | 159460852e41a77b7bb5d201e127f593681c370c (diff) |
chore: clean up handler tests to use httptest.NewRequest
Diffstat (limited to 'internal/handlers')
-rw-r--r-- | internal/handlers/handlers_test.go | 20 |
1 files changed, 6 insertions, 14 deletions
diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go index 62ea3a33..33a0cdb2 100644 --- a/internal/handlers/handlers_test.go +++ b/internal/handlers/handlers_test.go @@ -58,9 +58,7 @@ func TestHandleArtifactRequestedReturnsTrue(t *testing.T) { handlers := New(mockAuth, mockArtifact) result := httptest.NewRecorder() - reqURL, err := url.Parse("/something") - require.NoError(t, err) - r := &http.Request{URL: reqURL} + r := httptest.NewRequest(http.MethodGet, "/something", nil) require.Equal(t, true, handlers.HandleArtifactRequest("host", result, r)) } @@ -120,8 +118,7 @@ func TestForbiddenWithTokenIsNotHandled(t *testing.T) { handlers := New(mockAuth, nil) w := httptest.NewRecorder() - reqURL, _ := url.Parse("/") - r := &http.Request{URL: reqURL} + r := httptest.NewRequest(http.MethodGet, "/", nil) response := &http.Response{StatusCode: tc.StatusCode} // nolint:bodyclose // TODO investigate https://gitlab.com/gitlab-org/gitlab-pages/-/issues/606 handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, tc.Token)(response) @@ -141,8 +138,7 @@ func TestNotFoundWithoutTokenIsNotHandledWhenNotAuthSupport(t *testing.T) { handlers := New(mockAuth, nil) w := httptest.NewRecorder() - reqURL, _ := url.Parse("/") - r := &http.Request{URL: reqURL} + r := httptest.NewRequest(http.MethodGet, "/", nil) response := &http.Response{StatusCode: http.StatusNotFound} // nolint:bodyclose // TODO investigate https://gitlab.com/gitlab-org/gitlab-pages/-/issues/606 handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, "")(response) @@ -160,8 +156,7 @@ func TestNotFoundWithoutTokenIsHandled(t *testing.T) { handlers := New(mockAuth, nil) w := httptest.NewRecorder() - reqURL, _ := url.Parse("/") - r := &http.Request{URL: reqURL} + r := httptest.NewRequest(http.MethodGet, "/", nil) response := &http.Response{StatusCode: http.StatusNotFound} // nolint:bodyclose // TODO investigate https://gitlab.com/gitlab-org/gitlab-pages/-/issues/606 handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, "")(response) @@ -179,8 +174,7 @@ func TestInvalidTokenResponseIsHandled(t *testing.T) { handlers := New(mockAuth, nil) w := httptest.NewRecorder() - reqURL, _ := url.Parse("/") - r := &http.Request{URL: reqURL} + r := httptest.NewRequest(http.MethodGet, "/", nil) response := &http.Response{StatusCode: http.StatusUnauthorized} // nolint:bodyclose // TODO investigate https://gitlab.com/gitlab-org/gitlab-pages/-/issues/606 handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, "token")(response) @@ -203,9 +197,7 @@ func TestHandleArtifactRequestButGetTokenFails(t *testing.T) { handlers := New(mockAuth, mockArtifact) result := httptest.NewRecorder() - reqURL, err := url.Parse("/something") - require.NoError(t, err) - r := &http.Request{URL: reqURL} + r := httptest.NewRequest(http.MethodGet, "/something", nil) require.Equal(t, true, handlers.HandleArtifactRequest("host", result, r)) } |