Don't duplicate methods in Allow: header for 405 responses (#1029)

This commit is contained in:
Jonathan Hall 2026-08-20 05:16:25 -04:00 committed by GitHub
parent 29164f023b
commit 9b6ddcddb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 4 deletions

View file

@ -394,14 +394,18 @@ func TestMuxNestedNotFound(t *testing.T) {
func TestMethodNotAllowed(t *testing.T) {
r := NewRouter()
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
r.Get("/hi", func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("hi, get"))
})
r.Head("/hi", func(w http.ResponseWriter, r *http.Request) {
r.Head("/hi", func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("hi, head"))
})
r.Get("/*", func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("catch-all"))
})
ts := httptest.NewServer(r)
defer ts.Close()

View file

@ -479,7 +479,9 @@ func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
if endpoints == mALL || endpoints == mSTUB {
continue
}
rctx.methodsAllowed = append(rctx.methodsAllowed, endpoints)
if !slices.Contains(rctx.methodsAllowed, endpoints) {
rctx.methodsAllowed = append(rctx.methodsAllowed, endpoints)
}
}
// flag that the routing context found a route, but not a corresponding
@ -525,7 +527,9 @@ func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
if endpoints == mALL || endpoints == mSTUB {
continue
}
rctx.methodsAllowed = append(rctx.methodsAllowed, endpoints)
if !slices.Contains(rctx.methodsAllowed, endpoints) {
rctx.methodsAllowed = append(rctx.methodsAllowed, endpoints)
}
}
// flag that the routing context found a route, but not a corresponding