@@ -14,6 +14,7 @@ describe('Middleware utils', function() {
14
14
var envVarName = 'envDefaultName' ;
15
15
var hostName = 'www.myhost.com' ;
16
16
var traceId = '1-f9194208-2c7ad569f5d6ff149137be86' ;
17
+ var parentId = '74051af127d2bcba' ;
17
18
18
19
function reloadMWUtils ( ) {
19
20
var path = '../../../lib/logger' ;
@@ -45,8 +46,6 @@ describe('Middleware utils', function() {
45
46
} ) ;
46
47
47
48
describe ( '#processHeaders' , function ( ) {
48
- var parentId = '74051af127d2bcba' ;
49
-
50
49
it ( 'should return an empty array on an undefined request' , function ( ) {
51
50
var headers = MWUtils . processHeaders ( ) ;
52
51
@@ -149,7 +148,7 @@ describe('Middleware utils', function() {
149
148
} ) ;
150
149
151
150
describe ( '#resolveSampling' , function ( ) {
152
- var res , sandbox , segment , shouldSampleStub ;
151
+ var res , sandbox , segment , responseHeaders , shouldSampleStub ;
153
152
154
153
beforeEach ( function ( ) {
155
154
sandbox = sinon . createSandbox ( ) ;
@@ -158,18 +157,22 @@ describe('Middleware utils', function() {
158
157
shouldSampleStub = sandbox . stub ( MWUtils . sampler , 'shouldSample' ) . returns ( true ) ;
159
158
160
159
segment = { } ;
160
+ responseHeaders = { } ;
161
161
res = {
162
162
req : {
163
163
headers : { host : 'moop.hello.com' } ,
164
164
url : '/evergreen' ,
165
165
method : 'GET' ,
166
166
} ,
167
- header : { }
167
+ header : ( headerKey , headerValue ) => {
168
+ responseHeaders [ headerKey ] = headerValue ;
169
+ }
168
170
} ;
169
171
} ) ;
170
172
171
173
afterEach ( function ( ) {
172
174
sandbox . restore ( ) ;
175
+ responseHeaders = { } ;
173
176
} ) ;
174
177
175
178
it ( 'should not mark segment as not traced if the sampled header is set to "1"' , function ( ) {
@@ -209,7 +212,7 @@ describe('Middleware utils', function() {
209
212
MWUtils . resolveSampling ( headers , segment , res ) ;
210
213
211
214
var expected = new RegExp ( '^Root=' + traceId + ';Sampled=1$' ) ;
212
- assert . match ( res . header [ XRAY_HEADER ] , expected ) ;
215
+ assert . match ( responseHeaders [ XRAY_HEADER ] , expected ) ;
213
216
} ) ;
214
217
215
218
it ( 'should mark segment as not traced if the sampling rules check returns false' , function ( ) {
@@ -220,6 +223,73 @@ describe('Middleware utils', function() {
220
223
221
224
assert . equal ( segment . notTraced , true ) ;
222
225
} ) ;
226
+
227
+ it ( 'should not throw error when res.header is undefined and Sampled=?' , function ( ) {
228
+ var resWithoutHeader = {
229
+ req : {
230
+ headers : { } ,
231
+ url : '/api/move/up' ,
232
+ method : 'GET' ,
233
+ }
234
+ } ;
235
+ shouldSampleStub . returns ( false ) ;
236
+ var headers = { root : traceId , sampled : '?' } ;
237
+
238
+ assert . doesNotThrow (
239
+ ( ) => {
240
+ MWUtils . resolveSampling ( headers , segment , resWithoutHeader ) ;
241
+ }
242
+ ) ;
243
+
244
+ assert . equal ( segment . notTraced , true ) ;
245
+ } ) ;
246
+ } ) ;
247
+
248
+
249
+ describe ( '#traceRequestResponseCycle' , function ( ) {
250
+ var sandbox , shouldSampleStub ;
251
+
252
+ beforeEach ( function ( ) {
253
+ sandbox = sinon . createSandbox ( ) ;
254
+ MWUtils . sampler = localSampler ;
255
+ MWUtils . setDefaultName ( defaultName ) ;
256
+
257
+ shouldSampleStub = sandbox . stub ( MWUtils . sampler , 'shouldSample' ) . returns ( true ) ;
258
+ } ) ;
259
+
260
+ afterEach ( function ( ) {
261
+ sandbox . restore ( ) ;
262
+ } ) ;
263
+
264
+ it ( 'should not throw error when Sampled=?' , function ( ) {
265
+ var req = {
266
+ headers : {
267
+ url : '/api/move/up' ,
268
+ [ XRAY_HEADER ] : 'Root=' + traceId + ';Parent=' + parentId + ';Sampled=?'
269
+ } ,
270
+ host : hostName ,
271
+ method : 'GET' ,
272
+ } ;
273
+ var segment ;
274
+ var responseHeaders = { } ;
275
+
276
+ var res = {
277
+ req : req ,
278
+ on : ( name , callback ) => { } ,
279
+ header : ( headerKey , headerValue ) => {
280
+ responseHeaders [ headerKey ] = headerValue ;
281
+ }
282
+ } ;
283
+ shouldSampleStub . returns ( false ) ;
284
+
285
+ assert . doesNotThrow (
286
+ ( ) => {
287
+ segment = MWUtils . traceRequestResponseCycle ( req , res ) ;
288
+ }
289
+ ) ;
290
+ assert . equal ( segment . notTraced , true ) ;
291
+ assert . equal ( responseHeaders [ XRAY_HEADER ] , 'Root=' + traceId + ';Sampled=0' ) ;
292
+ } ) ;
223
293
} ) ;
224
294
225
295
describe ( '#samplingWildcardMatch' , function ( ) {
0 commit comments