Skip to content

Commit b5b5617

Browse files
Pass testing.T to roundtripper as context value. (#2944)
1 parent fca7a42 commit b5b5617

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

conformance/utils/http/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func MakeRequest(t *testing.T, expected *ExpectedResponse, gwAddr, protocol, sch
127127
tlog.Logf(t, "Making %s request to %s", expected.Request.Method, reqURL.String())
128128

129129
req := roundtripper.Request{
130+
T: t,
130131
Method: expected.Request.Method,
131132
Host: expected.Request.Host,
132133
URL: reqURL,

conformance/utils/roundtripper/roundtripper.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3840
const (
@@ -47,6 +49,7 @@ type RoundTripper interface {
4749

4850
// Request is the primary input for making a request.
4951
type 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+
326349
var startLineRegex = regexp.MustCompile(`(?m)^`)
327350

328351
func formatDump(data []byte, prefix string) string {

0 commit comments

Comments
 (0)