Skip to content

[test-improver] Improve tests for httputil package#3693

Open
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/httputil-marshal-error-coverage-6d302a2b64de1851
Open

[test-improver] Improve tests for httputil package#3693
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/httputil-marshal-error-coverage-6d302a2b64de1851

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

File Analyzed

  • Test File: internal/httputil/httputil_test.go
  • Package: internal/httputil
  • Lines of Code: 140 → 153

Improvements Made

1. Increased Coverage

  • ✅ Added test for the previously-uncovered json.Marshal error path in WriteJSONResponse

The WriteJSONResponse function commits response headers before attempting to marshal the body:

func WriteJSONResponse(w http.ResponseWriter, statusCode int, body interface{}) {
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(statusCode)          // ← headers committed here
    data, err := json.Marshal(body)
    if err != nil {
        return                         // ← this branch was untested
    }
    w.Write(data)
}
```

The new test passes a `chan int` (which `json.Marshal` cannot encode) to exercise the `return` branch and documents the observable side-effects: `Content-Type` and status code are already written, but the response body is empty.

### 2. Documents Important Behavior

The test makes explicit a subtle contract: callers should not rely on a body being present when passing exotic types, even though the status code will always be written. This is useful context for future maintainers.

## Test Output

All existing tests continue to pass; the new subtest name is:

```
--- PASS: TestWriteJSONResponse/unmarshalable_body_writes_headers_but_no_body

Why These Changes?

httputil is a shared utility used across the server and proxy packages. Its only function had one branch (if err != nil { return }) that was never exercised. A chan int is the canonical Go type that is always rejected by json.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

Generated by Test Improver · ● 3.4M ·

…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>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Marshal error path in WriteJSONResponse.
  • 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant