github.com/xmidt-org/webpa-common@v1.11.9/tracing/tracinghttp/http_test.go (about)

     1  package tracinghttp
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/xmidt-org/webpa-common/tracing"
    12  	"github.com/xmidt-org/webpa-common/xhttp"
    13  )
    14  
    15  func TestHeadersForSpans(t *testing.T) {
    16  	var (
    17  		assert = assert.New(t)
    18  
    19  		expectedStart    = time.Now()
    20  		expectedDuration = time.Duration(2342123)
    21  		spanner          = tracing.NewSpanner(
    22  			tracing.Now(func() time.Time { return expectedStart }),
    23  			tracing.Since(func(time.Time) time.Duration { return expectedDuration }),
    24  		)
    25  
    26  		testData = []struct {
    27  			spans          []tracing.Span
    28  			expectedHeader http.Header
    29  			timeLayout     string
    30  		}{
    31  			{expectedHeader: http.Header{}},
    32  			{
    33  				spans: []tracing.Span{
    34  					spanner.Start("first")(nil),
    35  					spanner.Start("second")(errors.New("second error")),
    36  					spanner.Start("third")(&xhttp.Error{Code: 503, Text: "fubar"}),
    37  				},
    38  				expectedHeader: http.Header{
    39  					SpanHeader: []string{
    40  						fmt.Sprintf(`"%s","%s","%s"`, "first", expectedStart.UTC().Format(time.RFC3339), expectedDuration.String()),
    41  						fmt.Sprintf(`"%s","%s","%s"`, "second", expectedStart.UTC().Format(time.RFC3339), expectedDuration.String()),
    42  						fmt.Sprintf(`"%s","%s","%s"`, "third", expectedStart.UTC().Format(time.RFC3339), expectedDuration.String()),
    43  					},
    44  					ErrorHeader: []string{
    45  						`"second",,"second error"`,
    46  						`"third",503,"fubar"`,
    47  					},
    48  				},
    49  			},
    50  		}
    51  	)
    52  
    53  	for _, record := range testData {
    54  		t.Logf("%#v", record)
    55  
    56  		actualHeader := make(http.Header)
    57  		HeadersForSpans(record.timeLayout, actualHeader, record.spans...)
    58  		assert.Equal(record.expectedHeader, actualHeader)
    59  	}
    60  }