Skip to content

Simplify code of Add/Remove trailing slash and fix bug #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions middleware/slash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package middleware

import (
"strings"

"github.com/labstack/echo/v4"
)

Expand Down Expand Up @@ -49,7 +51,7 @@ func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc
url := req.URL
path := url.Path
qs := c.QueryString()
if path != "/" && path[len(path)-1] != '/' {
if !strings.HasSuffix(path, "/") {
path += "/"
uri := path
if qs != "" {
Expand Down Expand Up @@ -97,7 +99,7 @@ func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFu
path := url.Path
qs := c.QueryString()
l := len(path) - 1
if l >= 0 && path != "/" && path[l] == '/' {
if l > 0 && strings.HasSuffix(path, "/") {
path = path[:l]
uri := path
if qs != "" {
Expand Down
53 changes: 36 additions & 17 deletions middleware/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@ import (
)

func TestAddTrailingSlash(t *testing.T) {
is := assert.New(t)
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/add-slash", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := AddTrailingSlash()(func(c echo.Context) error {
return nil
})
h(c)
is.NoError(h(c))
is.Equal("/add-slash/", req.URL.Path)
is.Equal("/add-slash/", req.RequestURI)

assert := assert.New(t)
assert.Equal("/add-slash/", req.URL.Path)
assert.Equal("/add-slash/", req.RequestURI)
// Method Connect must not fail:
req = httptest.NewRequest(http.MethodConnect, "", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
h = AddTrailingSlash()(func(c echo.Context) error {
return nil
})
is.NoError(h(c))
is.Equal("/", req.URL.Path)
is.Equal("/", req.RequestURI)

// With config
req = httptest.NewRequest(http.MethodGet, "/add-slash?key=value", nil)
Expand All @@ -32,25 +42,34 @@ func TestAddTrailingSlash(t *testing.T) {
})(func(c echo.Context) error {
return nil
})
h(c)
assert.Equal(http.StatusMovedPermanently, rec.Code)
assert.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
is.NoError(h(c))
is.Equal(http.StatusMovedPermanently, rec.Code)
is.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
}

func TestRemoveTrailingSlash(t *testing.T) {
is := assert.New(t)
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/remove-slash/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := RemoveTrailingSlash()(func(c echo.Context) error {
return nil
})
h(c)

assert := assert.New(t)
is.NoError(h(c))
is.Equal("/remove-slash", req.URL.Path)
is.Equal("/remove-slash", req.RequestURI)

assert.Equal("/remove-slash", req.URL.Path)
assert.Equal("/remove-slash", req.RequestURI)
// Method Connect must not fail:
req = httptest.NewRequest(http.MethodConnect, "", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
h = RemoveTrailingSlash()(func(c echo.Context) error {
return nil
})
is.NoError(h(c))
is.Equal("", req.URL.Path)
is.Equal("", req.RequestURI)

// With config
req = httptest.NewRequest(http.MethodGet, "/remove-slash/?key=value", nil)
Expand All @@ -61,9 +80,9 @@ func TestRemoveTrailingSlash(t *testing.T) {
})(func(c echo.Context) error {
return nil
})
h(c)
assert.Equal(http.StatusMovedPermanently, rec.Code)
assert.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
is.NoError(h(c))
is.Equal(http.StatusMovedPermanently, rec.Code)
is.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))

// With bare URL
req = httptest.NewRequest(http.MethodGet, "http://localhost", nil)
Expand All @@ -72,6 +91,6 @@ func TestRemoveTrailingSlash(t *testing.T) {
h = RemoveTrailingSlash()(func(c echo.Context) error {
return nil
})
h(c)
assert.Equal("", req.URL.Path)
is.NoError(h(c))
is.Equal("", req.URL.Path)
}