diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2022-02-21 01:22:47 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2022-02-21 01:22:47 +0300 |
commit | 7c0e94be67c3103b4ef4e336f688c9674cb92740 (patch) | |
tree | 29aa918fd7a1d85dd5343fd7b84c743d74310e6b | |
parent | 800075aeb93f74d9b299b950a93839ccd4a5aab2 (diff) | |
parent | ae086e4ca56064b511e306f0663e0badb3e3433a (diff) |
Merge branch 'use-test-cleanup' into 'master'
Avoid return fucn() instead use t.Cleanup(fucn() {...}) part 2
See merge request gitlab-org/gitlab-pages!701
-rw-r--r-- | internal/auth/auth_test.go | 11 | ||||
-rw-r--r-- | internal/httpfs/http_fs_test.go | 3 | ||||
-rw-r--r-- | internal/httptransport/metered_round_tripper_test.go | 4 | ||||
-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/urilimiter/urilimiter_test.go | 4 |
6 files changed, 16 insertions, 10 deletions
diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index 47e424c1..61b4e88a 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -17,6 +17,7 @@ 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 +69,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 +233,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 +319,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,11 +494,11 @@ 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() - defer res.Body.Close() + testhelpers.Close(t, res.Body) require.Equal(t, http.StatusFound, res.StatusCode) require.Equal(t, "http://pages.gitlab-example.com/test", result.Header().Get("Location")) } 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..2d05a1c3 100644 --- a/internal/httptransport/metered_round_tripper_test.go +++ b/internal/httptransport/metered_round_tripper_test.go @@ -10,6 +10,8 @@ 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 +25,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/urilimiter/urilimiter_test.go b/internal/urilimiter/urilimiter_test.go index 0ac89ea0..23b84102 100644 --- a/internal/urilimiter/urilimiter_test.go +++ b/internal/urilimiter/urilimiter_test.go @@ -8,6 +8,8 @@ import ( "testing" "github.com/stretchr/testify/require" + + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) func TestNewMiddleware(t *testing.T) { @@ -56,7 +58,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 { |