@@ -166,10 +166,20 @@ func newGetRequest(t *testing.T) *http.Request {
166166 return req
167167}
168168
169+ // closeBody closes resp.Body if resp is non-nil. Used by tests that exercise
170+ // the transport directly and need to satisfy bodyclose.
171+ func closeBody (t * testing.T , resp * http.Response ) {
172+ t .Helper ()
173+ if resp != nil {
174+ _ = resp .Body .Close ()
175+ }
176+ }
177+
169178func TestTokenRefreshTransport_PassesThrough200 (t * testing.T ) {
170179 transport , rt , _ , src := newTransportFixture (t , rtStep {statusCode : http .StatusOK , body : `{}` })
171180
172181 resp , err := transport .RoundTrip (newGetRequest (t ))
182+ defer closeBody (t , resp )
173183 require .NoError (t , err )
174184 require .Equal (t , http .StatusOK , resp .StatusCode )
175185 require .Equal (t , 1 , rt .callCount ())
@@ -183,6 +193,7 @@ func TestTokenRefreshTransport_RetriesOn401ThenSucceeds(t *testing.T) {
183193 )
184194
185195 resp , err := transport .RoundTrip (newGetRequest (t ))
196+ defer closeBody (t , resp )
186197 require .NoError (t , err )
187198 require .Equal (t , http .StatusOK , resp .StatusCode )
188199 require .Equal (t , 2 , rt .callCount (), "exactly one retry after 401" )
@@ -199,6 +210,7 @@ func TestTokenRefreshTransport_PersistentUnauthRetriesOnceThenSurfaces(t *testin
199210 )
200211
201212 resp , err := transport .RoundTrip (newGetRequest (t ))
213+ defer closeBody (t , resp )
202214 require .NoError (t , err )
203215 require .Equal (t , http .StatusUnauthorized , resp .StatusCode )
204216 require .Equal (t , 2 , rt .callCount (), "exactly one retry — no infinite loop" )
@@ -209,7 +221,8 @@ func TestTokenRefreshTransport_TransportErrorPassesThrough(t *testing.T) {
209221 wantErr := errors .New ("dial tcp: connection refused" )
210222 transport , rt , _ , src := newTransportFixture (t , rtStep {err : wantErr })
211223
212- _ , err := transport .RoundTrip (newGetRequest (t ))
224+ resp , err := transport .RoundTrip (newGetRequest (t ))
225+ defer closeBody (t , resp )
213226 require .ErrorIs (t , err , wantErr )
214227 require .Equal (t , 1 , rt .callCount (), "transport-level errors are not retried" )
215228 require .Equal (t , 0 , src .callCount ())
@@ -224,6 +237,7 @@ func TestTokenRefreshTransport_NoRetryWithBodyAndNoGetBody(t *testing.T) {
224237 req .GetBody = nil // explicitly: no rewind capability
225238
226239 resp , err := transport .RoundTrip (req )
240+ defer closeBody (t , resp )
227241 require .NoError (t , err )
228242 require .Equal (t , http .StatusUnauthorized , resp .StatusCode , "non-rewindable 401 must surface untouched" )
229243 require .Equal (t , 1 , rt .callCount (), "non-rewindable bodies must not be retried" )
@@ -241,6 +255,7 @@ func TestTokenRefreshTransport_NoRetryWhenGetBodyFails(t *testing.T) {
241255 }
242256
243257 resp , err := transport .RoundTrip (req )
258+ defer closeBody (t , resp )
244259 require .NoError (t , err )
245260 require .Equal (t , http .StatusUnauthorized , resp .StatusCode )
246261 require .Equal (t , 1 , rt .callCount (), "GetBody failure must not trigger a retry" )
0 commit comments