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

https_test.go « handlers « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df78ecbdd221af46ef3747224d6b80f6240c6784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)
		})
	}
}