...

Source file src/go.opencensus.io/exporter/stackdriver/propagation/http_test.go

Documentation: go.opencensus.io/exporter/stackdriver/propagation

     1  // Copyright 2018, OpenCensus Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package propagation
    16  
    17  import (
    18  	"net/http"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"go.opencensus.io/trace"
    23  )
    24  
    25  func TestHTTPFormat(t *testing.T) {
    26  	format := &HTTPFormat{}
    27  	traceID := [16]byte{16, 84, 69, 170, 120, 67, 188, 139, 242, 6, 177, 32, 0, 16, 0, 0}
    28  	spanID1 := [8]byte{255, 0, 0, 0, 0, 0, 0, 123}
    29  	spanID2 := [8]byte{0, 0, 0, 0, 0, 0, 0, 123}
    30  	tests := []struct {
    31  		incoming        string
    32  		wantSpanContext trace.SpanContext
    33  	}{
    34  		{
    35  			incoming: "105445aa7843bc8bf206b12000100000/18374686479671623803;o=1",
    36  			wantSpanContext: trace.SpanContext{
    37  				TraceID:      traceID,
    38  				SpanID:       spanID1,
    39  				TraceOptions: 1,
    40  			},
    41  		},
    42  		{
    43  			incoming: "105445aa7843bc8bf206b12000100000/123;o=0",
    44  			wantSpanContext: trace.SpanContext{
    45  				TraceID:      traceID,
    46  				SpanID:       spanID2,
    47  				TraceOptions: 0,
    48  			},
    49  		},
    50  	}
    51  	for _, tt := range tests {
    52  		t.Run(tt.incoming, func(t *testing.T) {
    53  			req, _ := http.NewRequest("GET", "http://example.com", nil)
    54  			req.Header.Add(httpHeader, tt.incoming)
    55  			sc, ok := format.SpanContextFromRequest(req)
    56  			if !ok {
    57  				t.Errorf("exporter.SpanContextFromRequest() = false; want true")
    58  			}
    59  			if got, want := sc, tt.wantSpanContext; !reflect.DeepEqual(got, want) {
    60  				t.Errorf("exporter.SpanContextFromRequest() returned span context %v; want %v", got, want)
    61  			}
    62  
    63  			req, _ = http.NewRequest("GET", "http://example.com", nil)
    64  			format.SpanContextToRequest(sc, req)
    65  			if got, want := req.Header.Get(httpHeader), tt.incoming; got != want {
    66  				t.Errorf("exporter.SpanContextToRequest() returned header %q; want %q", got, want)
    67  			}
    68  		})
    69  	}
    70  }
    71  

View as plain text