Skip to content

Commit c070e25

Browse files
committed
test(router): add test case for RouteNotFound handler in groups
Add TestRouterRouteNotFoundHandlerInGroup to verify that a RouteNotFound handler registered at root or group level is correctly invoked when a route exists but the HTTP method is not registered (issue #2485). Test scenarios covered: - Root-level RouteNotFound fires for group sub-path with wrong method - Group-level RouteNotFound fires for group sub-path with wrong method - Root-level handler fires when no group-level handler defined - Falls back to methodNotAllowedHandler when no RouteNotFound registered Signed-off-by: lyydsheep <2230561977@qq.com>
1 parent cdb8e6b commit c070e25

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

router_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,6 +3621,100 @@ func TestPathValues_GetOr(t *testing.T) {
36213621
}
36223622
}
36233623

3624+
// TestRouterRouteNotFoundHandlerInGroup verifies that a RouteNotFound handler registered
3625+
// at a parent (root or group) level is properly invoked for sub-paths when the route exists
3626+
// but the HTTP method is not registered. This is the scenario described in issue #2485.
3627+
func TestRouterRouteNotFoundHandlerInGroup(t *testing.T) {
3628+
var testCases = []struct {
3629+
name string
3630+
whenMethod string
3631+
whenURL string
3632+
expectHandlerID string
3633+
expectCode int
3634+
registerRootNotFound bool
3635+
registerGroupNotFound bool
3636+
}{
3637+
{
3638+
name: "root RouteNotFound handler fires for group sub-path with wrong method",
3639+
whenMethod: http.MethodPost,
3640+
whenURL: "/api/users",
3641+
expectHandlerID: "root-not-found",
3642+
registerRootNotFound: true,
3643+
},
3644+
{
3645+
name: "group RouteNotFound handler fires for group sub-path with wrong method",
3646+
whenMethod: http.MethodPost,
3647+
whenURL: "/api/users",
3648+
expectHandlerID: "group-not-found",
3649+
registerGroupNotFound: true,
3650+
},
3651+
{
3652+
name: "root RouteNotFound fires when no group-level handler defined",
3653+
whenMethod: http.MethodDelete,
3654+
whenURL: "/api/items/123",
3655+
expectHandlerID: "root-not-found",
3656+
registerRootNotFound: true,
3657+
},
3658+
{
3659+
name: "fallback to methodNotAllowed when no RouteNotFound handler defined",
3660+
whenMethod: http.MethodPost,
3661+
whenURL: "/api/users",
3662+
// no RouteNotFound registered → method not allowed
3663+
expectHandlerID: "method-not-allowed",
3664+
},
3665+
}
3666+
3667+
for _, tc := range testCases {
3668+
t.Run(tc.name, func(t *testing.T) {
3669+
e := New()
3670+
handlerID := ""
3671+
3672+
// Register a GET route inside a group
3673+
g := e.Group("/api")
3674+
g.GET("/users", func(c *Context) error {
3675+
handlerID = "users-get"
3676+
return nil
3677+
})
3678+
g.GET("/items/:id", func(c *Context) error {
3679+
handlerID = "items-get"
3680+
return nil
3681+
})
3682+
3683+
// Optionally register RouteNotFound at root level
3684+
if tc.registerRootNotFound {
3685+
e.RouteNotFound("/*", func(c *Context) error {
3686+
handlerID = "root-not-found"
3687+
return nil
3688+
})
3689+
}
3690+
3691+
// Optionally register RouteNotFound at group level
3692+
if tc.registerGroupNotFound {
3693+
g.RouteNotFound("/*", func(c *Context) error {
3694+
handlerID = "group-not-found"
3695+
return nil
3696+
})
3697+
}
3698+
3699+
req := httptest.NewRequest(tc.whenMethod, tc.whenURL, nil)
3700+
rec := httptest.NewRecorder()
3701+
c := e.NewContext(req, rec)
3702+
handler := e.router.Route(c)
3703+
3704+
_ = handler(c)
3705+
3706+
if tc.expectHandlerID == "method-not-allowed" {
3707+
// When no RouteNotFound is registered, the default methodNotAllowedHandler fires.
3708+
// It returns ErrMethodNotAllowed; handlerID will remain empty.
3709+
assert.Empty(t, handlerID, "expected no custom handler to fire")
3710+
} else {
3711+
assert.Equal(t, tc.expectHandlerID, handlerID,
3712+
"wrong handler fired for method=%s path=%s", tc.whenMethod, tc.whenURL)
3713+
}
3714+
})
3715+
}
3716+
}
3717+
36243718
func BenchmarkRouterStaticRoutes(b *testing.B) {
36253719
benchmarkRouterRoutes(b, staticRoutes, staticRoutes)
36263720
}

0 commit comments

Comments
 (0)