File tree Expand file tree Collapse file tree 2 files changed +52
-3
lines changed
Expand file tree Collapse file tree 2 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -10,9 +10,17 @@ import (
1010 "net/http"
1111)
1212
13- // DefaultHTTPClient is the default HTTP client used across the driver.
14- var DefaultHTTPClient = & http.Client {
15- Transport : http .DefaultTransport .(* http.Transport ).Clone (),
13+ var DefaultHTTPClient = & http.Client {}
14+
15+ // NewHTTPClient will return the globally-defined DefaultHHTTransport, updating
16+ // the transport if it differs from the http package DefaultTransport.
17+ func NewHTTPClient () * http.Client {
18+ client := DefaultHTTPClient
19+ if _ , ok := http .DefaultTransport .(* http.Transport ); ! ok {
20+ client .Transport = http .DefaultTransport
21+ }
22+
23+ return client
1624}
1725
1826// CloseIdleHTTPConnections closes any connections which were previously
Original file line number Diff line number Diff line change 1+ // Copyright (C) MongoDB, Inc. 2022-present.
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License"); you may
4+ // not use this file except in compliance with the License. You may obtain
5+ // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+ package httputil
8+
9+ import (
10+ "net/http"
11+ "testing"
12+
13+ "go.mongodb.org/mongo-driver/v2/internal/assert"
14+ )
15+
16+ type nonDefaultTransport struct {}
17+
18+ func (* nonDefaultTransport ) RoundTrip (* http.Request ) (* http.Response , error ) { return nil , nil }
19+
20+ func TestDefaultHTTPClientTransport (t * testing.T ) {
21+ t .Run ("default" , func (t * testing.T ) {
22+ client := NewHTTPClient ()
23+
24+ val := assert .ObjectsAreEqual (http .DefaultClient , client )
25+
26+ assert .True (t , val )
27+ assert .Equal (t , DefaultHTTPClient , client )
28+ })
29+
30+ t .Run ("non-default global transport" , func (t * testing.T ) {
31+ http .DefaultTransport = & nonDefaultTransport {}
32+
33+ client := NewHTTPClient ()
34+
35+ val := assert .ObjectsAreEqual (& nonDefaultTransport {}, client .Transport )
36+
37+ assert .True (t , val )
38+ assert .Equal (t , DefaultHTTPClient , client )
39+ assert .NotEqual (t , http .DefaultClient , client ) // Sanity Check
40+ })
41+ }
You can’t perform that action at this time.
0 commit comments