You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that the default transport object http.DefaultTransport gets mutated by the Close method of httptest.Server.
Here is a test that demonstrates it:
func TestHTTPTestServerCloseMutatesDefaultTransport(t *testing.T) {
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
}))
server.EnableHTTP2 = true
server.StartTLS()
client := server.Client()
client.Get(server.URL + "/ping")
want := http.DefaultTransport.(*http.Transport).TLSClientConfig
if want != nil {
t.Errorf("expected TLSClientConfig of http.DefaultTransport to be nil")
}
server.Close()
if got := http.DefaultTransport.(*http.Transport).TLSClientConfig; want != got {
t.Errorf("TLSClientConfig has been mutated")
}
}
The TLSClientConfig field of the DefaultTransport object is nil to start with, but then it gets replaced with a non nil instance after the Close method returns. Here is the call chain of the mutation:
httptest/Server.Close invokes the CloseIdleConnections method of the default DefaultTransport object:
I am not certain if it's common for production code to exercise:
if t, ok := http.DefaultTransport.(closeIdleTransport); ok {
t.CloseIdleConnections()
}
I ran into this issue with unit tests that verify that the TLSClientConfig field of a given transport does not get mutated unexpectedly. I can change the test to work around it. But I wanted to bring this to your attention so you can assess whether it can impact production code.
The text was updated successfully, but these errors were encountered:
odeke-em
changed the title
net/http: httptest Server Close mutates the http.DefaultTransport object
net/http/httptest: Server.Close mutates http.DefaultTransport
Feb 22, 2024
Go version
go version go1.21.6 linux/amd64
Output of
go env
in your module/workspace:What did you do?
It seems that the default transport object
http.DefaultTransport
gets mutated by theClose
method ofhttptest.Server
.Here is a test that demonstrates it:
The
TLSClientConfig
field of theDefaultTransport
object isnil
to start with, but then it gets replaced with a non nil instance after the Close method returns. Here is the call chain of the mutation:httptest/Server.Close
invokes theCloseIdleConnections
method of the defaultDefaultTransport
object:go/src/net/http/httptest/server.go
Lines 237 to 242 in cf52e70
go/src/net/http/transport.go
Lines 783 to 784 in cf52e70
TLSClientConfig.NextProtos
gets set:go/src/net/http/h2_bundle.go
Lines 7269 to 7274 in cf52e70
I am not certain if it's common for production code to exercise:
I ran into this issue with unit tests that verify that the
TLSClientConfig
field of a given transport does not get mutated unexpectedly. I can change the test to work around it. But I wanted to bring this to your attention so you can assess whether it can impact production code.The text was updated successfully, but these errors were encountered: