@@ -190,22 +190,32 @@ func TestTransport(t *testing.T) {
190
190
}
191
191
}
192
192
193
- func onSameConn (t * testing.T , modReq func (* http.Request )) bool {
193
+ func testTransportReusesConns (t * testing.T , useClient , wantSame bool , modReq func (* http.Request )) {
194
194
st := newServerTester (t , func (w http.ResponseWriter , r * http.Request ) {
195
195
io .WriteString (w , r .RemoteAddr )
196
196
}, optOnlyServer , func (c net.Conn , st http.ConnState ) {
197
197
t .Logf ("conn %v is now state %v" , c .RemoteAddr (), st )
198
198
})
199
199
defer st .Close ()
200
200
tr := & Transport {TLSClientConfig : tlsConfigInsecure }
201
+ if useClient {
202
+ tr .ConnPool = noDialClientConnPool {new (clientConnPool )}
203
+ }
201
204
defer tr .CloseIdleConnections ()
202
205
get := func () string {
203
206
req , err := http .NewRequest ("GET" , st .ts .URL , nil )
204
207
if err != nil {
205
208
t .Fatal (err )
206
209
}
207
210
modReq (req )
208
- res , err := tr .RoundTrip (req )
211
+ var res * http.Response
212
+ if useClient {
213
+ c := st .ts .Client ()
214
+ ConfigureTransports (c .Transport .(* http.Transport ))
215
+ res , err = c .Do (req )
216
+ } else {
217
+ res , err = tr .RoundTrip (req )
218
+ }
209
219
if err != nil {
210
220
t .Fatal (err )
211
221
}
@@ -222,24 +232,39 @@ func onSameConn(t *testing.T, modReq func(*http.Request)) bool {
222
232
}
223
233
first := get ()
224
234
second := get ()
225
- return first == second
226
- }
227
-
228
- func TestTransportReusesConns (t * testing.T ) {
229
- if ! onSameConn (t , func (* http.Request ) {}) {
230
- t .Errorf ("first and second responses were on different connections" )
235
+ if got := first == second ; got != wantSame {
236
+ t .Errorf ("first and second responses on same connection: %v; want %v" , got , wantSame )
231
237
}
232
238
}
233
239
234
- func TestTransportReusesConn_RequestClose (t * testing.T ) {
235
- if onSameConn (t , func (r * http.Request ) { r .Close = true }) {
236
- t .Errorf ("first and second responses were not on different connections" )
237
- }
238
- }
239
-
240
- func TestTransportReusesConn_ConnClose (t * testing.T ) {
241
- if onSameConn (t , func (r * http.Request ) { r .Header .Set ("Connection" , "close" ) }) {
242
- t .Errorf ("first and second responses were not on different connections" )
240
+ func TestTransportReusesConns (t * testing.T ) {
241
+ for _ , test := range []struct {
242
+ name string
243
+ modReq func (* http.Request )
244
+ wantSame bool
245
+ }{{
246
+ name : "ReuseConn" ,
247
+ modReq : func (* http.Request ) {},
248
+ wantSame : true ,
249
+ }, {
250
+ name : "RequestClose" ,
251
+ modReq : func (r * http.Request ) { r .Close = true },
252
+ wantSame : false ,
253
+ }, {
254
+ name : "ConnClose" ,
255
+ modReq : func (r * http.Request ) { r .Header .Set ("Connection" , "close" ) },
256
+ wantSame : false ,
257
+ }} {
258
+ t .Run (test .name , func (t * testing.T ) {
259
+ t .Run ("Transport" , func (t * testing.T ) {
260
+ const useClient = false
261
+ testTransportReusesConns (t , useClient , test .wantSame , test .modReq )
262
+ })
263
+ t .Run ("Client" , func (t * testing.T ) {
264
+ const useClient = true
265
+ testTransportReusesConns (t , useClient , test .wantSame , test .modReq )
266
+ })
267
+ })
243
268
}
244
269
}
245
270
0 commit comments