@@ -29,10 +29,12 @@ import (
2929 "net/http/httputil"
3030 "net/url"
3131 "regexp"
32+ "testing"
3233
3334 "golang.org/x/net/http2"
3435
3536 "sigs.k8s.io/gateway-api/conformance/utils/config"
37+ "sigs.k8s.io/gateway-api/conformance/utils/tlog"
3638)
3739
3840const (
@@ -47,6 +49,7 @@ type RoundTripper interface {
4749
4850// Request is the primary input for making a request.
4951type Request struct {
52+ T * testing.T
5053 URL url.URL
5154 Host string
5255 Protocol string
@@ -189,6 +192,7 @@ func (d *DefaultRoundTripper) defaultRoundTrip(request Request, transport http.R
189192 }
190193 ctx , cancel := context .WithTimeout (context .Background (), d .TimeoutConfig .RequestTimeout )
191194 defer cancel ()
195+ ctx = withT (ctx , request .T )
192196 req , err := http .NewRequestWithContext (ctx , method , request .URL .String (), nil )
193197 if err != nil {
194198 return nil , nil , err
@@ -211,7 +215,7 @@ func (d *DefaultRoundTripper) defaultRoundTrip(request Request, transport http.R
211215 return nil , nil , err
212216 }
213217
214- fmt . Printf ( "Sending Request:\n %s\n \n " , formatDump (dump , "< " ))
218+ tlog . Logf ( request . T , "Sending Request:\n %s\n \n " , formatDump (dump , "< " ))
215219 }
216220
217221 resp , err := client .Do (req )
@@ -227,7 +231,7 @@ func (d *DefaultRoundTripper) defaultRoundTrip(request Request, transport http.R
227231 return nil , nil , err
228232 }
229233
230- fmt . Printf ( "Received Response:\n %s\n \n " , formatDump (dump , "< " ))
234+ tlog . Logf ( request . T , "Received Response:\n %s\n \n " , formatDump (dump , "< " ))
231235 }
232236
233237 cReq := & CapturedRequest {}
@@ -323,6 +327,25 @@ func IsTimeoutError(statusCode int) bool {
323327 return false
324328}
325329
330+ // testingTContextKey is the key for adding testing.T to the context.Context
331+ type testingTContextKey struct {}
332+
333+ // withT returns a context with the testing.T added as a value.
334+ func withT (ctx context.Context , t * testing.T ) context.Context {
335+ return context .WithValue (ctx , testingTContextKey {}, t )
336+ }
337+
338+ // TFromContext returns the testing.T added to the context if available.
339+ func TFromContext (ctx context.Context ) (* testing.T , bool ) {
340+ v := ctx .Value (testingTContextKey {})
341+ if v != nil {
342+ if t , ok := v .(* testing.T ); ok {
343+ return t , true
344+ }
345+ }
346+ return nil , false
347+ }
348+
326349var startLineRegex = regexp .MustCompile (`(?m)^` )
327350
328351func formatDump (data []byte , prefix string ) string {
0 commit comments