Skip to content

feat(test): add RunTestHandler to flush status in test contexts#4570

Open
abhiyankhanal wants to merge 2 commits intogin-gonic:masterfrom
abhiyankhanal:fix/test-context-status
Open

feat(test): add RunTestHandler to flush status in test contexts#4570
abhiyankhanal wants to merge 2 commits intogin-gonic:masterfrom
abhiyankhanal:fix/test-context-status

Conversation

@abhiyankhanal
Copy link
Contributor

Summary

  • Adds RunTestHandler(w, req, handlers...) helper function to test_helpers.go
  • Solves the common issue where ctx.Status() doesn't flush the status code to httptest.ResponseRecorder.Code when using CreateTestContext directly
  • CreateTestContext behavior is unchanged (backward compatible)

Related Issue

Closes #3443

Changes Made

  • test_helpers.go: Added RunTestHandler function that creates a test context, runs handlers, and calls WriteHeaderNow() to flush the status
  • test_helpers_test.go: Added 5 test cases covering status flushing, middleware chains, default status, request setting, and backward compatibility

Testing

  • go test -run "TestRunTestHandler|TestCreateTestContextBackward" -v -count=1 — all tests pass

Example Usage

// Before (broken - w.Code stays 200):
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = req
MyHandler(ctx)
assert.Equal(t, 201, w.Code) // FAILS

// After (works):
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/", nil)
ctx := gin.RunTestHandler(w, req, MyHandler)
assert.Equal(t, 201, w.Code) // PASSES

🤖 Generated with Claude Code

@codecov
Copy link

codecov bot commented Mar 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.31%. Comparing base (3dc1cd6) to head (3f607b8).
⚠️ Report is 268 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4570      +/-   ##
==========================================
- Coverage   99.21%   98.31%   -0.90%     
==========================================
  Files          42       48       +6     
  Lines        3182     3148      -34     
==========================================
- Hits         3157     3095      -62     
- Misses         17       44      +27     
- Partials        8        9       +1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.30% <100.00%> (?)
-tags go_json 98.23% <100.00%> (?)
-tags nomsgpack 98.29% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 98.31% <100.00%> (?)
go-1.26 98.31% <100.00%> (?)
macos-latest 98.31% <100.00%> (-0.90%) ⬇️
ubuntu-latest 98.31% <100.00%> (-0.90%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

abhiyankhanal and others added 2 commits March 8, 2026 19:09
When using CreateTestContext directly to test handlers, ctx.Status()
sets the status internally but it is not flushed to the underlying
httptest.ResponseRecorder. RunTestHandler creates a test context, runs
the handler chain, and calls WriteHeaderNow() so that the status code
is properly reflected in the ResponseWriter.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace string literals with http.Method* constants to satisfy
the usestdlibvars linter.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@abhiyankhanal abhiyankhanal force-pushed the fix/test-context-status branch from 29918cd to 3f607b8 Compare March 8, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Status() not setting up status in response for unit test

1 participant