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
65
|
package acme
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)
type domainStub struct {
hasAcmeChallenge bool
}
func (d *domainStub) ServeFileHTTP(w http.ResponseWriter, r *http.Request) bool {
if r.URL.Path == "/.well-known/acme-challenge/token" {
return d.hasAcmeChallenge
}
return false
}
func serveAcmeOrNotFound(m *Middleware, domain Domain) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !m.ServeAcmeChallenges(w, r, domain) {
http.NotFound(w, r)
}
}
}
const (
baseURL = "http://example.com"
indexURL = baseURL + "/index.html"
challengeURL = baseURL + "/.well-known/acme-challenge/token"
)
var (
domainWithChallenge = &domainStub{hasAcmeChallenge: true}
domain = &domainStub{hasAcmeChallenge: false}
middleware = &Middleware{GitlabURL: "https://gitlab.example.com"}
middlewareMalformed = &Middleware{GitlabURL: ":foo"}
)
func TestServeAcmeChallengesNotConfigured(t *testing.T) {
require.HTTPStatusCode(t, serveAcmeOrNotFound(nil, domain), http.MethodGet, challengeURL, nil, http.StatusNotFound)
}
func TestServeAcmeChallengeMalformed(t *testing.T) {
require.HTTPStatusCode(t, serveAcmeOrNotFound(middlewareMalformed, domain), http.MethodGet, challengeURL, nil, http.StatusNotFound)
}
func TestServeAcmeChallengeWhenPresent(t *testing.T) {
require.HTTPStatusCode(t, serveAcmeOrNotFound(middleware, domainWithChallenge), http.MethodGet, challengeURL, nil, http.StatusNotFound)
}
func TestServeAcmeChallengeWhenMissing(t *testing.T) {
testhelpers.AssertRedirectTo(
t, serveAcmeOrNotFound(middleware, domain),
"GET", challengeURL, nil,
"https://gitlab.example.com/-/acme-challenge?domain=example.com&token=token",
)
require.HTTPStatusCode(t, serveAcmeOrNotFound(middleware, domain), http.MethodGet, indexURL, nil, http.StatusNotFound)
}
|