From 7344f49b62b9e1af88b936dda02997ce00d0d8dd Mon Sep 17 00:00:00 2001 From: feistel <6742251-feistel@users.noreply.gitlab.com> Date: Tue, 21 Dec 2021 22:53:39 +0100 Subject: test: cleanup gitlab server stub and stubOpts --- test/acceptance/artifacts_test.go | 2 +- test/acceptance/helpers_test.go | 42 +++++++++++++-------------------------- test/acceptance/stub_test.go | 41 -------------------------------------- 3 files changed, 15 insertions(+), 70 deletions(-) (limited to 'test') 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 769b0467..20f641fa 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( @@ -442,17 +443,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) @@ -466,28 +464,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) { @@ -537,11 +528,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() -- cgit v1.2.3