From 5fff32dd852a27151a08dea0cb2a8fd2983d7f65 Mon Sep 17 00:00:00 2001 From: Nathan Friend Date: Fri, 20 Aug 2021 06:52:09 -0400 Subject: Splat and placeholder support in _redirects This commit adds support for Netlify-style splats (*) and :placeholders in the _redirects file. Changelog: added --- test/acceptance/redirects_test.go | 15 ++++++++-- test/acceptance/rewrites_test.go | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 test/acceptance/rewrites_test.go (limited to 'test') diff --git a/test/acceptance/redirects_test.go b/test/acceptance/redirects_test.go index f2748789..dd1b6891 100644 --- a/test/acceptance/redirects_test.go +++ b/test/acceptance/redirects_test.go @@ -7,12 +7,14 @@ import ( "testing" "github.com/stretchr/testify/require" + + redirects "gitlab.com/gitlab-org/gitlab-pages/internal/redirects" ) func TestDisabledRedirects(t *testing.T) { RunPagesProcess(t, withListeners([]ListenSpec{httpListener}), - withEnv([]string{"FF_ENABLE_REDIRECTS=false"}), + withEnv([]string{"FF_ENABLE_REDIRECTS=false", redirects.FFEnablePlaceholders + "=true"}), ) // Test that redirects status page is forbidden @@ -33,6 +35,7 @@ func TestDisabledRedirects(t *testing.T) { func TestRedirectStatusPage(t *testing.T) { RunPagesProcess(t, withListeners([]ListenSpec{httpListener}), + withEnv([]string{redirects.FFEnablePlaceholders + "=true"}), ) rsp, err := GetPageFromListener(t, httpListener, "group.redirects.gitlab-example.com", "/project-redirects/_redirects") @@ -42,13 +45,14 @@ func TestRedirectStatusPage(t *testing.T) { require.NoError(t, err) defer rsp.Body.Close() - require.Contains(t, string(body), "11 rules") + require.Contains(t, string(body), "14 rules") require.Equal(t, http.StatusOK, rsp.StatusCode) } func TestRedirect(t *testing.T) { RunPagesProcess(t, withListeners([]ListenSpec{httpListener}), + withEnv([]string{redirects.FFEnablePlaceholders + "=true"}), ) // Test that serving a file still works with redirects enabled @@ -99,6 +103,13 @@ func TestRedirect(t *testing.T) { expectedStatus: http.StatusFound, expectedLocation: "/magic-land.html", }, + // Permanent redirect for splat (*) with replacement (:splat) + { + host: "group.redirects.gitlab-example.com", + path: "/project-redirects/jobs/assistant-to-the-regional-manager.html", + expectedStatus: http.StatusMovedPermanently, + expectedLocation: "/project-redirects/careers/assistant-to-the-regional-manager.html", + }, } for _, tt := range tests { diff --git a/test/acceptance/rewrites_test.go b/test/acceptance/rewrites_test.go new file mode 100644 index 00000000..a446a8cd --- /dev/null +++ b/test/acceptance/rewrites_test.go @@ -0,0 +1,59 @@ +package acceptance_test + +import ( + "io/ioutil" + "net/http" + "testing" + + "github.com/stretchr/testify/require" + + redirects "gitlab.com/gitlab-org/gitlab-pages/internal/redirects" +) + +func TestRewrites(t *testing.T) { + RunPagesProcess(t, + withListeners([]ListenSpec{httpListener}), + withEnv([]string{redirects.FFEnablePlaceholders + "=true"}), + ) + + tests := map[string]struct { + host string + path string + expectedBody string + }{ + "rewrite_for_splat_with_replacement": { + host: "group.redirects.gitlab-example.com", + path: "/project-redirects/arrakis/visitors-guide.html", + expectedBody: "Welcome to Dune!", + }, + "splat_with_no_replacement": { + host: "group.redirects.gitlab-example.com", + path: "/project-redirects/spa/client/side/route", + expectedBody: "This is an SPA", + }, + "existing_content_takes_precedence_over_rewrite_rules": { + host: "group.redirects.gitlab-example.com", + path: "/project-redirects/spa/existing-file.html", + expectedBody: "This is an existing file", + }, + "rewrite_using_placeholders": { + host: "group.redirects.gitlab-example.com", + path: "/project-redirects/blog/2021/08/12", + expectedBody: "Rewrites are pretty neat!", + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + rsp, err := GetPageFromListener(t, httpListener, tt.host, tt.path) + require.NoError(t, err) + defer rsp.Body.Close() + + body, err := ioutil.ReadAll(rsp.Body) + require.NoError(t, err) + + require.Contains(t, string(body), tt.expectedBody) + require.Equal(t, http.StatusOK, rsp.StatusCode) + }) + } +} -- cgit v1.2.3