@@ -15,7 +15,9 @@ import {
1515 mockFetchWith500 ,
1616 mockFetchWithError ,
1717 mockFetchWithErrorObject ,
18- mockFetchWithErrorObjectAndStack
18+ mockFetchWithErrorObjectAndStack ,
19+ mockFetchWith400 ,
20+ mockFetchWith429
1921} from '../../../test-utils/test-utils' ;
2022import { GetSession , PluginContext } from '../../types' ;
2123import { XRAY_TRACE_EVENT_TYPE , HTTP_EVENT_TYPE } from '../../utils/constant' ;
@@ -243,6 +245,7 @@ describe('FetchPlugin tests', () => {
243245 start_time : 0 ,
244246 trace_id : '1-0-000000000000000000000000' ,
245247 end_time : 0 ,
248+ fault : true ,
246249 subsegments : [
247250 {
248251 id : '0000000000000000' ,
@@ -256,7 +259,7 @@ describe('FetchPlugin tests', () => {
256259 traced : true
257260 }
258261 } ,
259- error : true ,
262+ fault : true ,
260263 cause : {
261264 exceptions : [
262265 {
@@ -269,6 +272,96 @@ describe('FetchPlugin tests', () => {
269272 } ) ;
270273 } ) ;
271274
275+ test ( 'when fetch returns a 404 then segment error is true' , async ( ) => {
276+ // Init
277+ global . fetch = mockFetchWith400 ;
278+ const config : PartialHttpPluginConfig = {
279+ logicalServiceName : 'sample.rum.aws.amazon.com' ,
280+ urlsToInclude : [ / a w s \. a m a z o n \. c o m / ]
281+ } ;
282+
283+ const plugin : FetchPlugin = new FetchPlugin ( config ) ;
284+ plugin . load ( xRayOnContext ) ;
285+
286+ // Run
287+ await fetch ( URL ) ;
288+ plugin . disable ( ) ;
289+ global . fetch = mockFetch ;
290+
291+ // Assert
292+ expect ( record . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
293+ error : true ,
294+ subsegments : [
295+ {
296+ error : true ,
297+ http : {
298+ response : { status : 404 }
299+ }
300+ }
301+ ]
302+ } ) ;
303+ } ) ;
304+
305+ test ( 'when fetch returns a 429 then segment throttle is true' , async ( ) => {
306+ // Init
307+ global . fetch = mockFetchWith429 ;
308+ const config : PartialHttpPluginConfig = {
309+ logicalServiceName : 'sample.rum.aws.amazon.com' ,
310+ urlsToInclude : [ / a w s \. a m a z o n \. c o m / ]
311+ } ;
312+
313+ const plugin : FetchPlugin = new FetchPlugin ( config ) ;
314+ plugin . load ( xRayOnContext ) ;
315+
316+ // Run
317+ await fetch ( URL ) ;
318+ plugin . disable ( ) ;
319+ global . fetch = mockFetch ;
320+
321+ // Assert
322+ expect ( record . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
323+ throttle : true ,
324+ subsegments : [
325+ {
326+ throttle : true ,
327+ http : {
328+ response : { status : 429 }
329+ }
330+ }
331+ ]
332+ } ) ;
333+ } ) ;
334+
335+ test ( 'when fetch returns a 500 then segment fault is true' , async ( ) => {
336+ // Init
337+ global . fetch = mockFetchWith500 ;
338+ const config : PartialHttpPluginConfig = {
339+ logicalServiceName : 'sample.rum.aws.amazon.com' ,
340+ urlsToInclude : [ / a w s \. a m a z o n \. c o m / ]
341+ } ;
342+
343+ const plugin : FetchPlugin = new FetchPlugin ( config ) ;
344+ plugin . load ( xRayOnContext ) ;
345+
346+ // Run
347+ await fetch ( URL ) ;
348+ plugin . disable ( ) ;
349+ global . fetch = mockFetch ;
350+
351+ // Assert
352+ expect ( record . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
353+ fault : true ,
354+ subsegments : [
355+ {
356+ fault : true ,
357+ http : {
358+ response : { status : 500 }
359+ }
360+ }
361+ ]
362+ } ) ;
363+ } ) ;
364+
272365 test ( 'when plugin is disabled then the plugin does not record a trace' , async ( ) => {
273366 // Init
274367 const config : PartialHttpPluginConfig = {
0 commit comments