Skip to content
Myr

fix(header): Allow header according to RFC 7231 (HTTP 405) (#3759)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Gabriel Augendre · committed by GitHub · parent e957d1a · Browse files · 2 files+23 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Helios <i@shangyes.net>

Diff stats

2 files+23 added−3 removed

  • other:+23 added−3 removed

2 files+23 added−3 removed

modified:gin.go+11 added-3 removed
631 unmodified lines
632 break633 }634635 if engine.HandleMethodNotAllowed {636 // According to RFC 7231 section 6.5.5, MUST generate an Allow header field in response637 // containing a list of the target resource's currently supported methods.638 allowed := make([]string, 0, len(t)-1)639 for _, tree := range engine.trees {640 if tree.method == httpMethod {641 continue642 }643 if value := tree.root.getValue(rPath, nil, c.skippedNodes, unescape); value.handlers != nil {641 c.handlers = engine.allNoMethod642 serveError(c, http.StatusMethodNotAllowed, default405Body)643 return644 allowed = append(allowed, tree.method)645 }646 }647 if len(allowed) > 0 {648 c.handlers = engine.allNoMethod649 c.writermem.Header().Set("Allow", strings.Join(allowed, ", "))650 serveError(c, http.StatusMethodNotAllowed, default405Body)651 return652 }653 }654655 c.handlers = engine.allNoRoute656 serveError(c, http.StatusNotFound, default404Body)657}658
60 unmodified lines
modified:routes_test.go+12 added
512 unmodified lines
513 w := PerformRequest(router, http.MethodPost, "/path2")514 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)515}516517func TestRouteNotAllowedEnabled3(t *testing.T) {518 router := New()519 router.HandleMethodNotAllowed = true520 router.GET("/path", func(c *Context) {})521 router.POST("/path", func(c *Context) {})522 w := PerformRequest(router, http.MethodPut, "/path")523 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)524 allowed := w.Header().Get("Allow")525 assert.Contains(t, allowed, "GET")526 assert.Contains(t, allowed, "POST")527}528529func TestRouteNotAllowedDisabled(t *testing.T) {530 router := New()531 router.HandleMethodNotAllowed = false532 router.POST("/path", func(c *Context) {})
258 unmodified lines