diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-04-20 22:21:56 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-06-05 22:53:52 +0300 |
commit | 185d9aed1b2645f7c110513685b7ff1b61c50c0f (patch) | |
tree | 7966c31b0721fa3ac0410a5fdcb23ec2e37b486f /internal/acme/acme_test.go | |
parent | fd62cfc0771c627bd1bda001fd1fa71178dd447b (diff) |
Refactor acme tests and middleware for lazy domain resolution
Diffstat (limited to 'internal/acme/acme_test.go')
-rw-r--r-- | internal/acme/acme_test.go | 91 |
1 files changed, 45 insertions, 46 deletions
diff --git a/internal/acme/acme_test.go b/internal/acme/acme_test.go index 225d1dc4..148e953f 100644 --- a/internal/acme/acme_test.go +++ b/internal/acme/acme_test.go @@ -1,65 +1,64 @@ -package acme +package acme_test import ( "net/http" + "net/url" "testing" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" + "gitlab.com/gitlab-org/gitlab-pages/internal/acme" ) -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 TestAcmeMiddleware(t *testing.T) { + u, err := url.Parse("https://gitlab.example.com") + require.NoError(t, err) -func TestServeAcmeChallengeWhenPresent(t *testing.T) { - require.HTTPStatusCode(t, serveAcmeOrNotFound(middleware, domainWithChallenge), http.MethodGet, challengeURL, nil, http.StatusOK) -} + 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, + }, + } -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", - ) + 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, serveAcmeOrNotFound(middleware, domain), http.MethodGet, indexURL, nil, http.StatusNotFound) + require.HTTPStatusCode(t, h.ServeHTTP, http.MethodGet, tc.path, nil, tc.expectedStatus) + }) + } } |