From 11eafe9b901c7598ddbac94294120e5c695941f1 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Wed, 13 Apr 2016 13:48:33 -0700 Subject: [PATCH] Fixed slash middleware Signed-off-by: Vishal Rana --- middleware/slash.go | 12 ++++++++++-- middleware/slash_test.go | 2 -- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/middleware/slash.go b/middleware/slash.go index e48e70b4..79329051 100644 --- a/middleware/slash.go +++ b/middleware/slash.go @@ -29,9 +29,13 @@ func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc rq := c.Request() url := rq.URL() path := url.Path() + qs := url.QueryString() if path != "/" && path[len(path)-1] != '/' { path += "/" - uri := path + "?" + url.QueryString() + uri := path + if qs != "" { + uri += "?" + qs + } if config.RedirectCode != 0 { return c.Redirect(config.RedirectCode, uri) } @@ -59,10 +63,14 @@ func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFu rq := c.Request() url := rq.URL() path := url.Path() + qs := url.QueryString() l := len(path) - 1 if path != "/" && path[l] == '/' { path = path[:l] - uri := path + "?" + url.QueryString() + uri := path + if qs != "" { + uri += "?" + qs + } if config.RedirectCode != 0 { return c.Redirect(config.RedirectCode, uri) } diff --git a/middleware/slash_test.go b/middleware/slash_test.go index c8776b0e..c009abaa 100644 --- a/middleware/slash_test.go +++ b/middleware/slash_test.go @@ -32,7 +32,6 @@ func TestAddTrailingSlash(t *testing.T) { }) h(c) assert.Equal(t, http.StatusMovedPermanently, rc.Status()) - assert.Equal(t, "/add-slash/?key=value", rq.URI()) assert.Equal(t, "/add-slash/?key=value", rc.Header().Get(echo.HeaderLocation)) } @@ -59,6 +58,5 @@ func TestRemoveTrailingSlash(t *testing.T) { }) h(c) assert.Equal(t, http.StatusMovedPermanently, rc.Status()) - assert.Equal(t, "/remove-slash?key=value", rq.URI()) assert.Equal(t, "/remove-slash?key=value", rc.Header().Get(echo.HeaderLocation)) }