mirror of
https://github.com/labstack/echo.git
synced 2025-04-27 12:32:09 +02:00
fix index out of bounds if path is empty
This commit is contained in:
parent
a98843b6e5
commit
348edc31c6
@ -69,7 +69,7 @@ func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFu
|
|||||||
path := url.Path()
|
path := url.Path()
|
||||||
qs := url.QueryString()
|
qs := url.QueryString()
|
||||||
l := len(path) - 1
|
l := len(path) - 1
|
||||||
if path != "/" && path[l] == '/' {
|
if l >= 0 && path != "/" && path[l] == '/' {
|
||||||
path = path[:l]
|
path = path[:l]
|
||||||
uri := path
|
uri := path
|
||||||
if qs != "" {
|
if qs != "" {
|
||||||
|
@ -59,4 +59,15 @@ func TestRemoveTrailingSlash(t *testing.T) {
|
|||||||
h(c)
|
h(c)
|
||||||
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
||||||
assert.Equal(t, "/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
|
assert.Equal(t, "/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
|
||||||
|
|
||||||
|
// With bare URL
|
||||||
|
req = test.NewRequest(echo.GET, "http://localhost", nil)
|
||||||
|
rec = test.NewResponseRecorder()
|
||||||
|
c = e.NewContext(req, rec)
|
||||||
|
h = RemoveTrailingSlash()(func(c echo.Context) error {
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
h(c)
|
||||||
|
assert.Equal(t, "", req.URL().Path())
|
||||||
|
assert.Equal(t, "http://localhost", req.URI())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user