Merge de1749c6f41a42e5a870d8a9d2aaac1f9a71aea9 into 43fe48e8a0f44af783116cdb010725e6bb50255f

This commit is contained in:
Ruben Pari 2026-10-04 20:27:23 +00:00 committed by GitHub
commit 729906bc8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 0 deletions

9
gin.go
View File

@ -129,6 +129,12 @@ type Engine struct {
// handler.
HandleMethodNotAllowed bool
// NoMethodSkipHandlers when true, the 405 chain is only Engine.NoMethod
// handlers — global middleware from Engine.Use is not prepended. Default
// false keeps historical behavior (logger/recovery still run on 405).
// Only applies when HandleMethodNotAllowed is true.
NoMethodSkipHandlers bool
// ForwardedByClientIP if enabled, client IP will be parsed from the request's headers that
// match those stored at `(*gin.Engine).RemoteIPHeaders`. If no IP was
// fetched, it falls back to the IP obtained from
@ -778,6 +784,9 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
}
if len(allowed) > 0 {
c.handlers = engine.allNoMethod
if engine.NoMethodSkipHandlers {
c.handlers = engine.noMethod
}
c.writermem.Header().Set("Allow", strings.Join(allowed, ", "))
serveError(c, http.StatusMethodNotAllowed, default405Body)
return

View File

@ -251,3 +251,59 @@ func TestMiddlewareWrite(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, strings.ReplaceAll("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", ""), strings.ReplaceAll(w.Body.String(), " ", ""))
}
func TestNoMethodSkipHandlers(t *testing.T) {
router := New()
router.HandleMethodNotAllowed = true
router.NoMethodSkipHandlers = true
router.Use(func(c *Context) {
c.String(http.StatusBadRequest, "mw")
c.Abort()
})
router.NoMethod(func(c *Context) {
c.String(http.StatusMethodNotAllowed, "no method")
})
router.POST("/ping", func(c *Context) {
c.String(http.StatusOK, "pong")
})
w := PerformRequest(router, http.MethodGet, "/ping")
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
assert.Equal(t, "no method", w.Body.String())
}
func TestNoMethodIncludesGlobalMiddlewareByDefault(t *testing.T) {
router := New()
router.HandleMethodNotAllowed = true
// NoMethodSkipHandlers left false
router.Use(func(c *Context) {
c.String(http.StatusBadRequest, "mw")
c.Abort()
})
router.NoMethod(func(c *Context) {
c.String(http.StatusMethodNotAllowed, "no method")
})
router.POST("/ping", func(c *Context) { c.Status(http.StatusOK) })
w := PerformRequest(router, http.MethodGet, "/ping")
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, "mw", w.Body.String())
}
func TestNoMethodSkipHandlersSetAfterRegistration(t *testing.T) {
router := New()
router.HandleMethodNotAllowed = true
router.Use(func(c *Context) {
c.String(http.StatusBadRequest, "mw")
c.Abort()
})
router.NoMethod(func(c *Context) {
c.String(http.StatusMethodNotAllowed, "no method")
})
router.NoMethodSkipHandlers = true
router.POST("/ping", func(c *Context) { c.Status(http.StatusOK) })
w := PerformRequest(router, http.MethodGet, "/ping")
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
assert.Equal(t, "no method", w.Body.String())
}