Skip to content

Commit 8896575

Browse files
im-kulikovvishr
authored andcommitted
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 #1275 (comment)
1 parent bc28fce commit 8896575

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

middleware/slash.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package middleware
22

33
import (
4+
"strings"
5+
46
"github.com/labstack/echo/v4"
57
)
68

@@ -49,7 +51,7 @@ func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc
4951
url := req.URL
5052
path := url.Path
5153
qs := c.QueryString()
52-
if path != "/" && path[len(path)-1] != '/' {
54+
if !strings.HasSuffix(path, "/") {
5355
path += "/"
5456
uri := path
5557
if qs != "" {
@@ -97,7 +99,7 @@ func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFu
9799
path := url.Path
98100
qs := c.QueryString()
99101
l := len(path) - 1
100-
if l >= 0 && path != "/" && path[l] == '/' {
102+
if l > 0 && strings.HasSuffix(path, "/") {
101103
path = path[:l]
102104
uri := path
103105
if qs != "" {

middleware/slash_test.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,28 @@ import (
1010
)
1111

1212
func TestAddTrailingSlash(t *testing.T) {
13+
is := assert.New(t)
1314
e := echo.New()
1415
req := httptest.NewRequest(http.MethodGet, "/add-slash", nil)
1516
rec := httptest.NewRecorder()
1617
c := e.NewContext(req, rec)
1718
h := AddTrailingSlash()(func(c echo.Context) error {
1819
return nil
1920
})
20-
h(c)
21+
is.NoError(h(c))
22+
is.Equal("/add-slash/", req.URL.Path)
23+
is.Equal("/add-slash/", req.RequestURI)
2124

22-
assert := assert.New(t)
23-
assert.Equal("/add-slash/", req.URL.Path)
24-
assert.Equal("/add-slash/", req.RequestURI)
25+
// Method Connect must not fail:
26+
req = httptest.NewRequest(http.MethodConnect, "", nil)
27+
rec = httptest.NewRecorder()
28+
c = e.NewContext(req, rec)
29+
h = AddTrailingSlash()(func(c echo.Context) error {
30+
return nil
31+
})
32+
is.NoError(h(c))
33+
is.Equal("/", req.URL.Path)
34+
is.Equal("/", req.RequestURI)
2535

2636
// With config
2737
req = httptest.NewRequest(http.MethodGet, "/add-slash?key=value", nil)
@@ -32,25 +42,34 @@ func TestAddTrailingSlash(t *testing.T) {
3242
})(func(c echo.Context) error {
3343
return nil
3444
})
35-
h(c)
36-
assert.Equal(http.StatusMovedPermanently, rec.Code)
37-
assert.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
45+
is.NoError(h(c))
46+
is.Equal(http.StatusMovedPermanently, rec.Code)
47+
is.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
3848
}
3949

4050
func TestRemoveTrailingSlash(t *testing.T) {
51+
is := assert.New(t)
4152
e := echo.New()
4253
req := httptest.NewRequest(http.MethodGet, "/remove-slash/", nil)
4354
rec := httptest.NewRecorder()
4455
c := e.NewContext(req, rec)
4556
h := RemoveTrailingSlash()(func(c echo.Context) error {
4657
return nil
4758
})
48-
h(c)
49-
50-
assert := assert.New(t)
59+
is.NoError(h(c))
60+
is.Equal("/remove-slash", req.URL.Path)
61+
is.Equal("/remove-slash", req.RequestURI)
5162

52-
assert.Equal("/remove-slash", req.URL.Path)
53-
assert.Equal("/remove-slash", req.RequestURI)
63+
// Method Connect must not fail:
64+
req = httptest.NewRequest(http.MethodConnect, "", nil)
65+
rec = httptest.NewRecorder()
66+
c = e.NewContext(req, rec)
67+
h = RemoveTrailingSlash()(func(c echo.Context) error {
68+
return nil
69+
})
70+
is.NoError(h(c))
71+
is.Equal("", req.URL.Path)
72+
is.Equal("", req.RequestURI)
5473

5574
// With config
5675
req = httptest.NewRequest(http.MethodGet, "/remove-slash/?key=value", nil)
@@ -61,9 +80,9 @@ func TestRemoveTrailingSlash(t *testing.T) {
6180
})(func(c echo.Context) error {
6281
return nil
6382
})
64-
h(c)
65-
assert.Equal(http.StatusMovedPermanently, rec.Code)
66-
assert.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
83+
is.NoError(h(c))
84+
is.Equal(http.StatusMovedPermanently, rec.Code)
85+
is.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
6786

6887
// With bare URL
6988
req = httptest.NewRequest(http.MethodGet, "http://localhost", nil)
@@ -72,6 +91,6 @@ func TestRemoveTrailingSlash(t *testing.T) {
7291
h = RemoveTrailingSlash()(func(c echo.Context) error {
7392
return nil
7493
})
75-
h(c)
76-
assert.Equal("", req.URL.Path)
94+
is.NoError(h(c))
95+
is.Equal("", req.URL.Path)
7796
}

0 commit comments

Comments
 (0)