diff options
author | yigithankardas <root> | 2022-02-16 16:34:05 +0300 |
---|---|---|
committer | yigithankardas <root> | 2022-02-16 16:34:05 +0300 |
commit | d8f645df7f1fb711698ecc31f71b577b3f8dc617 (patch) | |
tree | ff4b8dcc1bf5933e91caf790b285f056ee93a7e4 /internal | |
parent | d4a251ae1bb492fee8af0d3d3619bdd3f5bfc1c0 (diff) |
reset: go back to initial commit.
Diffstat (limited to 'internal')
-rw-r--r-- | internal/auth/auth_test.go | 10 | ||||
-rw-r--r-- | internal/httpfs/http_fs_test.go | 3 | ||||
-rw-r--r-- | internal/httptransport/metered_round_tripper_test.go | 3 | ||||
-rw-r--r-- | internal/serving/disk/local/serving_test.go | 2 | ||||
-rw-r--r-- | internal/serving/disk/zip/serving_test.go | 2 | ||||
-rw-r--r-- | internal/testhelpers/testhelpers.go | 10 | ||||
-rw-r--r-- | internal/urilimiter/urilimiter_test.go | 3 |
7 files changed, 24 insertions, 9 deletions
diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index 47e424c1..94ffc23b 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -17,6 +17,8 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/request" "gitlab.com/gitlab-org/gitlab-pages/internal/source/mock" + + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) func createTestAuth(t *testing.T, internalServer string, publicServer string) *Auth { @@ -68,7 +70,7 @@ func setSessionValues(t *testing.T, r *http.Request, store sessions.Store, value session.Save(tmpRequest, result) res := result.Result() - defer res.Body.Close() + testhelpers.Close(t, res.Body) for _, cookie := range res.Cookies() { r.AddCookie(cookie) @@ -232,7 +234,7 @@ func testTryAuthenticateWithCodeAndState(t *testing.T, https bool) { require.True(t, auth.TryAuthenticate(result, r, mockSource)) res := result.Result() - defer res.Body.Close() + testhelpers.Close(t, res.Body) require.Equal(t, http.StatusFound, result.Code) require.Equal(t, "https://pages.gitlab-example.com/project/", result.Header().Get("Location")) @@ -318,7 +320,7 @@ func TestCheckAuthenticationWhenNoAccess(t *testing.T) { contentServed := auth.CheckAuthentication(w, r, &domainMock{projectID: 1000, notFoundContent: "Generic 404"}) require.True(t, contentServed) res := w.Result() - defer res.Body.Close() + testhelpers.Close(t, res.Body) require.Equal(t, http.StatusNotFound, res.StatusCode) @@ -493,7 +495,7 @@ func TestCheckResponseForInvalidTokenWhenInvalidToken(t *testing.T) { r := &http.Request{URL: reqURL, Host: "pages.gitlab-example.com", RequestURI: "/test"} resp := &http.Response{StatusCode: http.StatusUnauthorized, Body: io.NopCloser(bytes.NewReader([]byte("{\"error\":\"invalid_token\"}")))} - defer resp.Body.Close() + testhelpers.Close(t, resp.Body) require.True(t, auth.CheckResponseForInvalidToken(result, r, resp)) res := result.Result() diff --git a/internal/httpfs/http_fs_test.go b/internal/httpfs/http_fs_test.go index 991c6017..25003726 100644 --- a/internal/httpfs/http_fs_test.go +++ b/internal/httpfs/http_fs_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitlab-pages/internal/httptransport" + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) func TestFSOpen(t *testing.T) { @@ -161,7 +162,7 @@ func TestFileSystemPathCanServeHTTP(t *testing.T) { res, err := client.Do(req) require.NoError(t, err) - defer res.Body.Close() + testhelpers.Close(t, res.Body) require.Equal(t, test.expectedStatusCode, res.StatusCode) content, err := io.ReadAll(res.Body) diff --git a/internal/httptransport/metered_round_tripper_test.go b/internal/httptransport/metered_round_tripper_test.go index 2b126760..8a298e20 100644 --- a/internal/httptransport/metered_round_tripper_test.go +++ b/internal/httptransport/metered_round_tripper_test.go @@ -10,6 +10,7 @@ import ( "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) func TestReconfigureMeteredRoundTripper(t *testing.T) { @@ -23,7 +24,7 @@ func TestReconfigureMeteredRoundTripper(t *testing.T) { res, err := mrt.RoundTrip(r) require.NoError(t, err) - defer res.Body.Close() + testhelpers.Close(t, res.Body) require.Equal(t, http.StatusOK, res.StatusCode) body, err := io.ReadAll(res.Body) diff --git a/internal/serving/disk/local/serving_test.go b/internal/serving/disk/local/serving_test.go index 8352bfc9..dbb14138 100644 --- a/internal/serving/disk/local/serving_test.go +++ b/internal/serving/disk/local/serving_test.go @@ -79,7 +79,7 @@ func TestDisk_ServeFileHTTP(t *testing.T) { require.True(t, s.ServeFileHTTP(handler)) resp := w.Result() - defer resp.Body.Close() + testhelpers.Close(t, resp.Body) require.Equal(t, test.expectedStatus, resp.StatusCode) body, err := io.ReadAll(resp.Body) diff --git a/internal/serving/disk/zip/serving_test.go b/internal/serving/disk/zip/serving_test.go index 2b603b5d..c3b18f2b 100644 --- a/internal/serving/disk/zip/serving_test.go +++ b/internal/serving/disk/zip/serving_test.go @@ -209,7 +209,7 @@ func TestZip_ServeFileHTTP(t *testing.T) { require.True(t, s.ServeFileHTTP(handler)) resp := w.Result() - defer resp.Body.Close() + testhelpers.Close(t, resp.Body) require.Equal(t, test.expectedStatus, resp.StatusCode) body, err := io.ReadAll(resp.Body) diff --git a/internal/testhelpers/testhelpers.go b/internal/testhelpers/testhelpers.go index 11bf7e65..2f708743 100644 --- a/internal/testhelpers/testhelpers.go +++ b/internal/testhelpers/testhelpers.go @@ -95,3 +95,13 @@ func PerformRequest(t *testing.T, handler http.Handler, r *http.Request) (int, s return res.StatusCode, string(b) } + +// Close will call the close function on a closer as part +// of the t.Cleanup function. +func Close(t *testing.T, c io.Closer) { + t.Helper() + + t.Cleanup(func() { + require.NoError(t, c.Close()) + }) +} diff --git a/internal/urilimiter/urilimiter_test.go b/internal/urilimiter/urilimiter_test.go index 0ac89ea0..a314ec91 100644 --- a/internal/urilimiter/urilimiter_test.go +++ b/internal/urilimiter/urilimiter_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) func TestNewMiddleware(t *testing.T) { @@ -56,7 +57,7 @@ func TestNewMiddleware(t *testing.T) { middleware.ServeHTTP(ww, rr) res := ww.Result() - defer res.Body.Close() + testhelpers.Close(t, res.Body) require.Equal(t, tt.expectedStatus, res.StatusCode) if tt.expectedStatus == http.StatusOK { |