55// Infrastructure for testing ClientConn.RoundTrip.
66// Put actual tests in transport_test.go.
77
8- package http2
8+ package http2_test
99
1010import (
1111 "bytes"
@@ -20,6 +20,7 @@ import (
2020 "testing/synctest"
2121 "time"
2222
23+ . "golang.org/x/net/http2"
2324 "golang.org/x/net/http2/hpack"
2425 "golang.org/x/net/internal/gate"
2526)
@@ -108,29 +109,32 @@ type testClientConn struct {
108109 netconn * synctestNetConn
109110}
110111
111- func newTestClientConnFromClientConn (t testing.TB , cc * ClientConn ) * testClientConn {
112+ func newTestClientConnFromClientConn (t testing.TB , tr * Transport , cc * ClientConn ) * testClientConn {
112113 tc := & testClientConn {
113114 t : t ,
114- tr : cc . t ,
115+ tr : tr ,
115116 cc : cc ,
116117 }
117118
118119 // srv is the side controlled by the test.
119120 var srv * synctestNetConn
120- if cc .tconn == nil {
121+ if tconn := cc .TestNetConn (); tconn == nil {
121122 // If cc.tconn is nil, we're being called with a new conn created by the
122123 // Transport's client pool. This path skips dialing the server, and we
123124 // create a test connection pair here.
124- cc .tconn , srv = synctestNetPipe ()
125+ var cli * synctestNetConn
126+ cli , srv = synctestNetPipe ()
127+ cc .TestSetNetConn (cli )
125128 } else {
126129 // If cc.tconn is non-nil, we're in a test which provides a conn to the
127130 // Transport via a TLSNextProto hook. Extract the test connection pair.
128- if tc , ok := cc . tconn .(* tls.Conn ); ok {
131+ if tc , ok := tconn .(* tls.Conn ); ok {
129132 // Unwrap any *tls.Conn to the underlying net.Conn,
130133 // to avoid dealing with encryption in tests.
131- cc .tconn = tc .NetConn ()
134+ tconn = tc .NetConn ()
135+ cc .TestSetNetConn (tconn )
132136 }
133- srv = cc . tconn .(* synctestNetConn ).peer
137+ srv = tconn .(* synctestNetConn ).peer
134138 }
135139
136140 srv .SetReadDeadline (time .Now ())
@@ -141,7 +145,7 @@ func newTestClientConnFromClientConn(t testing.TB, cc *ClientConn) *testClientCo
141145 tc .testConnFramer = testConnFramer {
142146 t : t ,
143147 fr : tc .fr ,
144- dec : hpack .NewDecoder (initialHeaderTableSize , nil ),
148+ dec : hpack .NewDecoder (InitialHeaderTableSize , nil ),
145149 }
146150 tc .fr .SetMaxReadFrameSize (10 << 20 )
147151 t .Cleanup (func () {
@@ -154,12 +158,12 @@ func newTestClientConnFromClientConn(t testing.TB, cc *ClientConn) *testClientCo
154158func (tc * testClientConn ) readClientPreface () {
155159 tc .t .Helper ()
156160 // Read the client's HTTP/2 preface, sent prior to any HTTP/2 frames.
157- buf := make ([]byte , len (clientPreface ))
161+ buf := make ([]byte , len (ClientPreface ))
158162 if _ , err := io .ReadFull (tc .netconn , buf ); err != nil {
159163 tc .t .Fatalf ("reading preface: %v" , err )
160164 }
161- if ! bytes .Equal (buf , clientPreface ) {
162- tc .t .Fatalf ("client preface: %q, want %q" , buf , clientPreface )
165+ if ! bytes .Equal (buf , [] byte ( ClientPreface ) ) {
166+ tc .t .Fatalf ("client preface: %q, want %q" , buf , ClientPreface )
163167 }
164168}
165169
@@ -168,7 +172,7 @@ func newTestClientConn(t testing.TB, opts ...any) *testClientConn {
168172
169173 tt := newTestTransport (t , opts ... )
170174 const singleUse = false
171- _ , err := tt .tr .newClientConn (nil , singleUse , nil )
175+ _ , err := tt .tr .TestNewClientConn (nil , singleUse , nil )
172176 if err != nil {
173177 t .Fatalf ("newClientConn: %v" , err )
174178 }
@@ -303,8 +307,8 @@ func (tc *testClientConn) roundTrip(req *http.Request) *testRoundTrip {
303307 tc .roundtrips = append (tc .roundtrips , rt )
304308 go func () {
305309 defer close (rt .donec )
306- rt .resp , rt .respErr = tc .cc .roundTrip (req , func (cs * clientStream ) {
307- rt .id .Store (cs . ID )
310+ rt .resp , rt .respErr = tc .cc .TestRoundTrip (req , func (streamID uint32 ) {
311+ rt .id .Store (streamID )
308312 })
309313 }()
310314 synctest .Wait ()
@@ -348,17 +352,11 @@ func (tc *testClientConn) makeHeaderBlockFragment(s ...string) []byte {
348352// inflowWindow returns the amount of inbound flow control available for a stream,
349353// or for the connection if streamID is 0.
350354func (tc * testClientConn ) inflowWindow (streamID uint32 ) int32 {
351- tc .cc .mu .Lock ()
352- defer tc .cc .mu .Unlock ()
353- if streamID == 0 {
354- return tc .cc .inflow .avail + tc .cc .inflow .unsent
355- }
356- cs := tc .cc .streams [streamID ]
357- if cs == nil {
358- tc .t .Errorf ("no stream with id %v" , streamID )
359- return - 1
355+ w , err := tc .cc .TestInflowWindow (streamID )
356+ if err != nil {
357+ tc .t .Error (err )
360358 }
361- return cs . inflow . avail + cs . inflow . unsent
359+ return w
362360}
363361
364362// testRoundTrip manages a RoundTrip in progress.
@@ -508,10 +506,7 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
508506 for _ , o := range opts {
509507 switch o := o .(type ) {
510508 case func (* http.Transport ):
511- if tr .t1 == nil {
512- tr .t1 = & http.Transport {}
513- }
514- o (tr .t1 )
509+ o (tr .TestTransport ())
515510 case func (* Transport ):
516511 o (tr )
517512 case * Transport :
@@ -520,12 +515,10 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
520515 }
521516 tt .tr = tr
522517
523- tr .transportTestHooks = & transportTestHooks {
524- newclientconn : func (cc * ClientConn ) {
525- tc := newTestClientConnFromClientConn (t , cc )
526- tt .ccs = append (tt .ccs , tc )
527- },
528- }
518+ tr .TestSetNewClientConnHook (func (cc * ClientConn ) {
519+ tc := newTestClientConnFromClientConn (t , tr , cc )
520+ tt .ccs = append (tt .ccs , tc )
521+ })
529522
530523 t .Cleanup (func () {
531524 synctest .Wait ()
0 commit comments