diff options
author | Alessio Caiazza <acaiazza@gitlab.com> | 2022-01-25 10:55:59 +0300 |
---|---|---|
committer | Alessio Caiazza <acaiazza@gitlab.com> | 2022-01-25 10:55:59 +0300 |
commit | 58a67cf0f0152a87b4944c92c701be602ac33c2e (patch) | |
tree | a0b64f77668d6bd1e25baeb175907aab5dbf9f11 /test/acceptance | |
parent | ba74fa849ecc80683438165bffbbbf415a143587 (diff) | |
parent | 7344f49b62b9e1af88b936dda02997ce00d0d8dd (diff) |
Merge branch 'cleanup/gitlab-server-stub' into 'master'
test: cleanup gitlab server stub and stubOpts
See merge request gitlab-org/gitlab-pages!652
Diffstat (limited to 'test/acceptance')
-rw-r--r-- | test/acceptance/artifacts_test.go | 2 | ||||
-rw-r--r-- | test/acceptance/helpers_test.go | 42 | ||||
-rw-r--r-- | test/acceptance/stub_test.go | 41 |
3 files changed, 15 insertions, 70 deletions
diff --git a/test/acceptance/artifacts_test.go b/test/acceptance/artifacts_test.go index dddfeb67..2545c77e 100644 --- a/test/acceptance/artifacts_test.go +++ b/test/acceptance/artifacts_test.go @@ -155,7 +155,7 @@ func TestArtifactProxyRequest(t *testing.T) { func TestPrivateArtifactProxyRequest(t *testing.T) { setupTransport(t) - testServer := makeGitLabPagesAccessStub(t) + testServer := NewGitlabUnstartedServerStub(t, &stubOpts{}) keyFile, certFile := CreateHTTPSFixtureFiles(t) cert, err := tls.LoadX509KeyPair(certFile, keyFile) diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go index d21df0a8..b9cba467 100644 --- a/test/acceptance/helpers_test.go +++ b/test/acceptance/helpers_test.go @@ -220,7 +220,8 @@ func RunPagesProcess(t *testing.T, opts ...processOption) *LogCaptureBuffer { processCfg.gitlabStubOpts.pagesRoot = wd } - source := NewGitlabDomainsSourceStub(t, processCfg.gitlabStubOpts) + source := NewGitlabUnstartedServerStub(t, processCfg.gitlabStubOpts) + source.Start() gitLabAPISecretKey := CreateGitLabAPISecretKeyFixtureFile(t) processCfg.extraArgs = append( @@ -443,17 +444,14 @@ func ClientWithConfig(tlsConfig *tls.Config) (*http.Client, func()) { } type stubOpts struct { - m sync.RWMutex - apiCalled bool - authHandler http.HandlerFunc - userHandler http.HandlerFunc - pagesHandler http.HandlerFunc - pagesStatusResponse int - pagesRoot string - delay time.Duration + m sync.RWMutex + apiCalled bool + pagesHandler http.HandlerFunc + pagesRoot string + delay time.Duration } -func NewGitlabDomainsSourceStub(t *testing.T, opts *stubOpts) *httptest.Server { +func NewGitlabUnstartedServerStub(t *testing.T, opts *stubOpts) *httptest.Server { t.Helper() require.NotNil(t, opts) @@ -467,28 +465,21 @@ func NewGitlabDomainsSourceStub(t *testing.T, opts *stubOpts) *httptest.Server { router.HandleFunc("/api/v4/internal/pages", pagesHandler) authHandler := defaultAuthHandler(t) - if opts.authHandler != nil { - authHandler = opts.authHandler - } - router.HandleFunc("/oauth/token", authHandler) userHandler := defaultUserHandler(t) - if opts.userHandler != nil { - userHandler = opts.userHandler - } - router.HandleFunc("/api/v4/user", userHandler) router.HandleFunc("/api/v4/projects/{project_id:[0-9]+}/pages_access", func(w http.ResponseWriter, r *http.Request) { - if handleAccessControlArtifactRequests(t, w, r) { - return - } - handleAccessControlRequests(t, w, r) }) - return httptest.NewServer(router) + router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ok := handleAccessControlArtifactRequests(t, w, r) + require.True(t, ok) + }) + + return httptest.NewUnstartedServer(router) } func (o *stubOpts) setAPICalled(v bool) { @@ -538,11 +529,6 @@ func defaultAPIHandler(t *testing.T, opts *stubOpts) http.HandlerFunc { opts.setAPICalled(true) - if opts.pagesStatusResponse != 0 { - w.WriteHeader(opts.pagesStatusResponse) - return - } - // check if predefined response exists if responseFn, ok := testdata.DomainResponses[domain]; ok { err := json.NewEncoder(w).Encode(responseFn(t, opts.pagesRoot)) diff --git a/test/acceptance/stub_test.go b/test/acceptance/stub_test.go index 7fc733a1..9e271f37 100644 --- a/test/acceptance/stub_test.go +++ b/test/acceptance/stub_test.go @@ -3,7 +3,6 @@ package acceptance_test import ( "fmt" "net/http" - "net/http/httptest" "os" "regexp" "testing" @@ -67,46 +66,6 @@ func withStubOptions(opts *stubOpts) processOption { } } -// makeGitLabPagesAccessStub provides a stub *httptest.Server to check pages_access API call. -// the result is based on the project id. -// -// Project IDs must be 4 digit long and the following rules applies: -// 1000-1999: Ok -// 2000-2999: Unauthorized -// 3000-3999: Invalid token -func makeGitLabPagesAccessStub(t *testing.T) *httptest.Server { - t.Helper() - - return httptest.NewUnstartedServer(apiHandler(t)) -} - -func apiHandler(t *testing.T) http.HandlerFunc { - t.Helper() - - return func(w http.ResponseWriter, r *http.Request) { - switch r.URL.Path { - // TODO: move OAuth and user endpoints to NewGitlabDomainsSourceStub - case "/oauth/token": - require.Equal(t, "POST", r.Method) - w.WriteHeader(http.StatusOK) - fmt.Fprint(w, "{\"access_token\":\"abc\"}") - case "/api/v4/user": - require.Equal(t, "Bearer abc", r.Header.Get("Authorization")) - w.WriteHeader(http.StatusOK) - case "/api/v4/internal/pages/status": - // Temporarily adding these handlers to this stub. - w.WriteHeader(http.StatusNoContent) - case "/api/v4/internal/pages": - defaultAPIHandler(t, &stubOpts{})(w, r) - default: - if handleAccessControlArtifactRequests(t, w, r) { - return - } - handleAccessControlRequests(t, w, r) - } - } -} - func CreateHTTPSFixtureFiles(t *testing.T) (key string, cert string) { t.Helper() |