1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Parameterized routes sometimes return 404 (#1480)

* url param bug

* add comment

* add tests

* Bump echo version
This commit is contained in:
Ajitem Sahasrabuddhe 2020-01-24 05:43:18 +05:30 committed by Vishal Rana
parent 399da56370
commit 5bf6888444
3 changed files with 35 additions and 1 deletions

View File

@ -227,7 +227,7 @@ const (
const ( const (
// Version of Echo // Version of Echo
Version = "4.1.13" Version = "4.1.14"
website = "https://echo.labstack.com" website = "https://echo.labstack.com"
// http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo // http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo
banner = ` banner = `

View File

@ -414,6 +414,10 @@ func (r *Router) Find(method, path string, c Context) {
if cn = nn.findChildByKind(pkind); cn != nil && strings.IndexByte(ns, '/') == -1 { if cn = nn.findChildByKind(pkind); cn != nil && strings.IndexByte(ns, '/') == -1 {
pvalues[len(cn.pnames)-1] = search pvalues[len(cn.pnames)-1] = search
break break
} else if cn != nil && strings.IndexByte(ns, '/') != 1 {
// If slash is present, it means that this is a parameterized route.
cn = cn.parent
goto Param
} }
for { for {
np = nn.parent np = nn.parent

View File

@ -1141,6 +1141,16 @@ func TestRouterParam1466(t *testing.T) {
r.Add(http.MethodGet, "/skills/:name/users", func(c Context) error { r.Add(http.MethodGet, "/skills/:name/users", func(c Context) error {
return nil return nil
}) })
// Additional routes for Issue 1479
r.Add(http.MethodGet, "/users/:username/likes/projects/ids", func(c Context) error {
return nil
})
r.Add(http.MethodGet, "/users/:username/profile", func(c Context) error {
return nil
})
r.Add(http.MethodGet, "/users/:username/uploads/:type", func(c Context) error {
return nil
})
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
@ -1152,6 +1162,26 @@ func TestRouterParam1466(t *testing.T) {
r.Find(http.MethodGet, "/users/signup", c) r.Find(http.MethodGet, "/users/signup", c)
assert.Equal(t, "", c.Param("username")) assert.Equal(t, "", c.Param("username"))
// Additional assertions for #1479
r.Find(http.MethodGet, "/users/sharewithme/likes/projects/ids", c)
assert.Equal(t, "sharewithme", c.Param("username"))
r.Find(http.MethodGet, "/users/ajitem/likes/projects/ids", c)
assert.Equal(t, "ajitem", c.Param("username"))
r.Find(http.MethodGet, "/users/sharewithme/profile", c)
assert.Equal(t, "sharewithme", c.Param("username"))
r.Find(http.MethodGet, "/users/ajitem/profile", c)
assert.Equal(t, "ajitem", c.Param("username"))
r.Find(http.MethodGet, "/users/sharewithme/uploads/self", c)
assert.Equal(t, "sharewithme", c.Param("username"))
assert.Equal(t, "self", c.Param("type"))
r.Find(http.MethodGet, "/users/ajitem/uploads/self", c)
assert.Equal(t, "ajitem", c.Param("username"))
assert.Equal(t, "self", c.Param("type"))
} }
func benchmarkRouterRoutes(b *testing.B, routes []*Route) { func benchmarkRouterRoutes(b *testing.B, routes []*Route) {