Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfeistel <6742251-feistel@users.noreply.gitlab.com>2021-12-08 16:27:44 +0300
committerfeistel <6742251-feistel@users.noreply.gitlab.com>2022-01-14 14:57:39 +0300
commita5c896d1aa4f672ec46778b9f98c7850a38bc94c (patch)
tree177f574f9cc782f450983e4b073911448144089a
parenta7e130776d934a12ab000bee930c5c353b772635 (diff)
test: replace http status assertions with testify method
-rw-r--r--internal/acme/acme_test.go8
-rw-r--r--internal/domain/domain_test.go18
-rw-r--r--internal/rejectmethods/middleware_test.go21
-rw-r--r--internal/testhelpers/testhelpers.go17
4 files changed, 11 insertions, 53 deletions
diff --git a/internal/acme/acme_test.go b/internal/acme/acme_test.go
index ab191694..0ee9e6fc 100644
--- a/internal/acme/acme_test.go
+++ b/internal/acme/acme_test.go
@@ -4,6 +4,8 @@ import (
"net/http"
"testing"
+ "github.com/stretchr/testify/require"
+
"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)
@@ -40,11 +42,11 @@ var (
)
func TestServeAcmeChallengesNotConfigured(t *testing.T) {
- testhelpers.AssertHTTP404(t, serveAcmeOrNotFound(nil, domain), "GET", challengeURL, nil, nil)
+ require.HTTPStatusCode(t, serveAcmeOrNotFound(nil, domain), http.MethodGet, challengeURL, nil, http.StatusNotFound)
}
func TestServeAcmeChallengeWhenPresent(t *testing.T) {
- testhelpers.AssertHTTP404(t, serveAcmeOrNotFound(middleware, domainWithChallenge), "GET", challengeURL, nil, nil)
+ require.HTTPStatusCode(t, serveAcmeOrNotFound(middleware, domainWithChallenge), http.MethodGet, challengeURL, nil, http.StatusNotFound)
}
func TestServeAcmeChallengeWhenMissing(t *testing.T) {
@@ -54,5 +56,5 @@ func TestServeAcmeChallengeWhenMissing(t *testing.T) {
"https://gitlab.example.com/-/acme-challenge?domain=example.com&token=token",
)
- testhelpers.AssertHTTP404(t, serveAcmeOrNotFound(middleware, domain), "GET", indexURL, nil, nil)
+ require.HTTPStatusCode(t, serveAcmeOrNotFound(middleware, domain), http.MethodGet, indexURL, nil, http.StatusNotFound)
}
diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go
index d76e10e8..57b8f185 100644
--- a/internal/domain/domain_test.go
+++ b/internal/domain/domain_test.go
@@ -2,9 +2,7 @@ package domain
import (
"fmt"
- "io"
"net/http"
- "net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
@@ -92,7 +90,8 @@ func TestPredefined404ServeHTTP(t *testing.T) {
testDomain := New("", "", "", &stubbedResolver{err: ErrDomainDoesNotExist})
- testhelpers.AssertHTTP404(t, serveFileOrNotFound(testDomain), "GET", "http://group.test.io/not-existing-file", nil, "The page you're looking for could not be found")
+ require.HTTPStatusCode(t, serveFileOrNotFound(testDomain), http.MethodGet, "http://group.test.io/not-existing-file", nil, http.StatusNotFound)
+ require.HTTPBodyContains(t, serveFileOrNotFound(testDomain), http.MethodGet, "http://group.test.io/not-existing-file", nil, "The page you're looking for could not be found")
}
func TestGroupCertificate(t *testing.T) {
@@ -204,18 +203,9 @@ func TestServeNamespaceNotFound(t *testing.T) {
Name: tt.domain,
Resolver: tt.resolver,
}
- w := httptest.NewRecorder()
- r := httptest.NewRequest("GET", fmt.Sprintf("http://%s%s", tt.domain, tt.path), nil)
- d.serveNamespaceNotFound(w, r)
- resp := w.Result()
- defer resp.Body.Close()
-
- require.Equal(t, http.StatusNotFound, resp.StatusCode)
- body, err := io.ReadAll(resp.Body)
- require.NoError(t, err)
-
- require.Contains(t, string(body), tt.expectedResponse)
+ require.HTTPStatusCode(t, d.serveNamespaceNotFound, http.MethodGet, fmt.Sprintf("http://%s%s", tt.domain, tt.path), nil, http.StatusNotFound)
+ require.HTTPBodyContains(t, d.serveNamespaceNotFound, http.MethodGet, fmt.Sprintf("http://%s%s", tt.domain, tt.path), nil, tt.expectedResponse)
})
}
}
diff --git a/internal/rejectmethods/middleware_test.go b/internal/rejectmethods/middleware_test.go
index b1c6a771..a62b0a14 100644
--- a/internal/rejectmethods/middleware_test.go
+++ b/internal/rejectmethods/middleware_test.go
@@ -3,7 +3,6 @@ package rejectmethods
import (
"io"
"net/http"
- "net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
@@ -19,27 +18,11 @@ func TestNewMiddleware(t *testing.T) {
acceptedMethods := []string{"GET", "HEAD", "POST", "PUT", "PATCH", "CONNECT", "OPTIONS", "TRACE"}
for _, method := range acceptedMethods {
t.Run(method, func(t *testing.T) {
- tmpRequest, _ := http.NewRequest(method, "/", nil)
- recorder := httptest.NewRecorder()
-
- middleware.ServeHTTP(recorder, tmpRequest)
-
- result := recorder.Result()
- defer result.Body.Close()
-
- require.Equal(t, http.StatusOK, result.StatusCode)
+ require.HTTPStatusCode(t, middleware.ServeHTTP, method, "/", nil, http.StatusOK)
})
}
t.Run("UNKNOWN", func(t *testing.T) {
- tmpRequest, _ := http.NewRequest("UNKNOWN", "/", nil)
- recorder := httptest.NewRecorder()
-
- middleware.ServeHTTP(recorder, tmpRequest)
-
- result := recorder.Result()
- defer result.Body.Close()
-
- require.Equal(t, http.StatusMethodNotAllowed, result.StatusCode)
+ require.HTTPStatusCode(t, middleware.ServeHTTP, "UNKNOWN", "/", nil, http.StatusMethodNotAllowed)
})
}
diff --git a/internal/testhelpers/testhelpers.go b/internal/testhelpers/testhelpers.go
index bb02b698..11bf7e65 100644
--- a/internal/testhelpers/testhelpers.go
+++ b/internal/testhelpers/testhelpers.go
@@ -3,7 +3,6 @@ package testhelpers
import (
"fmt"
"io"
- "mime"
"net/http"
"net/http/httptest"
"net/url"
@@ -15,22 +14,6 @@ import (
"github.com/stretchr/testify/require"
)
-// AssertHTTP404 asserts handler returns 404 with provided str body
-func AssertHTTP404(t *testing.T, handler http.HandlerFunc, mode, url string, values url.Values, str interface{}) {
- w := httptest.NewRecorder()
- req, err := http.NewRequest(mode, url+"?"+values.Encode(), nil)
- require.NoError(t, err)
- handler(w, req)
-
- require.Equal(t, http.StatusNotFound, w.Code, "HTTP status")
-
- if str != nil {
- contentType, _, _ := mime.ParseMediaType(w.Header().Get("Content-Type"))
- require.Equal(t, "text/html", contentType, "Content-Type")
- require.Contains(t, w.Body.String(), str)
- }
-}
-
// AssertRedirectTo asserts that handler redirects to particular URL
func AssertRedirectTo(t *testing.T, handler http.HandlerFunc, method string,
url string, values url.Values, expectedURL string) {