@@ -803,4 +803,108 @@ describe('XhrPlugin tests', () => {
803803 ]
804804 } ) ;
805805 } ) ;
806+
807+ test ( 'when the plugin records a trace then the trace id is added to the http event' , async ( ) => {
808+ // Init
809+ const config : PartialHttpPluginConfig = {
810+ logicalServiceName : 'sample.rum.aws.amazon.com' ,
811+ urlsToInclude : [ / r e s p o n s e \. j s o n / ] ,
812+ recordAllRequests : true
813+ } ;
814+
815+ mock . get ( / .* / , {
816+ body : JSON . stringify ( { message : 'Hello World!' } ) ,
817+ headers : { 'Content-Length' : '125' } as MockHeaders
818+ } ) ;
819+
820+ const plugin : XhrPlugin = new XhrPlugin ( config ) ;
821+ plugin . load ( xRayOnContext ) ;
822+
823+ // Run
824+ const xhr = new XMLHttpRequest ( ) ;
825+ xhr . open ( 'GET' , './response.json' , true ) ;
826+ xhr . send ( ) ;
827+
828+ // Yield to the event queue so the event listeners can run
829+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
830+
831+ plugin . disable ( ) ;
832+
833+ // Assert
834+ expect ( record ) . toHaveBeenCalledTimes ( 2 ) ;
835+ expect ( record . mock . calls [ 1 ] [ 0 ] ) . toEqual ( HTTP_EVENT_TYPE ) ;
836+ expect ( record . mock . calls [ 1 ] [ 1 ] ) . toMatchObject ( {
837+ trace_id : '1-0-000000000000000000000000' ,
838+ segment_id : '0000000000000000'
839+ } ) ;
840+ } ) ;
841+
842+ test ( 'when XHR aborts with tracing then the trace id is added to the http event' , async ( ) => {
843+ // Init
844+ const config : PartialHttpPluginConfig = {
845+ logicalServiceName : 'sample.rum.aws.amazon.com' ,
846+ urlsToInclude : [ / r e s p o n s e \. j s o n / ]
847+ } ;
848+
849+ mock . get ( / .* / , {
850+ body : JSON . stringify ( { message : 'Hello World!' } )
851+ } ) ;
852+
853+ const plugin : XhrPlugin = new XhrPlugin ( config ) ;
854+ plugin . load ( xRayOnContext ) ;
855+
856+ // Run
857+ const xhr = new XMLHttpRequest ( ) ;
858+ xhr . open ( 'GET' , './response.json' , true ) ;
859+ xhr . send ( ) ;
860+ xhr . abort ( ) ;
861+
862+ // Yield to the event queue so the event listeners can run
863+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
864+
865+ plugin . disable ( ) ;
866+
867+ // Assert
868+ expect ( record ) . toHaveBeenCalledTimes ( 2 ) ;
869+ expect ( record . mock . calls [ 1 ] [ 0 ] ) . toEqual ( HTTP_EVENT_TYPE ) ;
870+ expect ( record . mock . calls [ 1 ] [ 1 ] ) . toMatchObject ( {
871+ trace_id : '1-0-000000000000000000000000' ,
872+ segment_id : '0000000000000000'
873+ } ) ;
874+ } ) ;
875+
876+ test ( 'when the plugin does not record a trace then the trace id is not added to the http event' , async ( ) => {
877+ // Init
878+ const config : PartialHttpPluginConfig = {
879+ logicalServiceName : 'sample.rum.aws.amazon.com' ,
880+ urlsToInclude : [ / r e s p o n s e \. j s o n / ] ,
881+ recordAllRequests : true
882+ } ;
883+
884+ mock . get ( / .* / , {
885+ body : JSON . stringify ( { message : 'Hello World!' } ) ,
886+ headers : { 'Content-Length' : '125' } as MockHeaders
887+ } ) ;
888+
889+ const plugin : XhrPlugin = new XhrPlugin ( config ) ;
890+ plugin . load ( xRayOffContext ) ;
891+
892+ // Run
893+ const xhr = new XMLHttpRequest ( ) ;
894+ xhr . open ( 'GET' , './response.json' , true ) ;
895+ xhr . send ( ) ;
896+
897+ // Yield to the event queue so the event listeners can run
898+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
899+
900+ plugin . disable ( ) ;
901+
902+ // Assert
903+ expect ( record ) . toHaveBeenCalledTimes ( 1 ) ;
904+ expect ( record . mock . calls [ 0 ] [ 0 ] ) . toEqual ( HTTP_EVENT_TYPE ) ;
905+ expect ( record . mock . calls [ 0 ] [ 1 ] ) . not . toMatchObject ( {
906+ trace_id : '1-0-000000000000000000000000' ,
907+ segment_id : '0000000000000000'
908+ } ) ;
909+ } ) ;
806910} ) ;
0 commit comments