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

acme_test.go « acme « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 148e953f686bedf1e96406580fa842975b1460e1 (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
59
60
61
62
63
64
package acme_test

import (
	"net/http"
	"net/url"
	"testing"

	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/acme"
)

const (
	baseURL      = "http://example.com"
	indexURL     = baseURL + "/index.html"
	challengeURL = baseURL + "/.well-known/acme-challenge/token"
)

func TestAcmeMiddleware(t *testing.T) {
	u, err := url.Parse("https://gitlab.example.com")
	require.NoError(t, err)

	testCases := []struct {
		name           string
		f              acme.FallbackStrategy
		path           string
		expectedStatus int
	}{
		{
			name:           "not an acme request",
			path:           indexURL,
			expectedStatus: http.StatusAccepted,
		},
		{
			name: "acme challenge redirect to gitlab if missing",
			f: func(w http.ResponseWriter, r *http.Request) bool {
				return false
			},
			path:           challengeURL,
			expectedStatus: http.StatusTemporaryRedirect,
		},
		{
			name: "acme challenge served from disk if present",
			f: func(w http.ResponseWriter, r *http.Request) bool {
				w.WriteHeader(http.StatusOK)
				return true
			},
			path:           challengeURL,
			expectedStatus: http.StatusOK,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				if !acme.ServeAcmeChallenges(w, r, tc.f, u) {
					w.WriteHeader(http.StatusAccepted)
				}
			})

			require.HTTPStatusCode(t, h.ServeHTTP, http.MethodGet, tc.path, nil, tc.expectedStatus)
		})
	}
}