@@ -13,6 +13,10 @@ var Utils = require('../../../lib/utils');
1313
1414var logger = require ( '../../../lib/logger' ) . getLogger ( ) ;
1515
16+ import { exportedFacadeSegment , exportedNoOpSegment } from '../../../lib/env/aws_lambda' ;
17+ const { facadeSegment } = exportedFacadeSegment ;
18+ const { noOpSegment } = exportedNoOpSegment ;
19+
1620chai . should ( ) ;
1721chai . use ( sinonChai ) ;
1822
@@ -348,4 +352,91 @@ describe('AWS patcher', function() {
348352 } ) ;
349353
350354 } ) ;
355+
356+
357+ describe ( '#captureAWSRequest-Lambda-PassThrough' , function ( ) {
358+ var awsClient , awsRequest , MyEmitter , sandbox , segment , stubResolve , tempHeader ;
359+
360+ before ( function ( ) {
361+ MyEmitter = function ( ) {
362+ EventEmitter . call ( this ) ;
363+ } ;
364+
365+ awsClient = {
366+ customizeRequests : function customizeRequests ( captureAWSRequest ) {
367+ this . call = captureAWSRequest ;
368+ } ,
369+ throttledError : function throttledError ( ) { }
370+ } ;
371+ awsClient = awsPatcher . captureAWSClient ( awsClient ) ;
372+
373+ util . inherits ( MyEmitter , EventEmitter ) ;
374+ } ) ;
375+
376+ beforeEach ( function ( ) {
377+ sandbox = sinon . createSandbox ( ) ;
378+
379+ awsRequest = {
380+ httpRequest : {
381+ method : 'GET' ,
382+ url : '/' ,
383+ connection : {
384+ remoteAddress : 'localhost'
385+ } ,
386+ headers : { }
387+ } ,
388+ response : { }
389+ } ;
390+
391+ awsRequest . on = function ( event , fcn ) {
392+ if ( event === 'complete' ) {
393+ this . emitter . on ( event , fcn . bind ( this , this . response ) ) ;
394+ } else {
395+ this . emitter . on ( event , fcn . bind ( this , this ) ) ;
396+ }
397+ return this ;
398+ } ;
399+
400+ awsRequest . emitter = new MyEmitter ( ) ;
401+
402+ tempHeader = process . env . _X_AMZN_TRACE_ID ;
403+ process . env . _X_AMZN_TRACE_ID = 'Root=' + traceId + ';Foo=bar' ;
404+
405+ segment = noOpSegment ( ) ;
406+
407+ stubResolve = sandbox . stub ( contextUtils , 'resolveSegment' ) . returns ( segment ) ;
408+ } ) ;
409+
410+ afterEach ( function ( ) {
411+ process . env . _X_AMZN_TRACE_ID = tempHeader ;
412+ sandbox . restore ( ) ;
413+ } ) ;
414+
415+ it ( 'should log an info statement and exit if parent is not found on the context or on the call params' , function ( done ) {
416+ stubResolve . returns ( ) ;
417+ var logStub = sandbox . stub ( logger , 'info' ) ;
418+
419+ awsClient . call ( awsRequest ) ;
420+
421+ setTimeout ( function ( ) {
422+ logStub . should . have . been . calledOnce ;
423+ done ( ) ;
424+ } , 50 ) ;
425+ } ) ;
426+
427+ it ( 'should inject the tracing headers' , function ( done ) {
428+ sandbox . stub ( contextUtils , 'isAutomaticMode' ) . returns ( true ) ;
429+
430+ awsClient . call ( awsRequest ) ;
431+
432+ awsRequest . emitter . emit ( 'build' ) ;
433+
434+ setTimeout ( function ( ) {
435+ var expected = new RegExp ( '^Root=' + traceId + ';Foo=bar$' ) ;
436+ assert . match ( awsRequest . httpRequest . headers [ 'X-Amzn-Trace-Id' ] , expected ) ;
437+ done ( ) ;
438+ } , 50 ) ;
439+ } ) ;
440+
441+ } ) ;
351442} ) ;
0 commit comments