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

Merge pull request #1674 from codeocean/group-use-bug

Remove group.Use registering Any routes that break other routes
This commit is contained in:
Pablo Andres Fuente 2020-12-16 01:34:46 -03:00 committed by GitHub
commit cf002025e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View File

@ -23,10 +23,6 @@ func (g *Group) Use(middleware ...MiddlewareFunc) {
if len(g.middleware) == 0 { if len(g.middleware) == 0 {
return return
} }
// Allow all requests to reach the group as they might get dropped if router
// doesn't find a match, making none of the group middleware process.
g.Any("", NotFoundHandler)
g.Any("/*", NotFoundHandler)
} }
// CONNECT implements `Echo#CONNECT()` for sub-routes within the Group. // CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.

View File

@ -119,3 +119,37 @@ func TestGroupRouteMiddlewareWithMatchAny(t *testing.T) {
assert.Equal(t, "/*", m) assert.Equal(t, "/*", m)
} }
func TestMultipleGroupSamePathMiddleware(t *testing.T) {
// Ensure multiple groups with the same path do not clobber previous routes or mixup middlewares
e := New()
m1 := func(next HandlerFunc) HandlerFunc {
return func(c Context) error {
c.Set("middleware", "m1")
return next(c)
}
}
m2 := func(next HandlerFunc) HandlerFunc {
return func(c Context) error {
c.Set("middleware", "m2")
return next(c)
}
}
h := func(c Context) error {
return c.String(http.StatusOK, c.Get("middleware").(string))
}
g1 := e.Group("/group", m1)
{
g1.GET("", h)
}
g2 := e.Group("/group", m2)
{
g2.GET("/other", h)
}
_, m := request(http.MethodGet, "/group", e)
assert.Equal(t, "m1", m)
_, m = request(http.MethodGet, "/group/other", e)
assert.Equal(t, "m2", m)
}