1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

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

* Simplify code of Add/Remove trailing slash

- simplify code (more informative / understanding)
- assert collides with imported package name (in tests)
- fix unhandled errors

* add tests for https://github.com/labstack/echo/pull/1275#issuecomment-460467700
This commit is contained in:
Evgeniy Kulikov 2019-02-07 20:49:51 +03:00 committed by Vishal Rana
parent bc28fceaf3
commit 88965757af
2 changed files with 40 additions and 19 deletions

View File

@ -1,6 +1,8 @@
package middleware package middleware
import ( import (
"strings"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -49,7 +51,7 @@ func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc
url := req.URL url := req.URL
path := url.Path path := url.Path
qs := c.QueryString() qs := c.QueryString()
if path != "/" && path[len(path)-1] != '/' { if !strings.HasSuffix(path, "/") {
path += "/" path += "/"
uri := path uri := path
if qs != "" { if qs != "" {
@ -97,7 +99,7 @@ func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFu
path := url.Path path := url.Path
qs := c.QueryString() qs := c.QueryString()
l := len(path) - 1 l := len(path) - 1
if l >= 0 && path != "/" && path[l] == '/' { if l > 0 && strings.HasSuffix(path, "/") {
path = path[:l] path = path[:l]
uri := path uri := path
if qs != "" { if qs != "" {

View File

@ -10,6 +10,7 @@ import (
) )
func TestAddTrailingSlash(t *testing.T) { func TestAddTrailingSlash(t *testing.T) {
is := assert.New(t)
e := echo.New() e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/add-slash", nil) req := httptest.NewRequest(http.MethodGet, "/add-slash", nil)
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
@ -17,11 +18,20 @@ func TestAddTrailingSlash(t *testing.T) {
h := AddTrailingSlash()(func(c echo.Context) error { h := AddTrailingSlash()(func(c echo.Context) error {
return nil 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) // Method Connect must not fail:
assert.Equal("/add-slash/", req.URL.Path) req = httptest.NewRequest(http.MethodConnect, "", nil)
assert.Equal("/add-slash/", req.RequestURI) 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 // With config
req = httptest.NewRequest(http.MethodGet, "/add-slash?key=value", nil) req = httptest.NewRequest(http.MethodGet, "/add-slash?key=value", nil)
@ -32,12 +42,13 @@ func TestAddTrailingSlash(t *testing.T) {
})(func(c echo.Context) error { })(func(c echo.Context) error {
return nil return nil
}) })
h(c) is.NoError(h(c))
assert.Equal(http.StatusMovedPermanently, rec.Code) is.Equal(http.StatusMovedPermanently, rec.Code)
assert.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation)) is.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
} }
func TestRemoveTrailingSlash(t *testing.T) { func TestRemoveTrailingSlash(t *testing.T) {
is := assert.New(t)
e := echo.New() e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/remove-slash/", nil) req := httptest.NewRequest(http.MethodGet, "/remove-slash/", nil)
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
@ -45,12 +56,20 @@ func TestRemoveTrailingSlash(t *testing.T) {
h := RemoveTrailingSlash()(func(c echo.Context) error { h := RemoveTrailingSlash()(func(c echo.Context) error {
return nil return nil
}) })
h(c) is.NoError(h(c))
is.Equal("/remove-slash", req.URL.Path)
is.Equal("/remove-slash", req.RequestURI)
assert := assert.New(t) // Method Connect must not fail:
req = httptest.NewRequest(http.MethodConnect, "", nil)
assert.Equal("/remove-slash", req.URL.Path) rec = httptest.NewRecorder()
assert.Equal("/remove-slash", req.RequestURI) 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 // With config
req = httptest.NewRequest(http.MethodGet, "/remove-slash/?key=value", nil) req = httptest.NewRequest(http.MethodGet, "/remove-slash/?key=value", nil)
@ -61,9 +80,9 @@ func TestRemoveTrailingSlash(t *testing.T) {
})(func(c echo.Context) error { })(func(c echo.Context) error {
return nil return nil
}) })
h(c) is.NoError(h(c))
assert.Equal(http.StatusMovedPermanently, rec.Code) is.Equal(http.StatusMovedPermanently, rec.Code)
assert.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation)) is.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
// With bare URL // With bare URL
req = httptest.NewRequest(http.MethodGet, "http://localhost", nil) req = httptest.NewRequest(http.MethodGet, "http://localhost", nil)
@ -72,6 +91,6 @@ func TestRemoveTrailingSlash(t *testing.T) {
h = RemoveTrailingSlash()(func(c echo.Context) error { h = RemoveTrailingSlash()(func(c echo.Context) error {
return nil return nil
}) })
h(c) is.NoError(h(c))
assert.Equal("", req.URL.Path) is.Equal("", req.URL.Path)
} }