@@ -24,6 +24,8 @@ import { HttpEvent } from '../../../events/http-event';
2424// Mock getRandomValues -- since it does nothing, the 'random' number will be 0.
2525jest . mock ( '../../../utils/random' ) ;
2626
27+ const URL = 'https://aws.amazon.com' ;
28+
2729const TRACE_ID =
2830 'Root=1-0-000000000000000000000000;Parent=0000000000000000;Sampled=1' ;
2931
@@ -83,7 +85,7 @@ describe('FetchPlugin tests', () => {
8385 plugin . load ( xRayOffContext ) ;
8486
8587 // Run
86- await fetch ( 'https://aws.amazon.com' ) ;
88+ await fetch ( URL ) ;
8789 plugin . disable ( ) ;
8890
8991 // Assert
@@ -92,7 +94,8 @@ describe('FetchPlugin tests', () => {
9294 expect ( record . mock . calls [ 0 ] [ 0 ] ) . toEqual ( HTTP_EVENT_TYPE ) ;
9395 expect ( record . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
9496 request : {
95- method : 'GET'
97+ method : 'GET' ,
98+ url : URL
9699 } ,
97100 response : {
98101 status : 200 ,
@@ -113,7 +116,7 @@ describe('FetchPlugin tests', () => {
113116 plugin . load ( xRayOffContext ) ;
114117
115118 // Run
116- await fetch ( 'https://aws.amazon.com' ) . catch ( ( error ) => {
119+ await fetch ( URL ) . catch ( ( error ) => {
117120 // Expected
118121 } ) ;
119122 plugin . disable ( ) ;
@@ -124,7 +127,8 @@ describe('FetchPlugin tests', () => {
124127 expect ( record . mock . calls [ 0 ] [ 0 ] ) . toEqual ( HTTP_EVENT_TYPE ) ;
125128 expect ( record . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
126129 request : {
127- method : 'GET'
130+ method : 'GET' ,
131+ url : URL
128132 } ,
129133 error : {
130134 version : '1.0.0' ,
@@ -145,7 +149,7 @@ describe('FetchPlugin tests', () => {
145149 plugin . load ( xRayOffContext ) ;
146150
147151 // Run
148- await fetch ( 'https://aws.amazon.com' ) . catch ( ( error ) => {
152+ await fetch ( URL ) . catch ( ( error ) => {
149153 // Expected
150154 } ) ;
151155 plugin . disable ( ) ;
@@ -156,7 +160,8 @@ describe('FetchPlugin tests', () => {
156160 expect ( record . mock . calls [ 0 ] [ 0 ] ) . toEqual ( HTTP_EVENT_TYPE ) ;
157161 expect ( record . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
158162 request : {
159- method : 'GET'
163+ method : 'GET' ,
164+ url : URL
160165 } ,
161166 error : {
162167 version : '1.0.0' ,
@@ -178,7 +183,7 @@ describe('FetchPlugin tests', () => {
178183 plugin . load ( xRayOnContext ) ;
179184
180185 // Run
181- await fetch ( 'https://aws.amazon.com' ) ;
186+ await fetch ( URL ) ;
182187 plugin . disable ( ) ;
183188
184189 // Assert
@@ -201,6 +206,7 @@ describe('FetchPlugin tests', () => {
201206 http : {
202207 request : {
203208 method : 'GET' ,
209+ url : URL ,
204210 traced : true
205211 } ,
206212 response : { status : 200 , content_length : 125 }
@@ -222,7 +228,7 @@ describe('FetchPlugin tests', () => {
222228 plugin . load ( xRayOnContext ) ;
223229
224230 // Run
225- await fetch ( 'https://aws.amazon.com' ) . catch ( ( error ) => {
231+ await fetch ( URL ) . catch ( ( error ) => {
226232 // Expected
227233 } ) ;
228234 plugin . disable ( ) ;
@@ -246,6 +252,7 @@ describe('FetchPlugin tests', () => {
246252 http : {
247253 request : {
248254 method : 'GET' ,
255+ url : URL ,
249256 traced : true
250257 }
251258 } ,
@@ -274,7 +281,7 @@ describe('FetchPlugin tests', () => {
274281 plugin . disable ( ) ;
275282
276283 // Run
277- await fetch ( 'https://aws.amazon.com' ) ;
284+ await fetch ( URL ) ;
278285
279286 // Assert
280287 expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -294,7 +301,7 @@ describe('FetchPlugin tests', () => {
294301 plugin . enable ( ) ;
295302
296303 // Run
297- await fetch ( 'https://aws.amazon.com' ) ;
304+ await fetch ( URL ) ;
298305 plugin . disable ( ) ;
299306
300307 // Assert
@@ -312,7 +319,7 @@ describe('FetchPlugin tests', () => {
312319 const init : RequestInit = { } ;
313320
314321 // Run
315- await fetch ( 'https://aws.amazon.com' , init ) ;
322+ await fetch ( URL , init ) ;
316323 plugin . disable ( ) ;
317324
318325 // Assert
@@ -327,7 +334,7 @@ describe('FetchPlugin tests', () => {
327334 plugin . load ( xRayOnContext ) ;
328335
329336 // Run
330- await fetch ( 'https://aws.amazon.com' ) ;
337+ await fetch ( URL ) ;
331338 plugin . disable ( ) ;
332339
333340 // Assert
@@ -346,7 +353,7 @@ describe('FetchPlugin tests', () => {
346353 const init : RequestInit = { } ;
347354
348355 // Run
349- await fetch ( 'https://aws.amazon.com' , init ) ;
356+ await fetch ( URL , init ) ;
350357 plugin . disable ( ) ;
351358
352359 // Assert
@@ -364,7 +371,7 @@ describe('FetchPlugin tests', () => {
364371 plugin . load ( xRayOnContext ) ;
365372
366373 // Run
367- await fetch ( 'https://aws.amazon.com' ) ;
374+ await fetch ( URL ) ;
368375 plugin . disable ( ) ;
369376
370377 // Assert
@@ -385,7 +392,7 @@ describe('FetchPlugin tests', () => {
385392 plugin . load ( xRayOffContext ) ;
386393
387394 // Run
388- await fetch ( 'https://aws.amazon.com' ) ;
395+ await fetch ( URL ) ;
389396 plugin . disable ( ) ;
390397
391398 // Assert
@@ -416,7 +423,7 @@ describe('FetchPlugin tests', () => {
416423 plugin . load ( xRayOnContext ) ;
417424
418425 // Run
419- await fetch ( 'https://aws.amazon.com' ) ;
426+ await fetch ( URL ) ;
420427 plugin . disable ( ) ;
421428
422429 // Assert
@@ -445,7 +452,7 @@ describe('FetchPlugin tests', () => {
445452 plugin . load ( xRayOnContext ) ;
446453
447454 // Run
448- await fetch ( 'https://aws.amazon.com' ) ;
455+ await fetch ( URL ) ;
449456 plugin . disable ( ) ;
450457
451458 // Assert
@@ -465,7 +472,7 @@ describe('FetchPlugin tests', () => {
465472 plugin . load ( xRayOffContext ) ;
466473
467474 // Run
468- await fetch ( 'https://aws.amazon.com' ) . catch ( ( error ) => {
475+ await fetch ( URL ) . catch ( ( error ) => {
469476 // Expected
470477 } ) ;
471478 plugin . disable ( ) ;
@@ -500,7 +507,7 @@ describe('FetchPlugin tests', () => {
500507 plugin . load ( xRayOffContext ) ;
501508
502509 // Run
503- await fetch ( 'https://aws.amazon.com' ) . catch ( ( error ) => {
510+ await fetch ( URL ) . catch ( ( error ) => {
504511 // Expected
505512 } ) ;
506513 plugin . disable ( ) ;
@@ -535,7 +542,7 @@ describe('FetchPlugin tests', () => {
535542 plugin . load ( xRayOffContext ) ;
536543
537544 // Run
538- await fetch ( 'https://aws.amazon.com' ) ;
545+ await fetch ( URL ) ;
539546 plugin . disable ( ) ;
540547
541548 // Assert
@@ -554,7 +561,7 @@ describe('FetchPlugin tests', () => {
554561 plugin . load ( xRayOffContext ) ;
555562
556563 // Run
557- await fetch ( 'https://aws.amazon.com' ) ;
564+ await fetch ( URL ) ;
558565 plugin . disable ( ) ;
559566
560567 // Assert
@@ -574,7 +581,7 @@ describe('FetchPlugin tests', () => {
574581 plugin . load ( xRayOffContext ) ;
575582
576583 // Run
577- await fetch ( 'https://aws.amazon.com' ) ;
584+ await fetch ( URL ) ;
578585 plugin . disable ( ) ;
579586 global . fetch = mockFetch ;
580587
@@ -595,7 +602,7 @@ describe('FetchPlugin tests', () => {
595602 plugin . load ( xRayOffContext ) ;
596603
597604 // Run
598- await fetch ( 'https://aws.amazon.com' ) ;
605+ await fetch ( URL ) ;
599606 plugin . disable ( ) ;
600607
601608 // Assert
@@ -615,7 +622,7 @@ describe('FetchPlugin tests', () => {
615622 plugin . load ( xRayOffContext ) ;
616623
617624 // Run
618- await fetch ( { url : 'https://aws.amazon.com' } as Request ) ;
625+ await fetch ( { url : URL } as Request ) ;
619626 plugin . disable ( ) ;
620627
621628 // Assert
@@ -633,7 +640,7 @@ describe('FetchPlugin tests', () => {
633640 plugin . load ( xRayOffContext ) ;
634641
635642 // Run
636- await fetch ( 'https://aws.amazon.com' ) ;
643+ await fetch ( URL ) ;
637644 plugin . disable ( ) ;
638645
639646 // Assert
@@ -720,7 +727,7 @@ describe('FetchPlugin tests', () => {
720727 }
721728 } ;
722729
723- const request : Request = new Request ( 'https://aws.amazon.com' , init ) ;
730+ const request : Request = new Request ( URL , init ) ;
724731
725732 // Run
726733 await fetch ( request ) ;
@@ -730,4 +737,36 @@ describe('FetchPlugin tests', () => {
730737 expect ( request . headers . get ( X_AMZN_TRACE_ID ) ) . toEqual ( TRACE_ID ) ;
731738 expect ( request . headers . get ( SIGN_HEADER_KEY ) ) . toEqual ( SIGN_HEADER_VAL ) ;
732739 } ) ;
740+
741+ test ( 'when fetch uses request object then the URL is added to the event' , async ( ) => {
742+ // Init
743+ const config : PartialHttpPluginConfig = {
744+ urlsToInclude : [ / a w s \. a m a z o n \. c o m / ] ,
745+ recordAllRequests : true
746+ } ;
747+
748+ const plugin : FetchPlugin = new FetchPlugin ( config ) ;
749+ plugin . load ( xRayOffContext ) ;
750+
751+ const request : Request = new Request ( URL ) ;
752+
753+ // Run
754+ await fetch ( request ) ;
755+ plugin . disable ( ) ;
756+
757+ // Assert
758+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
759+ expect ( record ) . toHaveBeenCalledTimes ( 1 ) ;
760+ expect ( record . mock . calls [ 0 ] [ 0 ] ) . toEqual ( HTTP_EVENT_TYPE ) ;
761+ expect ( record . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
762+ request : {
763+ method : 'GET' ,
764+ url : URL
765+ } ,
766+ response : {
767+ status : 200 ,
768+ statusText : 'OK'
769+ }
770+ } ) ;
771+ } ) ;
733772} ) ;
0 commit comments