Extract BaseResponseWriter to httputil to eliminate duplicate status-capture code#6106
Merged
Merged
Conversation
5 tasks
Copilot
AI
changed the title
[WIP] Refactor duplicate HTTP response writer status-code wrappers
Extract BaseResponseWriter to httputil to eliminate duplicate status-capture code
May 20, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR centralizes HTTP status-code capture logic by introducing a shared httputil.BaseResponseWriter and reusing it from both tracing and server response-writer wrappers, eliminating duplicated (and previously diverged) implementations.
Changes:
- Added
internal/httputil/BaseResponseWriterto capture status codes (including implicit 200) and supporthttp.ResponseControllerviaUnwrap. - Refactored tracing’s
statusResponseWriterto embed the shared base wrapper and updated call sites/tests accordingly. - Updated the server
responseWriterto embed the shared base wrapper while retaining body capture + logging.
Show a summary per file
| File | Description |
|---|---|
| internal/httputil/response_writer.go | Introduces shared BaseResponseWriter implementation for status capture + Unwrap. |
| internal/httputil/response_writer_test.go | Adds unit tests for the new base response-writer behavior. |
| internal/tracing/http.go | Refactors tracing response-writer wrapper to embed the shared base type. |
| internal/tracing/http_test.go | Updates tests to validate status capture via the promoted StatusCode field. |
| internal/server/response_writer.go | Updates server response-writer to embed BaseResponseWriter and delegate status logic while keeping body buffering/logging. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 5/5 changed files
- Comments generated: 2
Comment on lines
+14
to
+29
| // StatusCode holds the captured HTTP status code. It is set by WriteHeader | ||
| // and, if still zero when Write is first called, defaults to http.StatusOK. | ||
| StatusCode int | ||
| } | ||
|
|
||
| // WriteHeader captures the status code and forwards it to the underlying writer. | ||
| func (w *BaseResponseWriter) WriteHeader(code int) { | ||
| w.StatusCode = code | ||
| w.ResponseWriter.WriteHeader(code) | ||
| } | ||
|
|
||
| // Write captures an implicit 200 status when called without a prior WriteHeader, | ||
| // then delegates to the underlying writer. | ||
| func (w *BaseResponseWriter) Write(b []byte) (int, error) { | ||
| if w.StatusCode == 0 { | ||
| w.StatusCode = http.StatusOK |
Comment on lines
+25
to
+31
| // Write captures an implicit 200 status when called without a prior WriteHeader, | ||
| // then delegates to the underlying writer. | ||
| func (w *BaseResponseWriter) Write(b []byte) (int, error) { | ||
| if w.StatusCode == 0 { | ||
| w.StatusCode = http.StatusOK | ||
| } | ||
| return w.ResponseWriter.Write(b) |
Collaborator
|
@copilot address review feedback |
Contributor
Author
Fixed in the latest commit.
Two new tests cover both scenarios ( |
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.
internal/server/response_writer.goandinternal/tracing/http.goeach independently implemented anhttp.ResponseWriterwrapper with identicalWriteHeader/Write/Unwrapstatus-capture logic — and had already diverged (implicit-200 handling present in tracing but absent in server).Changes
internal/httputil/response_writer.go— newBaseResponseWriterembeddinghttp.ResponseWriterwith:WriteHeader: captures status code and forwardsWrite: captures implicit 200 on first write if no priorWriteHeaderUnwrap: exposes underlying writer forhttp.ResponseControllerinterface delegation (http.Flusher, etc.)internal/tracing/http.go—statusResponseWriternow just embedshttputil.BaseResponseWriter; its ownWriteHeader,Write, andUnwrapimplementations removed (all promoted from base)internal/server/response_writer.go—responseWriterembedshttputil.BaseResponseWriterinstead of rawhttp.ResponseWriter; retains its ownWriteHeader/Writeonly for logging and body-capture, delegating the status logic to the base typeinternal/httputil/response_writer_test.go— full test coverage forBaseResponseWriterinternal/tracing/http_test.go— updated to use promotedStatusCodefield