Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
4 tasks
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
CreateTestContextdoes not flush the response at the end of a handler, unlikeEngine.ServeHTTP. This caused confusion (issue #3443) whenctx.Status(http.StatusCreated)was not observable in thehttptest.ResponseRecorderwithout callingctx.Writer.WriteHeaderNow().Root cause
In a real server,
http.Serverflushes the response after the handler returns.CreateTestContextcalls the handler directly without thehttp.Serverinfrastructure, so the status code set byContext.Statusremains buffered in the ginresponseWriterand is never written to the underlyinghttp.ResponseWriter.Changes
CreateTestContextandCreateTestContextOnlydoc comments, explaining thatc.Writer.WriteHeaderNow()must be called to observe the status code in theResponseWriterafter a handler that only callsContext.Status.TestCreateTestContextStatusRequiresFlush— a regression test that verifies:c.Status(201)sets the buffered status (c.Writer.Status() == 201)httptest.ResponseRecorderstill shows200(not yet flushed)c.Writer.WriteHeaderNow(), the recorder observes201No behavior change — documentation and test only.
Fixes #3443