From 9c3dd42b1c731c02af116ca940b764549b675f8b Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Mon, 25 Feb 2019 14:42:35 +0100 Subject: [PATCH 1/2] Add custom 404 acceptance test --- acceptance_test.go | 63 +++++++++++++++++++ .../public/404.html | 1 + 2 files changed, 64 insertions(+) create mode 100644 shared/pages/group.404/group.404.gitlab-example.com/public/404.html diff --git a/acceptance_test.go b/acceptance_test.go index 55a838812..189fc0cd4 100644 --- a/acceptance_test.go +++ b/acceptance_test.go @@ -195,6 +195,69 @@ func TestNestedSubgroups(t *testing.T) { }) } } + +func TestCustom404(t *testing.T) { + skipUnlessEnabled(t) + teardown := RunPagesProcess(t, *pagesBinary, listeners, "") + defer teardown() + + tests := []struct { + host string + path string + content string + }{ + { + host: "group.404.gitlab-example.com", + path: "project.404/not/existing-file", + content: "Custom 404 project page", + }, + { + host: "group.404.gitlab-example.com", + path: "project.404/", + content: "Custom 404 project page", + }, + { + host: "group.404.gitlab-example.com", + path: "not/existing-file", + content: "Custom 404 group page", + }, + { + host: "group.404.gitlab-example.com", + path: "not-existing-file", + content: "Custom 404 group page", + }, + { + host: "group.404.gitlab-example.com", + content: "Custom 404 group page", + }, + { + host: "domain.404.com", + content: "Custom 404 group page", + }, + { + host: "group.404.gitlab-example.com", + path: "project.no.404/not/existing-file", + content: "The page you're looking for could not be found.", + }, + } + + for _, test := range tests { + t.Run(fmt.Sprintf("%s/%s", test.host, test.path), func(t *testing.T) { + for _, spec := range listeners { + rsp, err := GetPageFromListener(t, spec, test.host, test.path) + + require.NoError(t, err) + defer rsp.Body.Close() + assert.Equal(t, http.StatusNotFound, rsp.StatusCode) + + page, err := ioutil.ReadAll(rsp.Body) + require.NoError(t, err) + require.Contains(t, string(page), test.content) + } + }) + } +} + func TestCORSWhenDisabled(t *testing.T) { skipUnlessEnabled(t) teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-disable-cross-origin-requests") diff --git a/shared/pages/group.404/group.404.gitlab-example.com/public/404.html b/shared/pages/group.404/group.404.gitlab-example.com/public/404.html new file mode 100644 index 000000000..454a3d507 --- /dev/null +++ b/shared/pages/group.404/group.404.gitlab-example.com/public/404.html @@ -0,0 +1 @@ +Custom 404 group page -- GitLab From f8cfeefec5d3e0b809463119dedc1825fc415385 Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Mon, 25 Feb 2019 15:15:14 +0100 Subject: [PATCH 2/2] Serve custom 404 even in case of auth failure --- app.go | 9 +++++++-- internal/auth/auth.go | 3 +-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app.go b/app.go index b872a6a1e..2bc83bf50 100644 --- a/app.go +++ b/app.go @@ -107,7 +107,12 @@ func (a *theApp) checkAuthenticationIfNotExists(domain *domain.D, w http.Respons } // User is authenticated, show the 404 - httperrors.Serve404(w) + if domain != nil { + domain.ServeNotFoundHTTP(w, r) + } else { + httperrors.Serve404(w) + } + return true } } @@ -210,7 +215,7 @@ func (a *theApp) serveFileOrNotFound(domain *domain.D) http.HandlerFunc { return } - httperrors.Serve404(w) + domain.ServeNotFoundHTTP(w, r) return } diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 852d462b4..6520ea019 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -415,8 +415,7 @@ func (a *Auth) CheckAuthenticationWithoutProject(w http.ResponseWriter, r *http. logRequest(r).WithError(err).Debug("Failed to retrieve info with token") } - httperrors.Serve404(w) - return true + return false } return false -- GitLab