@@ -14,6 +14,10 @@ import { XRAY_TRACE_EVENT_TYPE, HTTP_EVENT_TYPE } from '../../utils/constant';
1414import { DEFAULT_CONFIG } from '../../../test-utils/test-utils' ;
1515import { MockHeaders } from 'xhr-mock/lib/types' ;
1616
17+ const actualUserAgent = navigator . userAgent ;
18+ const SYNTHETIC_USER_AGENT =
19+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11 CloudWatchSynthetics/arn:aws:synthetics:us-west-2:0000000000000:canary:test-canary-name' ;
20+
1721// Mock getRandomValues -- since it does nothing, the 'random' number will be 0.
1822jest . mock ( '../../../utils/random' ) ;
1923
@@ -907,4 +911,98 @@ describe('XhrPlugin tests', () => {
907911 segment_id : '0000000000000000'
908912 } ) ;
909913 } ) ;
914+
915+ test ( 'when user agent is CW Synthetics then plugin does not record a trace' , async ( ) => {
916+ // Init
917+ Object . defineProperty ( navigator , 'userAgent' , {
918+ get ( ) {
919+ return SYNTHETIC_USER_AGENT ;
920+ } ,
921+ configurable : true
922+ } ) ;
923+
924+ const config : PartialHttpPluginConfig = {
925+ urlsToInclude : [ / r e s p o n s e \. j s o n / ]
926+ } ;
927+
928+ mock . get ( / .* / , {
929+ body : JSON . stringify ( { message : 'Hello World!' } )
930+ } ) ;
931+
932+ const plugin : XhrPlugin = new XhrPlugin ( config ) ;
933+ plugin . load ( xRayOnContext ) ;
934+
935+ // Run
936+ const xhr = new XMLHttpRequest ( ) ;
937+ xhr . open ( 'GET' , './response.json' , true ) ;
938+ xhr . send ( ) ;
939+
940+ // Yield to the event queue so the event listeners can run
941+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
942+
943+ // Reset
944+ plugin . disable ( ) ;
945+ Object . defineProperty ( navigator , 'userAgent' , {
946+ get ( ) {
947+ return actualUserAgent ;
948+ } ,
949+ configurable : true
950+ } ) ;
951+
952+ // Assert
953+ expect ( record ) . not . toHaveBeenCalled ( ) ;
954+ } ) ;
955+
956+ test ( 'when user agent is CW Synthetics then the plugin records the http request/response' , async ( ) => {
957+ // Init
958+ Object . defineProperty ( navigator , 'userAgent' , {
959+ get ( ) {
960+ return SYNTHETIC_USER_AGENT ;
961+ } ,
962+ configurable : true
963+ } ) ;
964+
965+ const config : PartialHttpPluginConfig = {
966+ urlsToInclude : [ / r e s p o n s e \. j s o n / ] ,
967+ recordAllRequests : true
968+ } ;
969+
970+ mock . get ( / .* / , {
971+ body : JSON . stringify ( { message : 'Hello World!' } )
972+ } ) ;
973+
974+ const plugin : XhrPlugin = new XhrPlugin ( config ) ;
975+ plugin . load ( xRayOnContext ) ;
976+
977+ // Run
978+ const xhr = new XMLHttpRequest ( ) ;
979+ xhr . open ( 'GET' , './response.json' , true ) ;
980+ xhr . send ( ) ;
981+
982+ // Yield to the event queue so the event listeners can run
983+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
984+
985+ // Reset
986+ plugin . disable ( ) ;
987+ Object . defineProperty ( navigator , 'userAgent' , {
988+ get ( ) {
989+ return actualUserAgent ;
990+ } ,
991+ configurable : true
992+ } ) ;
993+
994+ // Assert
995+ expect ( record ) . toHaveBeenCalledTimes ( 1 ) ;
996+ expect ( record . mock . calls [ 0 ] [ 0 ] ) . toEqual ( HTTP_EVENT_TYPE ) ;
997+ expect ( record . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
998+ request : {
999+ method : 'GET' ,
1000+ url : './response.json'
1001+ } ,
1002+ response : {
1003+ status : 200 ,
1004+ statusText : 'OK'
1005+ }
1006+ } ) ;
1007+ } ) ;
9101008} ) ;
0 commit comments