Fix chi task56 combined.patch: Allow header duplication bug#38
Merged
akhatua2 merged 1 commit intocooperbench:mainfrom Mar 12, 2026
Merged
Conversation
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.
Collaborator
|
Thanks Kevin! This looks great to me. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
combined.patchforgo_chi_task/task56has two bugs that causeTestMethodNotAllowedandTestSetAllowHeaderto always fail, making the oracle solution score 0 for any feature pair involving feature 1.Bug 1: Middleware reuses routing context across
Match()callsThe
SetAllowHeadermiddleware callsrctx.Routes.Match(rctx, method, url)in a loop for each HTTP method, reusing the samerctx. EachMatch()call triggersfindRoute(), which appends torctx.methodsAllowedwithout resetting. After 9 iterations,methodsAllowedcontains many duplicate[GET, HEAD]entries, causing the Allow header to contain values like:```
GET, HEAD, GET, HEAD, GET, HEAD, HEAD, GET, GET, HEAD, GET, HEAD, GET, HEAD, HEAD, GET, HEAD, GET
```
Fix: Use
chi.NewRouteContext()for eachMatch()call to prevent cross-call state pollution.Bug 2: Core handler uses
Set()instead ofAdd()for Allow headerThe
MethodNotAllowedHandlerbuilds a comma-joined string and callsw.Header().Set("Allow", "GET, HEAD"), creating a single header value. The tests expect separate values viaHeader.Values("Allow")— i.e.,["GET", "HEAD"](2 elements) not["GET, HEAD"](1 element).Fix: Use
w.Header().Add("Allow", name)per method to emit individual header values matching the test expectations and RFC conventions.Verification
Built the chi:task56 Docker image locally from the Dockerfile and ran all tests.
Buggy (original) combined.patch — feature 1 tests fail:
```
--- FAIL: TestMethodNotAllowed/Unregistered_Method (0.00s)
mux_test.go:426: Allow header should contain 2 headers: GET, HEAD. Received: [GET, HEAD]
--- FAIL: TestSetAllowHeader/Unregistered_Method (0.00s)
allowed_methods_test.go:58: GET, HEAD, GET, HEAD, GET, HEAD, GET, HEAD, GET, HEAD, HEAD, GET, GET, HEAD, GET, HEAD, GET, HEAD
```
Fixed combined.patch — all feature tests pass:
```
Feature 1: ok github.com/go-chi/chi/v5, ok github.com/go-chi/chi/v5/middleware
Feature 2: ok github.com/go-chi/chi/v5, ok github.com/go-chi/chi/v5/middleware
Feature 3: ok github.com/go-chi/chi/v5, ok github.com/go-chi/chi/v5/middleware
Feature 4: ok github.com/go-chi/chi/v5, ok github.com/go-chi/chi/v5/middleware
Feature 5: ok github.com/go-chi/chi/v5, ok github.com/go-chi/chi/v5/middleware
```
Test plan