1717import { HeaderGetter , HeaderSetter , SpanContext } from '@opencensus/core' ;
1818import * as assert from 'assert' ;
1919
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/' ;
2721
2822const traceContextFormat = new TraceContextFormat ( ) ;
2923
@@ -34,8 +28,8 @@ describe('TraceContextPropagation', () => {
3428
3529 beforeEach ( ( ) => {
3630 emptySpanContext = {
37- traceId : undefined ,
38- spanId : undefined ,
31+ traceId : '' ,
32+ spanId : '' ,
3933 options : DEFAULT_OPTIONS ,
4034 traceState : undefined
4135 } ;
@@ -44,7 +38,7 @@ describe('TraceContextPropagation', () => {
4438 // Generates the appropriate `traceparent` header for the given SpanContext
4539 const traceParentHeaderFromSpanContext =
4640 ( spanContext : SpanContext ) : string => {
47- const { traceId, spanId, options } = spanContext ;
41+ const { traceId, spanId} = spanContext ;
4842 return `00-${ traceId } -${ spanId } -${
4943 Buffer . from ( [ spanContext . options ] ) . toString ( 'hex' ) } `;
5044 } ;
@@ -57,8 +51,9 @@ describe('TraceContextPropagation', () => {
5751 // Construct headers from the generated span context
5852 const headers : Record < string , string > = { } ;
5953 headers [ TRACE_PARENT ] = traceParentHeaderFromSpanContext ( spanContext ) ;
60- headers [ TRACE_STATE ] = spanContext . traceState ;
61-
54+ if ( spanContext . traceState ) {
55+ headers [ TRACE_STATE ] = spanContext . traceState ;
56+ }
6257 const getter : HeaderGetter = {
6358 getHeader ( name : string ) {
6459 return headers [ name ] ;
@@ -135,7 +130,6 @@ describe('TraceContextPropagation', () => {
135130 } ;
136131
137132 Object . getOwnPropertyNames ( testCases ) . forEach ( testCase => {
138- const traceState = '' ;
139133 const headers : Headers = {
140134 [ TRACE_PARENT ] : testCases [ testCase ] ,
141135 [ TRACE_STATE ] : '' ,
@@ -179,8 +173,10 @@ describe('TraceContextPropagation', () => {
179173 } ;
180174
181175 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+ }
184180 } ) ;
185181 } ) ;
186182
@@ -205,18 +201,13 @@ describe('TraceContextPropagation', () => {
205201 it ( 'should gracefully handle an unset header' , ( ) => {
206202 const getter : HeaderGetter = {
207203 getHeader ( name : string ) {
208- return null ;
204+ return undefined ;
209205 }
210206 } ;
211207
212208 const extractedSpanContext = traceContextFormat . extract ( getter ) ;
213209 assert . deepEqual ( extractedSpanContext , emptySpanContext ) ;
214210 } ) ;
215-
216- it ( 'should gracefully handle null getter' , ( ) => {
217- const spanContext = traceContextFormat . extract ( null ) ;
218- assert . equal ( spanContext , null ) ;
219- } ) ;
220211 } ) ;
221212
222213 describe ( 'inject' , ( ) => {
@@ -251,24 +242,6 @@ describe('TraceContextPropagation', () => {
251242 traceContextFormat . inject ( setter , spanContext ) ;
252243 assert . strictEqual ( headers [ TRACE_STATE ] , 'foo=bar' ) ;
253244 } ) ;
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- } ) ;
272245 } ) ;
273246
274247 describe ( 'generate' , ( ) => {
0 commit comments