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>2022-04-25 19:56:34 +0300
committerfeistel <6742251-feistel@users.noreply.gitlab.com>2022-04-25 19:56:34 +0300
commitf9754e61e392203d9dbb61cc2276373b90322e85 (patch)
treee4d56242aaee55666a0c1fe6187b490c0e1f0147 /internal
parent950512e00946a5d7df2d82cd6e2a4fe131c90659 (diff)
Add early return and tests for internal/handlers/https
Diffstat (limited to 'internal')
-rw-r--r--internal/handlers/https.go6
-rw-r--r--internal/handlers/https_test.go58
2 files changed, 63 insertions, 1 deletions
diff --git a/internal/handlers/https.go b/internal/handlers/https.go
index 84c7e159..5598a8fc 100644
--- a/internal/handlers/https.go
+++ b/internal/handlers/https.go
@@ -7,8 +7,12 @@ import (
)
func HTTPSRedirectMiddleware(handler http.Handler, redirect bool) http.Handler {
+ if !redirect {
+ return handler
+ }
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if redirect && !request.IsHTTPS(r) {
+ if !request.IsHTTPS(r) {
redirectToHTTPS(w, r, http.StatusTemporaryRedirect)
return
}
diff --git a/internal/handlers/https_test.go b/internal/handlers/https_test.go
new file mode 100644
index 00000000..df78ecbd
--- /dev/null
+++ b/internal/handlers/https_test.go
@@ -0,0 +1,58 @@
+package handlers_test
+
+import (
+ "net/http"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "gitlab.com/gitlab-org/gitlab-pages/internal/handlers"
+)
+
+func TestAcmeMiddleware(t *testing.T) {
+ h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusAccepted)
+ })
+
+ httpsURL := "https://example.com"
+ httpURL := "http://example.com"
+
+ testCases := []struct {
+ name string
+ redirect bool
+ path string
+ expectedStatus int
+ }{
+ {
+ name: "http redirects to https with redirect enabled",
+ redirect: true,
+ path: httpURL,
+ expectedStatus: http.StatusTemporaryRedirect,
+ },
+ {
+ name: "https handled successfully with redirect enabled",
+ redirect: true,
+ path: httpsURL,
+ expectedStatus: http.StatusAccepted,
+ },
+ {
+ name: "http does not redirect to https with redirect disabled",
+ redirect: false,
+ path: httpURL,
+ expectedStatus: http.StatusAccepted,
+ },
+ {
+ name: "https handled successfully with redirect disabled",
+ redirect: false,
+ path: httpsURL,
+ expectedStatus: http.StatusAccepted,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ m := handlers.HTTPSRedirectMiddleware(h, tc.redirect)
+ require.HTTPStatusCode(t, m.ServeHTTP, http.MethodGet, tc.path, nil, tc.expectedStatus)
+ })
+ }
+}