Skip to content

[test-improver] Improve tests for mcp HTTP transport - #9667

Merged
lpcox merged 1 commit into
mainfrom
test-improver/mcp-http-transport-coverage-be813712b0077261
Jul 20, 2026
Merged

[test-improver] Improve tests for mcp HTTP transport#9667
lpcox merged 1 commit into
mainfrom
test-improver/mcp-http-transport-coverage-be813712b0077261

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Summary

Adds three targeted tests for previously uncovered error paths in the internal/mcp HTTP transport layer.

File analyzed

internal/mcp/http_transport_test.go

Improvements made

New tests

  • TestExecuteHTTPRequest_ConnectionError — verifies that when the backend server is unreachable, executeHTTPRequest returns an error with the expected "cannot connect to HTTP backend" message. Exercises the isHTTPConnectionError true path.

  • TestExecuteHTTPRequest_BodyReadError — verifies that when the server closes the connection mid-body (hijacked TCP connection that sends headers then drops), executeHTTPRequest returns an error wrapping the io.ReadAll failure.

  • TestSendHTTPRequest_ReconnectSucceedsButRetryFails — verifies that when a session-not-found response triggers a successful reconnect (new session issued), but the subsequent retry request fails because the server closes after the reconnect initialize, the retry error is returned to the caller.

New helper

  • newPlainJSONInitServer — test helper that starts a server handling only the MCP initialize method, used to bootstrap plain JSON connections whose httpURL will be redirected.

Coverage before/after

Function Before After
executeHTTPRequest 90.5% 95.2%
sendHTTPRequest 90.9% 95.5%
internal/mcp total 92.7% 93.0%

Test output

ok  github.com/github/gh-aw-mcpg/internal/mcp  2.644s  coverage: 93.0% of statements

All existing tests continue to pass.

Warning

Firewall blocked 2 domains

The following domains were blocked by the firewall during workflow execution:

  • awmgmcpg
  • index.crates.io

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "awmgmcpg"
    - "index.crates.io"

See Network Configuration for more information.

Generated by Test Improver · 307.8 AIC · ⊞ 5.5K ·

Add three targeted tests for previously uncovered error paths in the mcp
package's HTTP transport layer:

- TestExecuteHTTPRequest_ConnectionError: verifies that when the backend
  server is unreachable, executeHTTPRequest returns an error with the
  expected 'cannot connect to HTTP backend' message (exercises the
  isHTTPConnectionError true path).

- TestExecuteHTTPRequest_BodyReadError: verifies that when the server
  closes the connection mid-body, executeHTTPRequest returns an error
  wrapping the io.ReadAll failure.

- TestSendHTTPRequest_ReconnectSucceedsButRetryFails: verifies that when
  a session-not-found response triggers a successful reconnect but the
  subsequent retry request fails (server closes after reconnect), the
  error from the retry is returned to the caller.

Also adds a newPlainJSONInitServer test helper for bootstrapping plain
JSON connections in tests that need to redirect httpURL independently.

Coverage improvements:
- executeHTTPRequest: 90.5% → 95.2%
- sendHTTPRequest: 90.9% → 95.5%
- internal/mcp package total: 92.7% → 93.0%

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox

lpcox commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Copilot AI commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

@copilot fix this https://github.com/github/gh-aw-mcpg/actions/runs/29709310941/job/88250960630?pr=9667

This failure isn’t coming from the PR changes. The Analyze (rust) job failed during CodeQL init with a GitHub-side service error: Encountered an error while trying to determine feature enablement: HttpError: No server is currently available to service your request. I also ran make test-unit locally on 3cd5ae5, and the unit suite passed, including internal/mcp. This one should be resolved by rerunning the CodeQL job/workflow rather than by a repo change.

Copilot finished work on behalf of lpcox July 20, 2026 13:47
Copilot AI requested a review from lpcox July 20, 2026 13:47
@lpcox

lpcox commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Copilot AI commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

@copilot fix this failing ci https://github.com/github/gh-aw-mcpg/actions/runs/29709310941/job/88250960630?pr=9667

I investigated job 88250960630 on 3cd5ae5. It failed in github/codeql-action/init@v4 before any repo code was analyzed, with Encountered an error while trying to determine feature enablement: HttpError: No server is currently available to service your request. I also re-ran go test ./internal/mcp locally on 3cd5ae5, and it passed. There isn’t a repo-side change to make for this failure; the fix is to rerun the CodeQL job/workflow. If a rerun fails with a different error, I can dig into that one.

Copilot finished work on behalf of lpcox July 20, 2026 15:09
@lpcox
lpcox marked this pull request as ready for review July 20, 2026 17:21
Copilot AI review requested due to automatic review settings July 20, 2026 17:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds targeted coverage for HTTP transport error and reconnect paths.

Changes:

  • Tests connection and truncated-body failures.
  • Tests retry failure after reconnection.
  • Adds a plain JSON initialization helper.
Show a summary per file
File Description
internal/mcp/http_transport_test.go Expands HTTP transport error-path coverage.

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: Medium

case <-closeAfterReconnect:
default:
close(closeAfterReconnect)
go srv.Close()
@lpcox
lpcox merged commit cbeae13 into main Jul 20, 2026
24 of 25 checks passed
@lpcox
lpcox deleted the test-improver/mcp-http-transport-coverage-be813712b0077261 branch July 20, 2026 21:12
lpcox added a commit that referenced this pull request Jul 20, 2026
## Summary

Addresses the review feedback on #9667 (which was merged before the
feedback could be applied).

The Copilot reviewer flagged
`TestSendHTTPRequest_ReconnectSucceedsButRetryFails` in
`internal/mcp/http_transport_test.go` as racy:

> This shutdown is racy: starting `srv.Close()` in a goroutine does not
guarantee that the listener/current keep-alive connection is closed
before `sendHTTPRequest` issues its retry. The retry can therefore reach
the handler, receive another 404, and make `require.Error` fail
intermittently.

## Fix

Following the reviewer's suggestion, the teardown is now deterministic.
On the reconnect (second) `initialize` response the handler:

- Sets `Connection: close` so the client cannot reuse the keep-alive
connection for the retry.
- Synchronously closes `srv.Listener` before returning so the retry
`tools/list` request cannot reconnect.

`srv.Close()` itself can't be called synchronously inside the handler
(it blocks on the in-flight connection), so only the listener is closed
inline and full server cleanup is deferred. The now-unused
`closeAfterReconnect` channel and the `go srv.Close()` goroutine are
removed.

## Verification

- Target test passes 50× (`-count=50`) and 20× under `-race`.
- `make agent-finished` passes (format, build, lint, all Go tests, Rust
guard tests).
- Both SDK canary tests still pass.
pull Bot pushed a commit to Ditto190/gh-aw-mcpg that referenced this pull request Jul 21, 2026
Address review feedback on github#9667. The previous shutdown was racy:
starting srv.Close() in a goroutine did not guarantee the listener or
current keep-alive connection was closed before sendHTTPRequest issued
its retry, so the retry could reach the handler, get another 404, and
make require.Error fail intermittently.

Make the transition deterministic: on the reconnect initialize, set
Connection: close on the response and synchronously close srv.Listener
before the handler returns so the retry cannot reuse or reconnect to the
server. Normal server cleanup is deferred.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 16dda201-99df-4ddf-8139-e4ee11ef18be
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.

3 participants