mirror of
https://github.com/labstack/echo.git
synced 2025-07-03 00:56:59 +02:00
refs #1412: Fix multi level match any routes
This commit is contained in:
@ -595,20 +595,41 @@ func TestRouterMatchAnyMultiLevel(t *testing.T) {
|
||||
// Routes
|
||||
r.Add(http.MethodGet, "/api/users/jack", handler)
|
||||
r.Add(http.MethodGet, "/api/users/jill", handler)
|
||||
r.Add(http.MethodGet, "/api/users/*", handler)
|
||||
r.Add(http.MethodGet, "/api/*", handler)
|
||||
r.Add(http.MethodGet, "/other/*", handler)
|
||||
r.Add(http.MethodGet, "/*", handler)
|
||||
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
r.Find(http.MethodGet, "/api/users/jack", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, "/api/users/jack", c.Get("path"))
|
||||
assert.Equal(t, "", c.Param("*"))
|
||||
|
||||
r.Find(http.MethodGet, "/api/users/jill", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, "/api/users/jill", c.Get("path"))
|
||||
assert.Equal(t, "", c.Param("*"))
|
||||
|
||||
r.Find(http.MethodGet, "/api/users/joe", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, "/api/users/*", c.Get("path"))
|
||||
assert.Equal(t, "joe", c.Param("*"))
|
||||
|
||||
r.Find(http.MethodGet, "/api/nousers/joe", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, "/api/*", c.Get("path"))
|
||||
assert.Equal(t, "nousers/joe", c.Param("*"))
|
||||
|
||||
r.Find(http.MethodGet, "/api/none", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, "/api/*", c.Get("path"))
|
||||
assert.Equal(t, "none", c.Param("*"))
|
||||
|
||||
r.Find(http.MethodGet, "/noapi/users/jim", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, "/*", c.Get("path"))
|
||||
assert.Equal(t, "noapi/users/jim", c.Param("*"))
|
||||
}
|
||||
|
||||
func TestRouterMicroParam(t *testing.T) {
|
||||
@ -702,6 +723,15 @@ func TestRouterPriority(t *testing.T) {
|
||||
c.Set("g", 7)
|
||||
return nil
|
||||
})
|
||||
r.Add(http.MethodGet, "/users/new/*", func(c Context) error {
|
||||
c.Set("h", 8)
|
||||
return nil
|
||||
})
|
||||
r.Add(http.MethodGet, "/*", func(c Context) error {
|
||||
c.Set("i", 9)
|
||||
return nil
|
||||
})
|
||||
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
// Route > /users
|
||||
@ -734,11 +764,46 @@ func TestRouterPriority(t *testing.T) {
|
||||
c.handler(c)
|
||||
assert.Equal(t, 3, c.Get("c"))
|
||||
|
||||
// Route > /users/newsee
|
||||
r.Find(http.MethodGet, "/users/newsee", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, 6, c.Get("f"))
|
||||
|
||||
// Route > /users/*
|
||||
r.Find(http.MethodGet, "/users/joe/books", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, 7, c.Get("g"))
|
||||
assert.Equal(t, "joe/books", c.Param("*"))
|
||||
|
||||
// Route > /users/new/* should be matched
|
||||
r.Find(http.MethodGet, "/users/new/someone", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, 8, c.Get("h"))
|
||||
assert.Equal(t, "someone", c.Param("*"))
|
||||
|
||||
// Route > /users/* should be matched although /users/dew exists
|
||||
r.Find(http.MethodGet, "/users/dew/someone", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, 7, c.Get("g"))
|
||||
assert.Equal(t, "dew/someone", c.Param("*"))
|
||||
|
||||
// Route > /users/* should be matched although /users/dew exists
|
||||
r.Find(http.MethodGet, "/users/notexists/someone/else", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, 7, c.Get("g"))
|
||||
assert.Equal(t, "notexists/someone/else", c.Param("*"))
|
||||
|
||||
// Route > *
|
||||
r.Find(http.MethodGet, "/nousers", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, 9, c.Get("i"))
|
||||
assert.Equal(t, "nousers", c.Param("*"))
|
||||
|
||||
// Route > *
|
||||
r.Find(http.MethodGet, "/nousers/new", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, 9, c.Get("i"))
|
||||
assert.Equal(t, "nousers/new", c.Param("*"))
|
||||
}
|
||||
|
||||
func TestRouterIssue1348(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user