[test-improver] Improve tests for httputil package#3693
Open
github-actions[bot] wants to merge 1 commit intomainfrom
Open
[test-improver] Improve tests for httputil package#3693github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
…Response
Add a subtest that passes an unmarshalable type (chan int) to exercise the
previously-uncovered 'if err != nil { return }' branch. The test also
documents the observable behavior: Content-Type and status code headers are
committed before the marshal attempt, so they are present in the response
even when the body cannot be encoded.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Apr 13, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Improves test coverage and documents edge-case behavior for the shared internal/httputil helper that writes JSON HTTP responses.
Changes:
- Adds a new subtest that exercises the
json.Marshalerror path inWriteJSONResponse. - Asserts the observable behavior on marshal failure (headers/status written, empty body).
Show a summary per file
| File | Description |
|---|---|
| internal/httputil/httputil_test.go | Adds coverage for the marshal-error branch and documents the “headers committed before marshal” behavior via assertions. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 1
Comment on lines
+141
to
+147
| t.Run("unmarshalable body writes headers but no body", func(t *testing.T) { | ||
| rec := httptest.NewRecorder() | ||
| // Channels cannot be marshalled to JSON; json.Marshal returns an error. | ||
| WriteJSONResponse(rec, http.StatusInternalServerError, make(chan int)) | ||
|
|
||
| // Content-Type and status code are committed before the marshal attempt, | ||
| // so they are still present even when encoding fails. |
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.
File Analyzed
internal/httputil/httputil_test.gointernal/httputilImprovements Made
1. Increased Coverage
json.Marshalerror path inWriteJSONResponseThe
WriteJSONResponsefunction commits response headers before attempting to marshal the body:Why These Changes?
httputilis a shared utility used across the server and proxy packages. Its only function had one branch (if err != nil { return }) that was never exercised. Achan intis the canonical Go type that is always rejected byjson.Marshal, making this a clean, dependency-free way to cover the error path. The test also acts as documentation of the partial-write semantics.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests