test(gin): cover skippedNodes reset on reused pooled Contexts

The overflow reproducer only exercises one getValue walk per Context,
so a regression that only manifests on the second request served from a
recycled pooled Context would slip through. Drive the same #4818 route
table through two consecutive requests on one router so the 405 walk
re-enters getValue with reused pool state, asserting no panic and a 404
on both passes.

Follow-up to the review of #4828 (points 3-4).
This commit is contained in:
paidax 2026-09-13 14:41:48 +08:00
parent 7e3f82cbd7
commit 289b5a13a1

View File

@ -1073,6 +1073,37 @@ func TestMethodNotAllowedSkippedNodesOverflow(t *testing.T) {
assert.Equal(t, http.StatusNotFound, resp.Code)
}
// Test that the skippedNodes reset also holds on the pool-reuse path: the bug
// in #4818 was skippedNodes state leaking across getValue walks, so serve the
// same 405-triggering request twice on one router, letting the second walk run
// on a recycled pooled Context.
func TestMethodNotAllowedSkippedNodesOverflowOnContextReuse(t *testing.T) {
SetMode(TestMode)
router := New()
router.HandleMethodNotAllowed = true
h := func(c *Context) {}
router.OPTIONS("/:p0/:p1/a/:p2", h)
router.GET("/:p0/:p1/a/:p2", h)
router.PATCH("/b/:p0/:p1/c", h)
router.DELETE("/b/:p0/:p1/d/:p3", h)
router.GET("/b/:p0/:p1/e/f", h)
router.POST("/b/:p0/:p1/g/:p4/h", h)
router.OPTIONS("/b/:p0/:p1/g/:p4/h", h)
router.DELETE("/b/cache", h)
router.GET("/b/clients/:p1/g", h)
router.POST("/b/clients/:p1/g", h)
router.PATCH("/b/clients/:p1/g/:p4", h)
router.OPTIONS("/b/clients/:p1/g/:p4", h)
for i := 0; i < 2; i++ {
req := httptest.NewRequest(http.MethodPost, "/b/clients/42", nil)
resp := httptest.NewRecorder()
assert.NotPanics(t, func() { router.ServeHTTP(resp, req) })
assert.Equal(t, http.StatusNotFound, resp.Code)
}
}
// Test the fix for https://github.com/gin-gonic/gin/issues/4002
func TestMethodNotAllowedNoRoute(t *testing.T) {
g := New()