Skip to content

docs(test): document status code flushing behavior in CreateTestContext - #4801

Open
RubenPari wants to merge 2 commits into
gin-gonic:masterfrom
RubenPari:fix/3443-document-test-context-status-flushing
Open

RubenPari wants to merge 2 commits into
gin-gonic:masterfrom
RubenPari:fix/3443-document-test-context-status-flushing

Conversation

@RubenPari

Copy link
Copy Markdown
Contributor

Summary

CreateTestContext does not flush the response at the end of a handler, unlike Engine.ServeHTTP. This caused confusion (issue #3443) when ctx.Status(http.StatusCreated) was not observable in the httptest.ResponseRecorder without calling ctx.Writer.WriteHeaderNow().

Root cause

In a real server, http.Server flushes the response after the handler returns. CreateTestContext calls the handler directly without the http.Server infrastructure, so the status code set by Context.Status remains buffered in the gin responseWriter and is never written to the underlying http.ResponseWriter.

Changes

  • Documented the buffering behavior in CreateTestContext and CreateTestContextOnly doc comments, explaining that c.Writer.WriteHeaderNow() must be called to observe the status code in the ResponseWriter after a handler that only calls Context.Status.
  • Added TestCreateTestContextStatusRequiresFlush — a regression test that verifies:
    • c.Status(201) sets the buffered status (c.Writer.Status() == 201)
    • The httptest.ResponseRecorder still shows 200 (not yet flushed)
    • After c.Writer.WriteHeaderNow(), the recorder observes 201

No behavior change — documentation and test only.

Fixes #3443

CreateTestContext does not flush the response at the end of a handler,
unlike Engine.ServeHTTP. This caused confusion (issue gin-gonic#3443) when
ctx.Status(http.StatusCreated) was not observable in the
httptest.ResponseRecorder without calling ctx.Writer.WriteHeaderNow().

Added documentation to CreateTestContext and CreateTestContextOnly
explaining this behavior, plus a regression test verifying that
Status() buffers the code and WriteHeaderNow() flushes it.

Fixes gin-gonic#3443
@codecov

codecov Bot commented Aug 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.65%. Comparing base (3b08cd7) to head (b045f4c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4801   +/-   ##
=======================================
  Coverage   98.65%   98.65%           
=======================================
  Files          48       48           
  Lines        3190     3190           
=======================================
  Hits         3147     3147           
  Misses         43       43           
Flag Coverage Δ
--ldflags="-checklinkname=0" -tags sonic 98.64% <ø> (ø)
-tags go_json 98.57% <ø> (ø)
-tags nomsgpack 98.63% <ø> (ø)
go-1.26 98.33% <ø> (ø)
go-1.27 98.67% <ø> (ø)
macos-latest 98.65% <ø> (ø)
ubuntu-latest 98.65% <ø> (ø)

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

☔ View full report in Codecov by Harness.
📢 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.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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