internal/frontend: preserve query parameters when redirecting /v1beta/ to /v1/ Fixes golang/go#81045 Change-Id: Ic3d914069bb9e3536f313c292fe98c48b90b6808 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/824864 Auto-Submit: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Ethan Lee <ethanalee@google.com> kokoro-CI: kokoro <noreply+kokoro@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/internal/frontend/frontend_test.go b/internal/frontend/frontend_test.go index c155e33..3084546 100644 --- a/internal/frontend/frontend_test.go +++ b/internal/frontend/frontend_test.go
@@ -169,6 +169,17 @@ t.Errorf("got Location = %q, want %q", got, "/v1/moo") } }) + + t.Run("redirect /v1beta/ path with query", func(t *testing.T) { + w := httptest.NewRecorder() + handler.ServeHTTP(w, httptest.NewRequest("GET", "/v1beta/search?q=uuid&limit=25", nil)) + if w.Code != http.StatusMovedPermanently { + t.Errorf("got status code = %d, want %d", w.Code, http.StatusMovedPermanently) + } + if got, want := w.Header().Get("Location"), "/v1/search?q=uuid&limit=25"; got != want { + t.Errorf("got Location = %q, want %q", got, want) + } + }) } func TestAPIUnknownEndpoint(t *testing.T) {
diff --git a/internal/frontend/server.go b/internal/frontend/server.go index 1ba3db3..9bcf822 100644 --- a/internal/frontend/server.go +++ b/internal/frontend/server.go
@@ -266,8 +266,12 @@ http.Redirect(w, r, "/v1/api", http.StatusMovedPermanently) })) handle("/v1beta/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - path := strings.Replace(r.URL.Path, "/v1beta/", "/v1/", 1) - http.Redirect(w, r, path, http.StatusMovedPermanently) + u := *r.URL + u.Path = strings.Replace(u.Path, "/v1beta/", "/v1/", 1) + if u.RawPath != "" { + u.RawPath = strings.Replace(u.RawPath, "/v1beta/", "/v1/", 1) + } + http.Redirect(w, r, u.String(), http.StatusMovedPermanently) })) handle("/v1/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { api.ServeError(w, r, api.BadRequest("unknown API endpoint",