From 326a4e7fc8d54ffe9032140918370ebe64b10891 Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Thu, 24 Apr 2025 16:34:24 +0200 Subject: [PATCH] =?UTF-8?q?Update=20redirect=20logic=20to=20redirect=20ver?= =?UTF-8?q?sions=20=E2=89=A5=205.2=20to=20new=20docs=20domain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler.go | 6 +++--- handler_test.go | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/handler.go b/handler.go index 65b8aa6..fe4f058 100644 --- a/handler.go +++ b/handler.go @@ -14,7 +14,7 @@ import ( // versionPattern matches version strings like @5.2, @5.2.0, etc. and captures major and minor version numbers var versionPattern = regexp.MustCompile(`^@(\d+)\.(\d+)(?:\.(\d+))?$`) -// shouldRedirectVersion returns true for versions ≥ 5.3 (format: @major.minor[.patch]) +// shouldRedirectVersion returns true for versions ≥ 5.2 (format: @major.minor[.patch]) func shouldRedirectVersion(version string) bool { matches := versionPattern.FindStringSubmatch(version) if len(matches) < 3 { @@ -27,7 +27,7 @@ func shouldRedirectVersion(version string) bool { return false } - return major > 5 || (major == 5 && minor >= 3) + return major > 5 || (major == 5 && minor >= 2) } // Handler returns an http.Handler that serves the site. @@ -146,7 +146,7 @@ func (s *Site) Handler() http.Handler { contentVersion = r.URL.Path[1 : 1+end] } - // Redirect versions ≥ 5.3 to new docs domain with path preservation + // Redirect versions ≥ 5.2 to new docs domain with path preservation version := "@" + contentVersion if shouldRedirectVersion(version) { newURL := "https://www.sourcegraph.com/docs/" + contentVersion + "/" diff --git a/handler_test.go b/handler_test.go index 937b56b..f0585d5 100644 --- a/handler_test.go +++ b/handler_test.go @@ -272,11 +272,14 @@ func TestSite_Handler(t *testing.T) { checkResponseStatus(t, rr, http.StatusNotFound) }) - t.Run("version 5.2 - no redirect", func(t *testing.T) { + t.Run("version 5.2 - should redirect", func(t *testing.T) { rr := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/@5.2", nil) handler.ServeHTTP(rr, req) - checkResponseStatus(t, rr, http.StatusNotFound) + checkResponseStatus(t, rr, http.StatusPermanentRedirect) + if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/5.2/"; got != want { + t.Errorf("got redirect Location %q, want %q", got, want) + } }) t.Run("version 5.3 - should redirect", func(t *testing.T) {