Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix group.RouteNotFound not working when group has attached middlewares #2411

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func (g *Group) Use(middleware ...MiddlewareFunc) {
if len(g.middleware) == 0 {
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)
// group level middlewares are different from Echo `Pre` and `Use` middlewares (those are global). Group level middlewares
// are only executed if they are added to the Router with route.
// So we register catch all route (404 is a safe way to emulate route match) for this group and now during routing the
// Router would find route to match our request path and therefore guarantee the middleware(s) will get executed.
g.RouteNotFound("", NotFoundHandler)
g.RouteNotFound("/*", NotFoundHandler)
}

// CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.
Expand Down
70 changes: 70 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,73 @@ func TestGroup_RouteNotFound(t *testing.T) {
})
}
}

func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) {
var testCases = []struct {
name string
givenCustom404 bool
whenURL string
expectBody interface{}
expectCode int
}{
{
name: "ok, custom 404 handler is called with middleware",
givenCustom404: true,
whenURL: "/group/test3",
expectBody: "GET /group/*",
expectCode: http.StatusNotFound,
},
{
name: "ok, default group 404 handler is called with middleware",
givenCustom404: false,
whenURL: "/group/test3",
expectBody: "{\"message\":\"Not Found\"}\n",
expectCode: http.StatusNotFound,
},
{
name: "ok, (no slash) default group 404 handler is called with middleware",
givenCustom404: false,
whenURL: "/group",
expectBody: "{\"message\":\"Not Found\"}\n",
expectCode: http.StatusNotFound,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

okHandler := func(c Context) error {
return c.String(http.StatusOK, c.Request().Method+" "+c.Path())
}
notFoundHandler := func(c Context) error {
return c.String(http.StatusNotFound, c.Request().Method+" "+c.Path())
}

e := New()
e.GET("/test1", okHandler)
e.RouteNotFound("/*", notFoundHandler)

g := e.Group("/group")
g.GET("/test1", okHandler)

middlewareCalled := false
g.Use(func(next HandlerFunc) HandlerFunc {
return func(c Context) error {
middlewareCalled = true
return next(c)
}
})
if tc.givenCustom404 {
g.RouteNotFound("/*", notFoundHandler)
}

req := httptest.NewRequest(http.MethodGet, tc.whenURL, nil)
rec := httptest.NewRecorder()

e.ServeHTTP(rec, req)

assert.True(t, middlewareCalled)
assert.Equal(t, tc.expectCode, rec.Code)
assert.Equal(t, tc.expectBody, rec.Body.String())
})
}
}