mirror of
https://github.com/coder/coder.git
synced 2026-09-22 21:22:17 +08:00
In our codebase we have an existing convention of using
`flag.Lookup("test.v")` instead of `testing.Testing()`. This avoids
pulling in the entire `testing` package. Another consequence: some of
our custom linters trigger upon import of the `testing` package which
can lead to unexpected linter errors.
26 lines
738 B
Go
26 lines
738 B
Go
package mcpclient
|
|
|
|
import (
|
|
"flag"
|
|
"net/http"
|
|
)
|
|
|
|
// 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 flag.Lookup("test.v") == nil {
|
|
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,
|
|
}}
|
|
}
|