From 1e9539326d52c12224125c97e59b345146baafd1 Mon Sep 17 00:00:00 2001 From: Kevin Li Date: Wed, 11 Mar 2026 16:35:16 -0700 Subject: [PATCH] Fix chi task56 combined.patch: Allow header duplication bug The combined.patch for go-chi task56 has two bugs that cause TestMethodNotAllowed and TestSetAllowHeader to fail: 1. The SetAllowHeader middleware reuses the same routing context (rctx) for all Match() calls in the method-checking loop. Each call to Match() triggers findRoute(), which appends to rctx.methodsAllowed without resetting. This causes the Allow header to contain duplicated method values (e.g., "GET, HEAD" repeated 9+ times instead of appearing once). Fix: Use chi.NewRouteContext() for each Match() call to prevent cross-call state pollution. 2. The core MethodNotAllowedHandler builds a comma-joined string and uses w.Header().Set("Allow", joinedStr), creating a single header value "GET, HEAD". But the tests expect separate header values via Header.Values("Allow") (["GET", "HEAD"]). Fix: Use w.Header().Add("Allow", name) per method to emit individual header values matching the test expectations. --- dataset/go_chi_task/task56/combined.patch | 36 ++++++++--------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/dataset/go_chi_task/task56/combined.patch b/dataset/go_chi_task/task56/combined.patch index c8fbe31..a46f6b0 100644 --- a/dataset/go_chi_task/task56/combined.patch +++ b/dataset/go_chi_task/task56/combined.patch @@ -156,13 +156,13 @@ index 0000000..da8e92a + rctx := chi.RouteContext(r.Context()) + url := r.URL.Path + -+ if rctx.Routes.Match(rctx, r.Method, url) { ++ if rctx.Routes.Match(chi.NewRouteContext(), r.Method, url) { + next.ServeHTTP(w, r) + return + } + + for _, method := range []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "CONNECT", "TRACE"} { -+ if rctx.Routes.Match(rctx, method, url) { ++ if rctx.Routes.Match(chi.NewRouteContext(), method, url) { + w.Header().Add("Allow", method) + } + } @@ -302,7 +302,7 @@ index 0d1caa6..16317d6 100644 // With adds inline middlewares for an endpoint handler. func (mx *Mux) With(middlewares ...func(http.Handler) http.Handler) Router { // Similarly as in handle(), we must build the mux handler once additional -@@ -377,12 +418,41 @@ func (mx *Mux) NotFoundHandler() http.HandlerFunc { +@@ -377,12 +418,31 @@ func (mx *Mux) NotFoundHandler() http.HandlerFunc { } // MethodNotAllowedHandler returns the default Mux 405 responder whenever @@ -311,26 +311,14 @@ index 0d1caa6..16317d6 100644 +// a method cannot be resolved for a route. Optionally accepts allowed methods +// to set the Allow header. +func (mx *Mux) MethodNotAllowedHandler(allowedMethods ...methodTyp) http.HandlerFunc { -+ // Build allowed methods string -+ var allowedMethodsStr string -+ if len(allowedMethods) > 0 { -+ methods := []string{} -+ for _, m := range allowedMethods { -+ if name := GetMethodStringFromType(m); name != "" { -+ methods = append(methods, name) -+ } -+ } -+ if len(methods) > 0 { -+ allowedMethodsStr = strings.Join(methods, ", ") -+ } -+ } -+ + // If custom handler is set, wrap it to also set Allow header if mx.methodNotAllowedHandler != nil { - return mx.methodNotAllowedHandler + return func(w http.ResponseWriter, r *http.Request) { -+ if allowedMethodsStr != "" { -+ w.Header().Set("Allow", allowedMethodsStr) ++ for _, m := range allowedMethods { ++ if name := GetMethodStringFromType(m); name != "" { ++ w.Header().Add("Allow", name) ++ } + } + mx.methodNotAllowedHandler(w, r) + } @@ -338,8 +326,10 @@ index 0d1caa6..16317d6 100644 + + // Default handler with Allow header + return func(w http.ResponseWriter, r *http.Request) { -+ if allowedMethodsStr != "" { -+ w.Header().Set("Allow", allowedMethodsStr) ++ for _, m := range allowedMethods { ++ if name := GetMethodStringFromType(m); name != "" { ++ w.Header().Add("Allow", name) ++ } + } + w.WriteHeader(405) + w.Write(nil) @@ -348,7 +338,7 @@ index 0d1caa6..16317d6 100644 } // handle registers a http.Handler in the routing tree for a particular http method -@@ -435,17 +505,37 @@ func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) { +@@ -435,17 +495,37 @@ func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) { } method, ok := methodMap[rctx.RouteMethod] if !ok { @@ -388,7 +378,7 @@ index 0d1caa6..16317d6 100644 } else { mx.NotFoundHandler().ServeHTTP(w, r) } -@@ -485,3 +575,39 @@ func methodNotAllowedHandler(w http.ResponseWriter, r *http.Request) { +@@ -485,3 +565,39 @@ func methodNotAllowedHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(405) w.Write(nil) }