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:
Diffstat (limited to 'internal/acme/acme_test.go')
-rw-r--r--internal/acme/acme_test.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/acme/acme_test.go b/internal/acme/acme_test.go
new file mode 100644
index 00000000..c0daefeb
--- /dev/null
+++ b/internal/acme/acme_test.go
@@ -0,0 +1,54 @@
+package acme
+
+import (
+ "net/http"
+ "testing"
+
+ "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
+)
+
+type domainStub struct {
+ hasAcmeChallenge bool
+}
+
+func (d *domainStub) HasAcmeChallenge(_ string) bool {
+ return d.hasAcmeChallenge
+}
+
+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)
+}