Skip to content

[test] Add tests for server.peekRequestBody and config.isTransientHTTPError#3317

Merged
lpcox merged 2 commits intomainfrom
test-coverage-peek-request-body-and-transient-http-error-066956e7f4cea185
Apr 7, 2026
Merged

[test] Add tests for server.peekRequestBody and config.isTransientHTTPError#3317
lpcox merged 2 commits intomainfrom
test-coverage-peek-request-body-and-transient-http-error-066956e7f4cea185

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 7, 2026

Test Coverage Improvement

Functions Analyzed

Function Package File Previous Coverage
peekRequestBody internal/server http_helpers.go 0% direct coverage
isTransientHTTPError internal/config validation_schema.go 0% direct coverage

Both functions were exercised only indirectly through higher-level tests — peekRequestBody via logHTTPRequestBody tests and isTransientHTTPError via fetchAndFixSchema retry tests — but neither had any direct unit tests.

Why These Functions?

peekRequestBody was selected as the primary target. Despite being a critical helper used by every incoming HTTP request handler, it had no direct unit tests. The function restores the request body after reading it (so downstream handlers can re-read it), which is a correctness property that deserves explicit verification. Its branches span:

  • Non-POST method → fast return without touching body
  • nil body or http.NoBody → fast return
  • Read error during body consumption → propagated error
  • Close error after successful read → propagated error
  • Empty body → replaced with http.NoBody
  • Non-empty body → bytes returned and body reset for re-reading

isTransientHTTPError was added as a complementary target: a small but completely untested function where all 5xx / 429 / 503 branches needed explicit coverage including boundary values (499, 599, 600).

Tests Added

internal/server/peek_request_body_test.go

  • ✅ Non-POST methods (GET, PUT, DELETE) return nil, nil
  • ✅ POST with nil body returns nil, nil
  • ✅ POST with http.NoBody returns nil, nil
  • ✅ POST with non-empty body returns bytes and restores the body for re-reading
  • ✅ POST with binary (non-text) body — bytes preserved exactly
  • ✅ POST with empty body reader returns []byte{} and sets body to http.NoBody
  • ✅ POST with read error propagates the error
  • ✅ POST with close error propagates the error
  • ✅ Body-restoration contract test: downstream io.ReadAll gets full body after peek
  • ✅ Empty-body contract test: req.Body is set to http.NoBody exactly

internal/config/transient_http_error_test.go

  • 429 Too Many Requeststrue
  • 503 Service Unavailabletrue
  • ✅ Full 5xx range (500, 501, 502, 504, 599) → true
  • ✅ 2xx, 3xx, 4xx (non-429) → false
  • ✅ Boundary values: 499 → false, 600 → false

Implementation Notes

  • Both test files use table-driven tests with t.Parallel() per sub-test
  • Custom io.ReadCloser helpers (errorOnReadReader, errorOnCloseReader) are scoped to the server test file for error injection without external dependencies
  • No production code was changed

Generated by Test Coverage Improver

Generated by Test Coverage Improver · ● 13.1M ·

peekRequestBody (internal/server/http_helpers.go) had no direct unit
tests despite having five distinct code paths: non-POST methods, nil
body, http.NoBody, read errors, close errors, empty body (replaced with
http.NoBody), and non-empty body with body-restoration. The new tests
cover every branch, including a dedicated test that confirms downstream
handlers can re-read the body after peekRequestBody returns.

isTransientHTTPError (internal/config/validation_schema.go) had no
direct unit tests. The function is exercised indirectly through
fetchAndFixSchema retry tests but never tested in isolation. The new
table-driven tests cover the named constants (429, 503), the full 5xx
range, and boundary/false cases (4xx, 2xx, 3xx, 600).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 7, 2026 18:34
Copilot AI review requested due to automatic review settings April 7, 2026 18:34
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

Adds direct unit tests to improve coverage for two previously indirectly-tested helpers: server.peekRequestBody and config.isTransientHTTPError.

Changes:

  • Added table-driven unit tests for peekRequestBody, including body restoration and error paths.
  • Added table-driven unit tests for isTransientHTTPError, covering 429/503/5xx and boundary values.
Show a summary per file
File Description
internal/server/peek_request_body_test.go New unit tests covering peekRequestBody branches and body-restoration contract.
internal/config/transient_http_error_test.go New unit tests covering isTransientHTTPError status-code classification.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Comment on lines +73 to +80
{
name: "POST with nil body returns nil",
buildReq: func() *http.Request {
return httptest.NewRequest(http.MethodPost, "/", nil)
},
wantBytes: nil,
wantErr: nil,
},
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

The "POST with nil body" case doesn’t actually exercise the r.Body == nil branch: httptest.NewRequest(..., nil) sets req.Body to http.NoBody (not nil), so this is effectively duplicating the explicit http.NoBody test. To cover the intended branch, explicitly set req.Body = nil after constructing the request (or rename/remove this case).

Copilot uses AI. Check for mistakes.
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Apr 7, 2026

@copilot apply changes based on the comments in this thread

@github github deleted a comment from rdevps Apr 7, 2026
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 7, 2026

@copilot apply changes based on the comments in this thread

Fixed in 9f29e6a. The "POST with nil body" test case now explicitly sets req.Body = nil after creating the request, so it correctly exercises the r.Body == nil branch rather than duplicating the http.NoBody case.

Copilot finished work on behalf of lpcox April 7, 2026 18:43
Copilot AI requested a review from lpcox April 7, 2026 18:43
@lpcox lpcox merged commit 66458f2 into main Apr 7, 2026
12 checks passed
@lpcox lpcox deleted the test-coverage-peek-request-body-and-transient-http-error-066956e7f4cea185 branch April 7, 2026 18:45
@rdevps
Copy link
Copy Markdown

rdevps commented Apr 7, 2026

ThreatLenses — AI PR review

Task (understood)
Add unit tests for the functions server.peekRequestBody and config.isTransientHTTPError to improve test coverage.

Summary
The pull request successfully adds comprehensive unit tests for both the peekRequestBody and isTransientHTTPError functions, addressing previously uncovered branches. The tests are well-structured using table-driven tests and include edge cases, which is commendable. However, the review is based on a large diff, and while the tests appear thorough, it's essential to ensure that they are executed in the CI pipeline to validate their effectiveness. Overall, this is a positive contribution to the codebase.

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.

4 participants