From 277abcf91c67b4dcbd73c81f38619bfe32bfb60d Mon Sep 17 00:00:00 2001 From: repenno Date: Mon, 15 Oct 2018 15:41:35 -0700 Subject: [PATCH] Redirect HTTP requests with trailing slashes Fixes #972 Signed-off-by: repenno --- server/server.go | 1 + server/server_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index 8c483b2efc..f6f2f08eeb 100644 --- a/server/server.go +++ b/server/server.go @@ -128,6 +128,7 @@ func New() *Server { // Initialize HTTP handlers. router := mux.NewRouter() router.UseEncodedPath() + router.StrictSlash(true) router.Handle("/metrics", promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{})).Methods(http.MethodGet) s.registerHandler(router, 0, "/data/{path:.+}", http.MethodPost, promhttp.InstrumentHandlerDuration(v0DataDur, http.HandlerFunc(s.v0DataPost))) s.registerHandler(router, 0, "/data", http.MethodPost, promhttp.InstrumentHandlerDuration(v0DataDur, http.HandlerFunc(s.v0DataPost))) diff --git a/server/server_test.go b/server/server_test.go index 6282f0a9d3..dd91abcce2 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -152,7 +152,7 @@ func Test405StatusCodev1(t *testing.T) { } } -// Tests that the responses for (theoretically) valid resources but with forbidden mewthods return the proper status code +// Tests that the responses for (theoretically) valid resources but with forbidden methods return the proper status code func Test405StatusCodev0(t *testing.T) { tests := []struct { note string @@ -359,6 +359,42 @@ func TestCompileV1UnsafeBuiltin(t *testing.T) { } } +func TestDataV1Redirection(t *testing.T) { + f := newFixture(t) + // Testing redirect at the root level + if err := f.v1(http.MethodPut, "/data/", `{"foo": [1,2,3]}`, 301, ""); err != nil { + t.Fatalf("Unexpected error from PUT: %v", err) + } + locHdr := f.recorder.Header().Get("Location") + if strings.Compare(locHdr, "/v1/data") != 0 { + t.Fatalf("Unexpected error Location header value: %v", locHdr) + } + RedirectedPath := strings.SplitAfter(locHdr, "/v1")[1] + fmt.Println(RedirectedPath) + if err := f.v1(http.MethodPut, RedirectedPath, `{"foo": [1,2,3]}`, 204, ""); err != nil { + t.Fatalf("Unexpected error from PUT: %v", err) + } + if err := f.v1(http.MethodGet, RedirectedPath, "", 200, `{"result": {"foo": [1,2,3]}}`); err != nil { + t.Fatalf("Unexpected error from GET: %v", err) + } + // Now we test redirection a few levels down + if err := f.v1(http.MethodPut, "/data/a/b/c/", `{"foo": [1,2,3]}`, 301, ""); err != nil { + t.Fatalf("Unexpected error from PUT: %v", err) + } + locHdrLv := f.recorder.Header().Get("Location") + if strings.Compare(locHdrLv, "/v1/data/a/b/c") != 0 { + t.Fatalf("Unexpected error Location header value: %v", locHdrLv) + } + RedirectedPathLvl := strings.SplitAfter(locHdrLv, "/v1")[1] + fmt.Println(RedirectedPathLvl) + if err := f.v1(http.MethodPut, RedirectedPathLvl, `{"foo": [1,2,3]}`, 204, ""); err != nil { + t.Fatalf("Unexpected error from PUT: %v", err) + } + if err := f.v1(http.MethodGet, RedirectedPathLvl, "", 200, `{"result": {"foo": [1,2,3]}}`); err != nil { + t.Fatalf("Unexpected error from GET: %v", err) + } +} + func TestDataV1(t *testing.T) { testMod1 := `package testmod