Conversation
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>
There was a problem hiding this comment.
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
| { | ||
| name: "POST with nil body returns nil", | ||
| buildReq: func() *http.Request { | ||
| return httptest.NewRequest(http.MethodPost, "/", nil) | ||
| }, | ||
| wantBytes: nil, | ||
| wantErr: nil, | ||
| }, |
There was a problem hiding this comment.
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 apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/github/gh-aw-mcpg/sessions/1140d42c-8505-436d-a213-3ebccb1381ab Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Fixed in 9f29e6a. The "POST with nil body" test case now explicitly sets |
ThreatLenses — AI PR reviewTask (understood) Summary |
Test Coverage Improvement
Functions Analyzed
peekRequestBodyinternal/serverhttp_helpers.goisTransientHTTPErrorinternal/configvalidation_schema.goBoth functions were exercised only indirectly through higher-level tests —
peekRequestBodyvialogHTTPRequestBodytests andisTransientHTTPErrorviafetchAndFixSchemaretry tests — but neither had any direct unit tests.Why These Functions?
peekRequestBodywas 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:nilbody orhttp.NoBody→ fast returnhttp.NoBodyisTransientHTTPErrorwas 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.gonil, nilnilbody returnsnil, nilhttp.NoBodyreturnsnil, nil[]byte{}and sets body tohttp.NoBodyio.ReadAllgets full body after peekreq.Bodyis set tohttp.NoBodyexactlyinternal/config/transient_http_error_test.go429 Too Many Requests→true503 Service Unavailable→truetruefalsefalse, 600 →falseImplementation Notes
t.Parallel()per sub-testio.ReadCloserhelpers (errorOnReadReader,errorOnCloseReader) are scoped to the server test file for error injection without external dependenciesGenerated by Test Coverage Improver