[test-improver] Improve tests for proxy handler#8251
Merged
lpcox merged 2 commits intoJun 29, 2026
Conversation
…Passthrough, and rate-limit paths - Add TestForwardAndReadBody_BodyReadError: exercises io.ReadAll error path (lines 432-435 of handler.go) using a custom http.RoundTripper that returns a valid response whose body pipe is closed with an error. forwardAndReadBody now reaches 100% coverage. - Add TestHandleUnrecognizedPassthrough_UpstreamFails: exercises the early resp == nil return (lines 147-149 of handler.go) by pointing the proxy at an unreachable upstream for an unrecognized URL. handleUnrecognizedPassthrough now reaches 100% coverage. - Add TestHandleWithDIFC_RateLimitDetected: exercises the rate-limit span annotation block in handleWithDIFC (lines 230-237 of handler.go) via an upstream 429 with X-Ratelimit-Reset header. handleWithDIFC coverage improves from 86% to 95.1%. - Add bodyErrorTransport type (RoundTripper that pipes a read error) to support the BodyReadError test without any external setup or hijacking. Overall proxy package coverage: 91.4% → 92.2% Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves unit test coverage for internal/proxy/handler.go by adding targeted tests that exercise previously-uncovered error and rate-limit branches in the proxy handler implementation.
Changes:
- Added a
bodyErrorTransporthttp.RoundTripperto simulate an upstream response whose body fails duringio.ReadAll. - Added a test for
forwardAndReadBody’s body-read error path and a test for DIFC rate-limit detection behavior. - Added a test intended to cover the
resp == nilearly return inhandleUnrecognizedPassthroughwhen upstream forwarding fails.
Show a summary per file
| File | Description |
|---|---|
| internal/proxy/handler_test.go | Adds helper transport and new tests to cover additional error/rate-limit branches in the proxy handler. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Low
Collaborator
|
@copilot address review feedback |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Contributor
Done. The test now creates a real |
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.
Test Improvements:
handler_test.goFile Analyzed
internal/proxy/handler_test.gointernal/proxyImprovements Made
1. Increased Coverage
Three previously uncovered code paths in
handler.goare now exercised:handleUnrecognizedPassthroughforwardAndReadBodyhandleWithDIFC2. New Tests Added
TestForwardAndReadBody_BodyReadErrorhandler.go: theio.ReadAllerror path insideforwardAndReadBodybodyErrorTransport(customhttp.RoundTripper) that returns a valid HTTP response whose body is anio.Pipeimmediately closed with an error502 bad_gateway/"failed to read upstream response"JSON errorTestHandleUnrecognizedPassthrough_UpstreamFailshandler.go: theif resp == nil { return }early exit inhandleUnrecognizedPassthrough(127.0.0.1/redacted) (unreachable) with an unrecognized path/v1/unknown/endpoint`502error and returns without panicking on nil responseTestHandleWithDIFC_RateLimitDetectedhandler.go: the rate-limit span annotation block insidehandleWithDIFC429 Too Many RequestswithX-Ratelimit-Remaining: 0and a validX-Ratelimit-ResetUnix timestamprateLimitedbranch and the inner!resetAt.IsZero()branch429is passed through andRetry-Afteris injected bywriteResponse3. Helper Type Added
bodyErrorTransport— a minimalhttp.RoundTripperthat synthesises a response with a failing body reader (viaio.Pipe). Clean, no network I/O required, no TCP hijacking.Test Execution
All 160+ tests across all packages pass.
Why These Changes?
handler.gohad three distinct code paths that were entirely untested:forwardAndReadBody— requires a transport-level mock sincehttptest.Serverbodies never failhandleUnrecognizedPassthrough— requires an unreachable upstream for a non-DIFC pathhandleWithDIFC— requires a429response through the full DIFC pipelineAll three paths are now covered, and the tests are self-contained, fast, and deterministic.
Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
index.crates.ioSee Network Configuration for more information.