github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/tracing/http_client_test.go (about)

     1  package tracing
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/opentracing/opentracing-go"
    10  	"github.com/opentracing/opentracing-go/mocktracer"
    11  	"github.com/pkg/errors"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestTracingRoundTripper(t *testing.T) {
    16  	tracer := mocktracer.New()
    17  
    18  	// mock Round Tripper to just return a valid response
    19  	var mockRoundTripper http.RoundTripper = RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
    20  		return &http.Response{
    21  			Status:     http.StatusText(http.StatusNotImplemented),
    22  			StatusCode: http.StatusNotImplemented,
    23  		}, nil
    24  	})
    25  
    26  	rt := RoundTripper(tracer, mockRoundTripper)
    27  
    28  	parent := tracer.StartSpan("parent")
    29  	ctx := opentracing.ContextWithSpan(context.Background(), parent)
    30  	req := httptest.NewRequest("GET", "http://example.com/foo", nil)
    31  	req = req.WithContext(ctx)
    32  	res, err := rt.RoundTrip(req)
    33  
    34  	parent.Finish()
    35  	assert.Nil(t, err)
    36  	assert.Equal(t, http.StatusNotImplemented, res.StatusCode)
    37  
    38  	// Did this create the expected spans?
    39  	recordedSpans := tracer.FinishedSpans()
    40  	assert.Len(t, recordedSpans, 2)
    41  	assert.Equal(t, "webhook", recordedSpans[0].OperationName)
    42  	assert.Equal(t, "GET", recordedSpans[0].Tag("http.method"))
    43  	assert.Equal(t, "http://example.com/foo", recordedSpans[0].Tag("http.url"))
    44  	assert.Equal(t, uint16(http.StatusNotImplemented), recordedSpans[0].Tag("http.status_code"))
    45  
    46  	assert.Equal(t, recordedSpans[1].OperationName, "parent")
    47  }
    48  
    49  func TestTracingRoundTripper_error(t *testing.T) {
    50  	tracer := mocktracer.New()
    51  
    52  	var mockRoundTripper http.RoundTripper = RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
    53  		return nil, errors.New("round trip failed!")
    54  	})
    55  
    56  	rt := RoundTripper(tracer, mockRoundTripper)
    57  
    58  	parent := tracer.StartSpan("parent")
    59  	ctx := opentracing.ContextWithSpan(context.Background(), parent)
    60  	req := httptest.NewRequest("GET", "http://example.com/foo", nil)
    61  	req = req.WithContext(ctx)
    62  	res, err := rt.RoundTrip(req)
    63  
    64  	parent.Finish()
    65  	assert.Error(t, err)
    66  	assert.Nil(t, res)
    67  
    68  	recordedSpans := tracer.FinishedSpans()
    69  
    70  	assert.Len(t, recordedSpans, 2)
    71  	assert.Equal(t, "webhook", recordedSpans[0].OperationName)
    72  	assert.Equal(t, "GET", recordedSpans[0].Tag("http.method"))
    73  	assert.Equal(t, "http://example.com/foo", recordedSpans[0].Tag("http.url"))
    74  	assert.Equal(t, "round trip failed!", recordedSpans[0].Tag("http.error"))
    75  	assert.NotNil(t, recordedSpans[0].FinishTime)
    76  	assert.Equal(t, "parent", recordedSpans[1].OperationName)
    77  }