Skip to content

Commit cf00202

Browse files
authored
Merge pull request #1674 from codeocean/group-use-bug
Remove group.Use registering Any routes that break other routes
2 parents 0482cb3 + 7a1126f commit cf00202

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

group.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ func (g *Group) Use(middleware ...MiddlewareFunc) {
2323
if len(g.middleware) == 0 {
2424
return
2525
}
26-
// Allow all requests to reach the group as they might get dropped if router
27-
// doesn't find a match, making none of the group middleware process.
28-
g.Any("", NotFoundHandler)
29-
g.Any("/*", NotFoundHandler)
3026
}
3127

3228
// CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.

group_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,37 @@ func TestGroupRouteMiddlewareWithMatchAny(t *testing.T) {
119119
assert.Equal(t, "/*", m)
120120

121121
}
122+
123+
func TestMultipleGroupSamePathMiddleware(t *testing.T) {
124+
// Ensure multiple groups with the same path do not clobber previous routes or mixup middlewares
125+
e := New()
126+
m1 := func(next HandlerFunc) HandlerFunc {
127+
return func(c Context) error {
128+
c.Set("middleware", "m1")
129+
return next(c)
130+
}
131+
}
132+
m2 := func(next HandlerFunc) HandlerFunc {
133+
return func(c Context) error {
134+
c.Set("middleware", "m2")
135+
return next(c)
136+
}
137+
}
138+
h := func(c Context) error {
139+
return c.String(http.StatusOK, c.Get("middleware").(string))
140+
}
141+
142+
g1 := e.Group("/group", m1)
143+
{
144+
g1.GET("", h)
145+
}
146+
g2 := e.Group("/group", m2)
147+
{
148+
g2.GET("/other", h)
149+
}
150+
151+
_, m := request(http.MethodGet, "/group", e)
152+
assert.Equal(t, "m1", m)
153+
_, m = request(http.MethodGet, "/group/other", e)
154+
assert.Equal(t, "m2", m)
155+
}

0 commit comments

Comments
 (0)