github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/w3ctrace/context_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package w3ctrace_test 5 6 import ( 7 "net/http" 8 "testing" 9 10 "github.com/instana/testify/assert" 11 "github.com/instana/testify/require" 12 "github.com/mier85/go-sensor/w3ctrace" 13 ) 14 15 const ( 16 exampleTraceParent = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01" 17 exampleTraceState = "vendorname1=opaqueValue1 , vendorname2=opaqueValue2" 18 ) 19 20 func TestNew(t *testing.T) { 21 p := w3ctrace.Parent{ 22 Version: w3ctrace.Version_Max, 23 TraceID: "1234", 24 ParentID: "5678", 25 } 26 27 assert.Equal(t, w3ctrace.Context{RawParent: p.String()}, w3ctrace.New(p)) 28 } 29 30 func TestExtract(t *testing.T) { 31 examples := map[string]struct { 32 ParentHeader string 33 StateHeader string 34 }{ 35 "lower case": {"traceparent", "tracestate"}, 36 "upper case": {"TRACEPARENT", "TRACESTATE"}, 37 "mixed case": {"Traceparent", "Tracestate"}, 38 } 39 40 for name, example := range examples { 41 t.Run(name, func(t *testing.T) { 42 headers := http.Header{} 43 // set raw headers to preserve header name case 44 headers[example.ParentHeader] = []string{exampleTraceParent} 45 headers[example.StateHeader] = []string{exampleTraceState} 46 47 tr, err := w3ctrace.Extract(headers) 48 require.NoError(t, err) 49 50 assert.Equal(t, w3ctrace.Context{ 51 RawParent: exampleTraceParent, 52 RawState: exampleTraceState, 53 }, tr) 54 }) 55 } 56 } 57 58 func TestExtract_NoContext(t *testing.T) { 59 headers := http.Header{} 60 headers.Set(w3ctrace.TraceStateHeader, exampleTraceState) 61 62 _, err := w3ctrace.Extract(headers) 63 assert.Equal(t, w3ctrace.ErrContextNotFound, err) 64 } 65 66 func TestInject(t *testing.T) { 67 examples := map[string]http.Header{ 68 "add": { 69 "Authorization": []string{"Basic 123"}, 70 }, 71 "update": { 72 "Authorization": []string{"Basic 123"}, 73 "traceparent": []string{"00-abcdef1-01"}, 74 "TraceState": []string{"x=y"}, 75 }, 76 } 77 78 for name, headers := range examples { 79 t.Run(name, func(t *testing.T) { 80 w3ctrace.Inject(w3ctrace.Context{ 81 RawParent: exampleTraceParent, 82 RawState: exampleTraceState, 83 }, headers) 84 85 assert.Equal(t, "Basic 123", headers.Get("Authorization")) 86 assert.Equal(t, exampleTraceParent, headers.Get(w3ctrace.TraceParentHeader)) 87 assert.Equal(t, exampleTraceState, headers.Get(w3ctrace.TraceStateHeader)) 88 }) 89 } 90 } 91 92 func TestContext_State(t *testing.T) { 93 trCtx := w3ctrace.Context{ 94 RawParent: exampleTraceParent, 95 RawState: exampleTraceState, 96 } 97 98 assert.Equal(t, w3ctrace.NewState([]string{"vendorname1=opaqueValue1 ", " vendorname2=opaqueValue2"}, ""), trCtx.State()) 99 } 100 101 func TestContext_Parent(t *testing.T) { 102 trCtx := w3ctrace.Context{ 103 RawParent: exampleTraceParent, 104 RawState: exampleTraceState, 105 } 106 107 assert.Equal(t, w3ctrace.Parent{ 108 Version: w3ctrace.Version_0, 109 TraceID: "0af7651916cd43dd8448eb211c80319c", 110 ParentID: "b7ad6b7169203331", 111 Flags: w3ctrace.Flags{ 112 Sampled: true, 113 }, 114 }, trCtx.Parent()) 115 } 116 117 func TestContext_IsZero(t *testing.T) { 118 examples := map[string]struct { 119 Context w3ctrace.Context 120 Expected bool 121 }{ 122 "empty": { 123 Context: w3ctrace.Context{}, 124 Expected: true, 125 }, 126 "non-empty": { 127 Context: w3ctrace.Context{ 128 RawParent: "parent", 129 RawState: "state", 130 }, 131 Expected: false, 132 }, 133 "with empty state": { 134 Context: w3ctrace.Context{ 135 RawParent: "parent", 136 }, 137 Expected: false, 138 }, 139 } 140 141 for name, example := range examples { 142 t.Run(name, func(t *testing.T) { 143 assert.Equal(t, example.Expected, example.Context.IsZero()) 144 }) 145 } 146 }