diff options
-rw-r--r-- | acceptance_test.go | 24 | ||||
-rw-r--r-- | helpers_test.go | 19 | ||||
-rw-r--r-- | internal/serving/disk/reader.go | 2 | ||||
-rw-r--r-- | internal/source/domains.go | 1 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab.go | 14 | ||||
-rw-r--r-- | shared/lookups/new-source.test.io.json | 1 | ||||
-rw-r--r-- | shared/pages/group/new-source.test.io/public/index.html | 1 |
7 files changed, 51 insertions, 11 deletions
diff --git a/acceptance_test.go b/acceptance_test.go index fe598b0c..3ed69254 100644 --- a/acceptance_test.go +++ b/acceptance_test.go @@ -16,6 +16,7 @@ import ( "time" "github.com/namsral/flag" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -1531,3 +1532,26 @@ func TestApiSecretKeyFlagIsSupported(t *testing.T) { teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-api-secret-key", "/path/to/secret.key") defer teardown() } + +func TestGitlabDomainsSource(t *testing.T) { + skipUnlessEnabled(t) + + source := NewGitlabDomainsSourceStub(t) + defer source.Close() + + sourceArgs := []string{ + "-gitlab-server", source.URL, + // "-api-secret-key", "/path/to/secret.key", + } + teardown := RunPagesProcess(t, *pagesBinary, listeners, "", sourceArgs...) + defer teardown() + + response, err := GetPageFromListener(t, httpListener, "new-source.test.io", "/my/pages/project/") + require.NoError(t, err) + + defer response.Body.Close() + body, _ := ioutil.ReadAll(response.Body) + + assert.Equal(t, http.StatusOK, response.StatusCode) + assert.Equal(t, "New Pages GitLab Source PoC OK\n", string(body)) +} diff --git a/helpers_test.go b/helpers_test.go index ad7c65f1..a61aaa27 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -5,9 +5,11 @@ import ( "crypto/tls" "crypto/x509" "fmt" + "io" "io/ioutil" "net" "net/http" + "net/http/httptest" "os" "os/exec" "strings" @@ -375,3 +377,20 @@ func waitForRoundtrips(t *testing.T, listeners []ListenSpec, timeout time.Durati require.Equal(t, len(listeners), nListening, "all listeners must be accepting TCP connections") } + +func NewGitlabDomainsSourceStub(t *testing.T) *httptest.Server { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + domain := r.URL.Query().Get("host") + + fixture, err := os.Open("shared/lookups/" + domain + ".json") + defer fixture.Close() + require.NoError(t, err) + + _, err = io.Copy(w, fixture) + require.NoError(t, err) + + t.Logf("GitLab domain %s source stub served lookup", domain) + }) + + return httptest.NewServer(handler) +} diff --git a/internal/serving/disk/reader.go b/internal/serving/disk/reader.go index b52b5cff..fb2201a1 100644 --- a/internal/serving/disk/reader.go +++ b/internal/serving/disk/reader.go @@ -27,7 +27,7 @@ func (reader *Reader) tryFile(h serving.Handler) error { if locationError, _ := err.(*locationDirectoryError); locationError != nil { if endsWithSlash(urlPath) { fullPath, err = reader.resolvePath(h.LookupPath.Path, h.SubPath, "index.html") - } else { + } else { // TODO why are we doing that? In tests it redirects to HTTPS. // Concat Host with URL.Path redirectPath := "//" + host + "/" redirectPath += strings.TrimPrefix(urlPath, "/") diff --git a/internal/source/domains.go b/internal/source/domains.go index 1db285a4..a69387e0 100644 --- a/internal/source/domains.go +++ b/internal/source/domains.go @@ -12,6 +12,7 @@ var newSourceDomains = []string{ "pages-project-poc.gitlab.io", "pages-namespace-poc.gitlab.io", "pages-custom-poc.grzegorz.co", + "new-source.test.io", // used in acceptance tests } var brokenSourceDomain = "pages-broken-poc.gitlab.io" diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index 18da0c0b..eeabd028 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -2,7 +2,6 @@ package gitlab import ( "errors" - "net" "net/http" "strings" @@ -45,12 +44,7 @@ func (g *Gitlab) GetDomain(name string) (*domain.Domain, error) { // Resolve is supposed to get the serving lookup path based on the request from // the GitLab source func (g *Gitlab) Resolve(r *http.Request) (*serving.LookupPath, string, error) { - domain, _, err := net.SplitHostPort(r.Host) - if err != nil { - return nil, "", err - } - - response, err := g.client.GetVirtualDomain(domain) + response, err := g.client.GetVirtualDomain(r.Host) if err != nil { return nil, "", err } @@ -59,14 +53,14 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.LookupPath, string, error) { if strings.Contains(r.URL.Path, lookup.Prefix) { lookupPath := &serving.LookupPath{ Location: lookup.Prefix, - Path: lookup.Source.Path, - IsNamespaceProject: false, // TODO is this still relevant? it is not served in the API + Path: strings.TrimPrefix(lookup.Source.Path, "/"), // TODO test + IsNamespaceProject: false, // TODO is this still relevant? it is not served in the API IsHTTPSOnly: lookup.HTTPSOnly, HasAccessControl: lookup.AccessControl, ProjectID: uint64(lookup.ProjectID), } - requestPath := strings.Replace(r.URL.Path, lookup.Prefix, "", 1) + requestPath := strings.TrimPrefix(r.URL.Path, lookup.Prefix) return lookupPath, requestPath, nil } diff --git a/shared/lookups/new-source.test.io.json b/shared/lookups/new-source.test.io.json new file mode 100644 index 00000000..5dfbd9ba --- /dev/null +++ b/shared/lookups/new-source.test.io.json @@ -0,0 +1 @@ +{"certificate":"","key":"","lookup_paths":[{"project_id":123,"access_control":false,"https_only":false,"prefix":"/my/pages/project","source":{"type":"file","path":"/group/new-source.test.io/public"}}]} diff --git a/shared/pages/group/new-source.test.io/public/index.html b/shared/pages/group/new-source.test.io/public/index.html new file mode 100644 index 00000000..e6e1e58e --- /dev/null +++ b/shared/pages/group/new-source.test.io/public/index.html @@ -0,0 +1 @@ +New Pages GitLab Source PoC OK |