Skip to content

Commit

Permalink
Fixed slash middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Apr 13, 2016
1 parent 909f6da commit 11eafe9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 10 additions & 2 deletions middleware/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 0 additions & 2 deletions middleware/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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))
}

0 comments on commit 11eafe9

Please sign in to comment.