server: fix Transport override (#7834)

This changes makeRequest to update the http client Transport if and only
if testMakeRequestDialContext is set. This is to avoid overriding the
default Transport when testMakeRequestDialContext is nil, which broke
existing behavior, included proxies, timeouts, and other behaviors.

Fixes #7829
Fixes #7788
This commit is contained in:
Blake Mizerany 2024-11-25 15:08:34 -08:00 committed by GitHub
parent 647513a7d4
commit 2b7ed61ca2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1076,17 +1076,15 @@ func makeRequest(ctx context.Context, method string, requestURL *url.URL, header
req.ContentLength = contentLength req.ContentLength = contentLength
} }
resp, err := (&http.Client{ c := &http.Client{
Transport: &http.Transport{
DialContext: testMakeRequestDialContext,
},
CheckRedirect: regOpts.CheckRedirect, CheckRedirect: regOpts.CheckRedirect,
}).Do(req)
if err != nil {
return nil, err
} }
if testMakeRequestDialContext != nil {
return resp, nil tr := http.DefaultTransport.(*http.Transport).Clone()
tr.DialContext = testMakeRequestDialContext
c.Transport = tr
}
return c.Do(req)
} }
func getValue(header, key string) string { func getValue(header, key string) string {