Commit 1c1ac57
authored
[test-improver] Improve tests for proxy handler (#8251)
# Test Improvements: `handler_test.go`
## File Analyzed
- **Test File**: `internal/proxy/handler_test.go`
- **Package**: `internal/proxy`
- **Lines Added**: +80 lines (3 new tests + 1 helper type)
## Improvements Made
### 1. Increased Coverage
Three previously uncovered code paths in `handler.go` are now exercised:
| Function | Before | After |
|---|---|---|
| `handleUnrecognizedPassthrough` | 0% | **100%** |
| `forwardAndReadBody` | partial | **100%** |
| `handleWithDIFC` | ~86% | **95.1%** |
| **Package total** | **91.4%** | **92.2%** |
### 2. New Tests Added
#### `TestForwardAndReadBody_BodyReadError`
- Covers lines 432–435 of `handler.go`: the `io.ReadAll` error path
inside `forwardAndReadBody`
- Uses a new `bodyErrorTransport` (custom `http.RoundTripper`) that
returns a valid HTTP response whose body is an `io.Pipe` immediately
closed with an error
- Verifies the handler writes a `502 bad_gateway` / `"failed to read
upstream response"` JSON error
#### `TestHandleUnrecognizedPassthrough_UpstreamFails`
- Covers lines 147–149 of `handler.go`: the `if resp == nil { return }`
early exit in `handleUnrecognizedPassthrough`
- Points the proxy at `(127.0.0.1/redacted) (unreachable) with an
unrecognized path `/v1/unknown/endpoint`
- Verifies the handler writes a `502` error and returns without
panicking on nil response
#### `TestHandleWithDIFC_RateLimitDetected`
- Covers lines 230–237 of `handler.go`: the rate-limit span annotation
block inside `handleWithDIFC`
- Upstream returns `429 Too Many Requests` with `X-Ratelimit-Remaining:
0` and a valid `X-Ratelimit-Reset` Unix timestamp
- Exercises both the outer `rateLimited` branch and the inner
`!resetAt.IsZero()` branch
- Verifies the `429` is passed through and `Retry-After` is injected by
`writeResponse`
### 3. Helper Type Added
`bodyErrorTransport` — a minimal `http.RoundTripper` that synthesises a
response with a failing body reader (via `io.Pipe`). Clean, no network
I/O required, no TCP hijacking.
## Test Execution
```
ok github.com/github/gh-aw-mcpg/internal/proxy 0.088s coverage: 92.5% of statements
```
All 160+ tests across all packages pass.
## Why These Changes?
`handler.go` had three distinct code paths that were entirely untested:
1. The body-read failure in `forwardAndReadBody` — requires a
transport-level mock since `httptest.Server` bodies never fail
2. The nil-response early return in `handleUnrecognizedPassthrough` —
requires an unreachable upstream for a non-DIFC path
3. The rate-limit tracing annotations in `handleWithDIFC` — requires a
`429` response through the full DIFC pipeline
All 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]
> <details>
> <summary>Firewall blocked 1 domain</summary>
>
> The following domain was blocked by the firewall during workflow
execution:
>
> - `index.crates.io`
>> To allow these domains, add them to the `network.allowed` list in
your workflow frontmatter:
>
> ```yaml
> network:
> allowed:
> - defaults
> - "index.crates.io"
> ```
>
> See [Network
Configuration](https://github.github.com/gh-aw/reference/network/) for
more information.
>
> </details>
> Generated by [Test
Improver](https://github.com/github/gh-aw-mcpg/actions/runs/28340521640)
· 2K AIC · ⊞ 29.6K ·
[◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-improver%22&type=pullrequests)
<!-- gh-aw-agentic-workflow: Test Improver, engine: copilot, version:
1.0.60, model: claude-sonnet-4.6, id: 28340521640, workflow_id:
test-improver, run:
https://github.com/github/gh-aw-mcpg/actions/runs/28340521640 -->
<!-- gh-aw-workflow-id: test-improver -->
<!-- gh-aw-workflow-call-id: github/gh-aw-mcpg/test-improver -->1 file changed
Lines changed: 83 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
23 | 38 | | |
24 | 39 | | |
25 | 40 | | |
| |||
745 | 760 | | |
746 | 761 | | |
747 | 762 | | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
0 commit comments