From 2f2d7dcfaa21cbcb69e17b9ea7d7a444a5734242 Mon Sep 17 00:00:00 2001 From: dillonstreator Date: Fri, 11 Nov 2022 10:17:48 -0600 Subject: [PATCH] contrib/net/http nil transport should default to http.DefaultTransport (#1572) * null transport should default to http.DefaultTransport * Test name casing update Co-authored-by: Andrew Glaude Co-authored-by: Andrew Glaude --- contrib/net/http/roundtripper.go | 3 +++ contrib/net/http/roundtripper_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/contrib/net/http/roundtripper.go b/contrib/net/http/roundtripper.go index 90a6451cb9..40a6c761f0 100644 --- a/contrib/net/http/roundtripper.go +++ b/contrib/net/http/roundtripper.go @@ -82,6 +82,9 @@ func (rt *roundTripper) Unwrap() http.RoundTripper { // WrapRoundTripper returns a new RoundTripper which traces all requests sent // over the transport. func WrapRoundTripper(rt http.RoundTripper, opts ...RoundTripperOption) http.RoundTripper { + if rt == nil { + rt = http.DefaultTransport + } cfg := newRoundTripperConfig() for _, opt := range opts { opt(cfg) diff --git a/contrib/net/http/roundtripper_test.go b/contrib/net/http/roundtripper_test.go index 45bad4c640..0ec1401527 100644 --- a/contrib/net/http/roundtripper_test.go +++ b/contrib/net/http/roundtripper_test.go @@ -21,6 +21,18 @@ import ( "gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig" ) +func TestWrapRoundTripperAllowNilTransport(t *testing.T) { + assert := assert.New(t) + + httpClient := &http.Client{} + httpClient.Transport = WrapRoundTripper(httpClient.Transport) + + wrapped, ok := httpClient.Transport.(*roundTripper) + assert.True(ok) + + assert.Equal(http.DefaultTransport, wrapped.base) +} + func TestRoundTripper(t *testing.T) { mt := mocktracer.Start() defer mt.Stop()