diff --git a/middlewares/replace_path.go b/middlewares/replace_path.go index 208415a51..8d23b905c 100644 --- a/middlewares/replace_path.go +++ b/middlewares/replace_path.go @@ -16,5 +16,6 @@ const ReplacedPathHeader = "X-Replaced-Path" func (s *ReplacePath) ServeHTTP(w http.ResponseWriter, r *http.Request) { r.Header.Add(ReplacedPathHeader, r.URL.Path) r.URL.Path = s.Path + r.RequestURI = r.URL.RequestURI() s.Handler.ServeHTTP(w, r) } diff --git a/middlewares/replace_path_test.go b/middlewares/replace_path_test.go index 179ebdabb..838a7b633 100644 --- a/middlewares/replace_path_test.go +++ b/middlewares/replace_path_test.go @@ -1,10 +1,11 @@ -package middlewares_test +package middlewares import ( "net/http" "testing" - "github.com/containous/traefik/middlewares" + "github.com/containous/traefik/testhelpers" + "github.com/stretchr/testify/assert" ) func TestReplacePath(t *testing.T) { @@ -17,28 +18,24 @@ func TestReplacePath(t *testing.T) { for _, path := range paths { t.Run(path, func(t *testing.T) { - var newPath, oldPath string - handler := &middlewares.ReplacePath{ + + var expectedPath, actualHeader, requestURI string + handler := &ReplacePath{ Path: replacementPath, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - newPath = r.URL.Path - oldPath = r.Header.Get("X-Replaced-Path") + expectedPath = r.URL.Path + actualHeader = r.Header.Get(ReplacedPathHeader) + requestURI = r.RequestURI }), } - req, err := http.NewRequest("GET", "http://localhost"+path, nil) - if err != nil { - t.Error(err) - } + req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+path, nil) handler.ServeHTTP(nil, req) - if newPath != replacementPath { - t.Fatalf("new path should be '%s'", replacementPath) - } - if oldPath != path { - t.Fatalf("old path should be '%s'", path) - } + assert.Equal(t, expectedPath, replacementPath, "Unexpected path.") + assert.Equal(t, path, actualHeader, "Unexpected '%s' header.", ReplacedPathHeader) + assert.Equal(t, expectedPath, requestURI, "Unexpected request URI.") }) } }