17
17
import { HeaderGetter , HeaderSetter , SpanContext } from '@opencensus/core' ;
18
18
import * as assert from 'assert' ;
19
19
20
- import { TraceContextFormat } from '../src/' ;
21
-
22
- // Header names
23
- const TRACE_PARENT = 'traceparent' ;
24
- const TRACE_STATE = 'tracestate' ;
25
-
26
- const DEFAULT_OPTIONS = 0x0 ;
20
+ import { DEFAULT_OPTIONS , TRACE_PARENT , TRACE_STATE , TraceContextFormat } from '../src/' ;
27
21
28
22
const traceContextFormat = new TraceContextFormat ( ) ;
29
23
@@ -34,8 +28,8 @@ describe('TraceContextPropagation', () => {
34
28
35
29
beforeEach ( ( ) => {
36
30
emptySpanContext = {
37
- traceId : undefined ,
38
- spanId : undefined ,
31
+ traceId : '' ,
32
+ spanId : '' ,
39
33
options : DEFAULT_OPTIONS ,
40
34
traceState : undefined
41
35
} ;
@@ -44,7 +38,7 @@ describe('TraceContextPropagation', () => {
44
38
// Generates the appropriate `traceparent` header for the given SpanContext
45
39
const traceParentHeaderFromSpanContext =
46
40
( spanContext : SpanContext ) : string => {
47
- const { traceId, spanId, options } = spanContext ;
41
+ const { traceId, spanId} = spanContext ;
48
42
return `00-${ traceId } -${ spanId } -${
49
43
Buffer . from ( [ spanContext . options ] ) . toString ( 'hex' ) } `;
50
44
} ;
@@ -57,8 +51,9 @@ describe('TraceContextPropagation', () => {
57
51
// Construct headers from the generated span context
58
52
const headers : Record < string , string > = { } ;
59
53
headers [ TRACE_PARENT ] = traceParentHeaderFromSpanContext ( spanContext ) ;
60
- headers [ TRACE_STATE ] = spanContext . traceState ;
61
-
54
+ if ( spanContext . traceState ) {
55
+ headers [ TRACE_STATE ] = spanContext . traceState ;
56
+ }
62
57
const getter : HeaderGetter = {
63
58
getHeader ( name : string ) {
64
59
return headers [ name ] ;
@@ -135,7 +130,6 @@ describe('TraceContextPropagation', () => {
135
130
} ;
136
131
137
132
Object . getOwnPropertyNames ( testCases ) . forEach ( testCase => {
138
- const traceState = '' ;
139
133
const headers : Headers = {
140
134
[ TRACE_PARENT ] : testCases [ testCase ] ,
141
135
[ TRACE_STATE ] : '' ,
@@ -179,8 +173,10 @@ describe('TraceContextPropagation', () => {
179
173
} ;
180
174
181
175
const extractedSpanContext = traceContextFormat . extract ( getter ) ;
182
- assert . strictEqual (
183
- extractedSpanContext . options , DEFAULT_OPTIONS , testCase ) ;
176
+ if ( extractedSpanContext !== null ) {
177
+ assert . strictEqual (
178
+ extractedSpanContext . options , DEFAULT_OPTIONS , testCase ) ;
179
+ }
184
180
} ) ;
185
181
} ) ;
186
182
@@ -205,18 +201,13 @@ describe('TraceContextPropagation', () => {
205
201
it ( 'should gracefully handle an unset header' , ( ) => {
206
202
const getter : HeaderGetter = {
207
203
getHeader ( name : string ) {
208
- return null ;
204
+ return undefined ;
209
205
}
210
206
} ;
211
207
212
208
const extractedSpanContext = traceContextFormat . extract ( getter ) ;
213
209
assert . deepEqual ( extractedSpanContext , emptySpanContext ) ;
214
210
} ) ;
215
-
216
- it ( 'should gracefully handle null getter' , ( ) => {
217
- const spanContext = traceContextFormat . extract ( null ) ;
218
- assert . equal ( spanContext , null ) ;
219
- } ) ;
220
211
} ) ;
221
212
222
213
describe ( 'inject' , ( ) => {
@@ -251,24 +242,6 @@ describe('TraceContextPropagation', () => {
251
242
traceContextFormat . inject ( setter , spanContext ) ;
252
243
assert . strictEqual ( headers [ TRACE_STATE ] , 'foo=bar' ) ;
253
244
} ) ;
254
-
255
- it ( 'should gracefully handle null setter or context' , ( ) => {
256
- // Null setter
257
- const spanContext = traceContextFormat . generate ( ) ;
258
- try {
259
- traceContextFormat . inject ( null , spanContext ) ;
260
- } catch ( err ) {
261
- assert . fail ( err ) ;
262
- }
263
-
264
- // Null context
265
- const setter : HeaderSetter = { setHeader ( name : string , value : string ) { } } ;
266
- try {
267
- traceContextFormat . inject ( setter , null ) ;
268
- } catch ( err ) {
269
- assert . fail ( err ) ;
270
- }
271
- } ) ;
272
245
} ) ;
273
246
274
247
describe ( 'generate' , ( ) => {
0 commit comments