req := treq.Request
trace := treq.trace
ctx := req.Context()
- if trace != nil {
+ if trace != nil && trace.GetConn != nil {
trace.GetConn(cm.addr())
}
if pc, idleSince := t.getIdleConn(cm); pc != nil {
- if trace != nil {
+ if trace != nil && trace.GotConn != nil {
trace.GotConn(pc.gotIdleConnTrace(idleSince))
}
// set request canceler to some non-nil function so we
select {
case v := <-dialc:
// Our dial finished.
- if trace != nil && v.pc != nil {
+ if trace != nil && trace.GotConn != nil && v.pc != nil {
trace.GotConn(httptrace.GotConnInfo{Conn: v.pc.conn})
}
return v.pc, v.err
}
}
-func TestTransportEventTrace(t *testing.T) {
+func TestTransportEventTrace(t *testing.T) { testTransportEventTrace(t, false) }
+
+// test a non-nil httptrace.ClientTrace but with all hooks set to zero.
+func TestTransportEventTrace_NoHooks(t *testing.T) { testTransportEventTrace(t, true) }
+
+func testTransportEventTrace(t *testing.T, noHooks bool) {
defer afterTest(t)
const resBody = "some body"
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
})
req, _ := NewRequest("POST", "http://dns-is-faked.golang:"+port, strings.NewReader("some body"))
- req = req.WithContext(httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
+ trace := &httptrace.ClientTrace{
GetConn: func(hostPort string) { logf("Getting conn for %v ...", hostPort) },
GotConn: func(ci httptrace.GotConnInfo) { logf("got conn: %+v", ci) },
GotFirstResponseByte: func() { logf("first response byte") },
Wait100Continue: func() { logf("Wait100Continue") },
Got100Continue: func() { logf("Got100Continue") },
WroteRequest: func(e httptrace.WroteRequestInfo) { logf("WroteRequest: %+v", e) },
- }))
+ }
+ if noHooks {
+ // zero out all func pointers, trying to get some path to crash
+ *trace = httptrace.ClientTrace{}
+ }
+ req = req.WithContext(httptrace.WithClientTrace(ctx, trace))
req.Header.Set("Expect", "100-continue")
res, err := c.Do(req)
}
res.Body.Close()
+ if noHooks {
+ // Done at this point. Just testing a full HTTP
+ // requests can happen with a trace pointing to a zero
+ // ClientTrace, full of nil func pointers.
+ return
+ }
+
got := buf.String()
wantSub := func(sub string) {
if !strings.Contains(got, sub) {