mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Merge pull request #1659 from pafuent/panic_router_find_fails_on_params_with_no_root
Fixed panic when Router#Find fails on Param paths
This commit is contained in:
commit
8c27828f11
@ -428,7 +428,9 @@ func (r *Router) Find(method, path string, c Context) {
|
||||
pos := strings.IndexByte(ns, '/')
|
||||
if pos == -1 {
|
||||
// If no slash is remaining in search string set param value
|
||||
pvalues[len(cn.pnames)-1] = search
|
||||
if len(cn.pnames) > 0 {
|
||||
pvalues[len(cn.pnames)-1] = search
|
||||
}
|
||||
break
|
||||
} else if pos > 0 {
|
||||
// Otherwise continue route processing with restored next node
|
||||
|
@ -1298,6 +1298,40 @@ func TestRouterParam1466(t *testing.T) {
|
||||
assert.Equal(t, 0, c.response.Status)
|
||||
}
|
||||
|
||||
// Issue #1653
|
||||
func TestRouterPanicWhenParamNoRootOnlyChildsFailsFind(t *testing.T) {
|
||||
e := New()
|
||||
r := e.router
|
||||
|
||||
r.Add(http.MethodGet, "/users/create", handlerHelper("create", 1))
|
||||
r.Add(http.MethodGet, "/users/:id/edit", func(c Context) error {
|
||||
return nil
|
||||
})
|
||||
r.Add(http.MethodGet, "/users/:id/active", func(c Context) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
r.Find(http.MethodGet, "/users/alice/edit", c)
|
||||
assert.Equal(t, "alice", c.Param("id"))
|
||||
|
||||
c = e.NewContext(nil, nil).(*context)
|
||||
r.Find(http.MethodGet, "/users/bob/active", c)
|
||||
assert.Equal(t, "bob", c.Param("id"))
|
||||
|
||||
c = e.NewContext(nil, nil).(*context)
|
||||
r.Find(http.MethodGet, "/users/create", c)
|
||||
c.Handler()(c)
|
||||
assert.Equal(t, 1, c.Get("create"))
|
||||
assert.Equal(t, "/users/create", c.Get("path"))
|
||||
|
||||
//This panic before the fix for Issue #1653
|
||||
c = e.NewContext(nil, nil).(*context)
|
||||
r.Find(http.MethodGet, "/users/createNotFound", c)
|
||||
he := c.Handler()(c).(*HTTPError)
|
||||
assert.Equal(t, http.StatusNotFound, he.Code)
|
||||
}
|
||||
|
||||
func benchmarkRouterRoutes(b *testing.B, routes []*Route) {
|
||||
e := New()
|
||||
r := e.router
|
||||
|
Loading…
Reference in New Issue
Block a user