diff --git a/gin.go b/gin.go index a1607275..dc43a4f8 100644 --- a/gin.go +++ b/gin.go @@ -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 diff --git a/middleware_test.go b/middleware_test.go index 8dc7c3b3..af417cec 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -251,3 +251,59 @@ func TestMiddlewareWrite(t *testing.T) { assert.Equal(t, http.StatusBadRequest, w.Code) assert.Equal(t, strings.ReplaceAll("hola\nbar{\"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()) +}