From 12ca83b790fdddbbfcf7c6b08aa9da79a3ab8f6d Mon Sep 17 00:00:00 2001 From: Nejc Habjan Date: Thu, 9 Jun 2022 15:39:27 +0200 Subject: Set up redirects config for unit tests --- internal/redirects/redirects_test.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'internal') diff --git a/internal/redirects/redirects_test.go b/internal/redirects/redirects_test.go index c0df43c0..210e8fe0 100644 --- a/internal/redirects/redirects_test.go +++ b/internal/redirects/redirects_test.go @@ -12,12 +12,20 @@ import ( "github.com/stretchr/testify/require" netlifyRedirects "github.com/tj/go-redirects" + "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/feature" "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) +const ( + defaultMaxConfigSize = 64 * 1024 + defaultMaxPathSegments = 25 + defaultMaxRuleCount = 1000 +) + func TestRedirectsRewrite(t *testing.T) { t.Setenv(feature.RedirectsPlaceholders.EnvVariable, "true") + setupRedirectsConfig() tests := []struct { name string @@ -138,6 +146,7 @@ func TestRedirectsRewrite(t *testing.T) { func TestRedirectsParseRedirects(t *testing.T) { ctx := context.Background() + setupRedirectsConfig() root, tmpDir := testhelpers.TmpDir(t) @@ -165,7 +174,7 @@ func TestRedirectsParseRedirects(t *testing.T) { }, { name: "Config file too big", - redirectsFile: strings.Repeat("a", 2*cfg.Redirects.MaxConfigSize), + redirectsFile: strings.Repeat("a", 2*cfg.MaxConfigSize), expectedRules: 0, expectedErr: errFileTooLarge, }, @@ -196,8 +205,9 @@ func TestRedirectsParseRedirects(t *testing.T) { func TestMaxRuleCount(t *testing.T) { root, tmpDir := testhelpers.TmpDir(t) + setupRedirectsConfig() - err := os.WriteFile(path.Join(tmpDir, ConfigFile), []byte(strings.Repeat("/goto.html /target.html 301\n", cfg.Redirects.MaxRuleCount-1)+ + err := os.WriteFile(path.Join(tmpDir, ConfigFile), []byte(strings.Repeat("/goto.html /target.html 301\n", cfg.MaxRuleCount-1)+ "/1000.html /target1000 301\n"+ "/1001.html /target1001 301\n", ), 0600) @@ -226,3 +236,11 @@ func TestMaxRuleCount(t *testing.T) { t.Run("maxRuleCount matches", testFn("/1000.html", "/target1000", http.StatusMovedPermanently, nil)) t.Run("maxRuleCount+1 does not match", testFn("/1001.html", "", 0, ErrNoRedirect)) } + +func setupRedirectsConfig() { + SetConfig(config.Redirects{ + MaxConfigSize: defaultMaxConfigSize, + MaxPathSegments: defaultMaxPathSegments, + MaxRuleCount: defaultMaxRuleCount, + }) +} -- cgit v1.2.3