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 acme
import (
"net/http"
"testing"
"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"}
)
func TestServeAcmeChallengesNotConfigured(t *testing.T) {
testhelpers.AssertHTTP404(t, serveAcmeOrNotFound(nil, domain), "GET", challengeURL, nil, nil)
}
func TestServeAcmeChallengeWhenPresent(t *testing.T) {
testhelpers.AssertHTTP404(t, serveAcmeOrNotFound(middleware, domainWithChallenge), "GET", challengeURL, nil, nil)
}
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",
)
testhelpers.AssertHTTP404(t, serveAcmeOrNotFound(middleware, domain), "GET", indexURL, nil, nil)
}
|