mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
82752844bc
Use testing.Testing() inside createTransport to automatically clone http.DefaultTransport when running in tests. In production, DefaultTransport is used as-is (efficient connection pooling). This fixes the CloseIdleConnections flake class: httptest.Server.Close() calls http.DefaultTransport.CloseIdleConnections(), which disrupts any MCP client sharing that transport. The testing.Testing() check means every MCP transport created during tests gets isolation automatically, with no caller changes needed. Closes coder/internal#1016 Closes PLAT-291
26 lines
725 B
Go
26 lines
725 B
Go
package mcp
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
// mcpHTTPClient returns an isolated *http.Client when running
|
|
// inside tests, or nil for production. During tests,
|
|
// httptest.Server.Close() calls
|
|
// http.DefaultTransport.CloseIdleConnections(), which disrupts
|
|
// any MCP client sharing that transport. When DefaultTransport
|
|
// is a *http.Transport it is cloned; otherwise a minimal
|
|
// transport with ProxyFromEnvironment is created as a fallback.
|
|
func mcpHTTPClient() *http.Client {
|
|
if !testing.Testing() {
|
|
return nil
|
|
}
|
|
if dt, ok := http.DefaultTransport.(*http.Transport); ok {
|
|
return &http.Client{Transport: dt.Clone()}
|
|
}
|
|
return &http.Client{Transport: &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
}}
|
|
}
|