...

Source file src/google.golang.org/api/transport/http/internal/propagation/http_test.go

Documentation: google.golang.org/api/transport/http/internal/propagation

     1  // Copyright 2018 Google LLC.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.8
     6  // +build go1.8
     7  
     8  package propagation
     9  
    10  import (
    11  	"net/http"
    12  	"reflect"
    13  	"testing"
    14  
    15  	"go.opencensus.io/trace"
    16  )
    17  
    18  func TestHTTPFormat(t *testing.T) {
    19  	format := &HTTPFormat{}
    20  	traceID := [16]byte{16, 84, 69, 170, 120, 67, 188, 139, 242, 6, 177, 32, 0, 16, 0, 0}
    21  	spanID1 := [8]byte{255, 0, 0, 0, 0, 0, 0, 123}
    22  	spanID2 := [8]byte{0, 0, 0, 0, 0, 0, 0, 123}
    23  	tests := []struct {
    24  		incoming        string
    25  		wantSpanContext trace.SpanContext
    26  	}{
    27  		{
    28  			incoming: "105445aa7843bc8bf206b12000100000/18374686479671623803;o=1",
    29  			wantSpanContext: trace.SpanContext{
    30  				TraceID:      traceID,
    31  				SpanID:       spanID1,
    32  				TraceOptions: 1,
    33  			},
    34  		},
    35  		{
    36  			incoming: "105445aa7843bc8bf206b12000100000/123;o=0",
    37  			wantSpanContext: trace.SpanContext{
    38  				TraceID:      traceID,
    39  				SpanID:       spanID2,
    40  				TraceOptions: 0,
    41  			},
    42  		},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.incoming, func(t *testing.T) {
    46  			req, _ := http.NewRequest("GET", "http://example.com", nil)
    47  			req.Header.Add(httpHeader, tt.incoming)
    48  			sc, ok := format.SpanContextFromRequest(req)
    49  			if !ok {
    50  				t.Errorf("exporter.SpanContextFromRequest() = false; want true")
    51  			}
    52  			if got, want := sc, tt.wantSpanContext; !reflect.DeepEqual(got, want) {
    53  				t.Errorf("exporter.SpanContextFromRequest() returned span context %v; want %v", got, want)
    54  			}
    55  
    56  			req, _ = http.NewRequest("GET", "http://example.com", nil)
    57  			format.SpanContextToRequest(sc, req)
    58  			if got, want := req.Header.Get(httpHeader), tt.incoming; got != want {
    59  				t.Errorf("exporter.SpanContextToRequest() returned header %q; want %q", got, want)
    60  			}
    61  		})
    62  	}
    63  }
    64  

View as plain text