@@ -412,71 +412,164 @@ describe('validateAndBuildRumConfiguration', () => {
412412 } )
413413
414414 describe ( 'trackResourceHeaders' , ( ) => {
415- it ( 'defaults to empty array' , ( ) => {
416- expect ( validateAndBuildRumConfiguration ( DEFAULT_INIT_CONFIGURATION ) ! . trackResourceHeaders ) . toEqual ( [ ] )
417- } )
415+ describe ( 'disabled' , ( ) => {
416+ it ( 'defaults to empty array' , ( ) => {
417+ expect ( validateAndBuildRumConfiguration ( DEFAULT_INIT_CONFIGURATION ) ! . trackResourceHeaders ) . toEqual ( [ ] )
418+ } )
418419
419- it ( 'returns default headers when set to true' , ( ) => {
420- expect (
421- validateAndBuildRumConfiguration ( { ...DEFAULT_INIT_CONFIGURATION , trackResourceHeaders : true } ) !
422- . trackResourceHeaders
423- ) . toEqual ( DEFAULT_TRACKED_RESOURCE_HEADERS )
420+ it ( 'returns empty array when set to false' , ( ) => {
421+ expect (
422+ validateAndBuildRumConfiguration ( { ...DEFAULT_INIT_CONFIGURATION , trackResourceHeaders : false } ) !
423+ . trackResourceHeaders
424+ ) . toEqual ( [ ] )
425+ } )
424426 } )
425427
426- it ( 'returns empty array when set to false ', ( ) => {
427- expect (
428- validateAndBuildRumConfiguration ( { ... DEFAULT_INIT_CONFIGURATION , trackResourceHeaders : false } ) !
429- . trackResourceHeaders
430- ) . toEqual ( [ ] )
431- } )
428+ describe ( 'enabled with regular usage ', ( ) => {
429+ it ( 'returns default headers as MatchHeader[] when set to true' , ( ) => {
430+ const result = validateAndBuildRumConfiguration ( {
431+ ... DEFAULT_INIT_CONFIGURATION ,
432+ trackResourceHeaders : true ,
433+ } ) ! . trackResourceHeaders
432434
433- it ( 'merges user matchers with defaults when an array is provided' , ( ) => {
434- const result = validateAndBuildRumConfiguration ( {
435- ...DEFAULT_INIT_CONFIGURATION ,
436- trackResourceHeaders : [ 'x-custom-header' ] ,
437- } ) ! . trackResourceHeaders
435+ expect ( result ) . toEqual ( DEFAULT_TRACKED_RESOURCE_HEADERS . map ( ( name ) => ( { name } ) ) )
436+ } )
438437
439- expect ( result ) . toEqual ( [ ...DEFAULT_TRACKED_RESOURCE_HEADERS , 'x-custom-header' ] )
440- } )
438+ it ( 'accepts a MatchHeader with only name' , ( ) => {
439+ const result = validateAndBuildRumConfiguration ( {
440+ ...DEFAULT_INIT_CONFIGURATION ,
441+ trackResourceHeaders : [ { name : 'x-custom' } ] ,
442+ } ) ! . trackResourceHeaders
441443
442- it ( 'lowercases string matchers from user input' , ( ) => {
443- const result = validateAndBuildRumConfiguration ( {
444- ...DEFAULT_INIT_CONFIGURATION ,
445- trackResourceHeaders : [ 'X-Custom-Header' ] ,
446- } ) ! . trackResourceHeaders
444+ expect ( result ) . toEqual ( [ { name : 'x-custom' } ] )
445+ } )
447446
448- expect ( result ) . toContain ( 'x-custom-header' )
449- } )
447+ it ( 'lowercases string name' , ( ) => {
448+ const result = validateAndBuildRumConfiguration ( {
449+ ...DEFAULT_INIT_CONFIGURATION ,
450+ trackResourceHeaders : [ { name : 'X-Custom-Header' } ] ,
451+ } ) ! . trackResourceHeaders
450452
451- it ( 'accepts RegExp and function matchers' , ( ) => {
452- const regexpMatcher = / x - c u s t o m - .* / i
453- const fnMatcher = ( header : string ) => header . startsWith ( 'x-' )
453+ expect ( result [ 0 ] . name ) . toBe ( 'x-custom-header' )
454+ } )
454455
455- const result = validateAndBuildRumConfiguration ( {
456- ...DEFAULT_INIT_CONFIGURATION ,
457- trackResourceHeaders : [ regexpMatcher , fnMatcher ] ,
458- } ) ! . trackResourceHeaders
456+ it ( 'accepts RegExp and function name matchers' , ( ) => {
457+ const regexpMatcher = / x - c u s t o m - .* / i
458+ const fnMatcher = ( header : string ) => header . startsWith ( 'x-' )
459459
460- expect ( result ) . toContain ( regexpMatcher )
461- expect ( result ) . toContain ( fnMatcher )
462- } )
460+ const result = validateAndBuildRumConfiguration ( {
461+ ...DEFAULT_INIT_CONFIGURATION ,
462+ trackResourceHeaders : [ { name : regexpMatcher } , { name : fnMatcher } ] ,
463+ } ) ! . trackResourceHeaders
463464
464- it ( 'warns and returns empty array for invalid value' , ( ) => {
465- expect (
466- validateAndBuildRumConfiguration ( { ...DEFAULT_INIT_CONFIGURATION , trackResourceHeaders : 42 as any } ) !
467- . trackResourceHeaders
468- ) . toEqual ( [ ] )
469- expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith ( 'trackResourceHeaders should be true or an array of MatchOption' )
465+ expect ( result ) . toEqual ( [ { name : regexpMatcher } , { name : fnMatcher } ] )
466+ } )
467+
468+ it ( 'preserves url' , ( ) => {
469+ const result = validateAndBuildRumConfiguration ( {
470+ ...DEFAULT_INIT_CONFIGURATION ,
471+ trackResourceHeaders : [ { url : / a p i / , name : 'x-id' } ] ,
472+ } ) ! . trackResourceHeaders
473+
474+ expect ( result ) . toEqual ( [ { url : / a p i / , name : 'x-id' } ] )
475+ } )
476+
477+ it ( 'preserves location' , ( ) => {
478+ const result = validateAndBuildRumConfiguration ( {
479+ ...DEFAULT_INIT_CONFIGURATION ,
480+ trackResourceHeaders : [ { name : 'x-id' , location : 'response' } ] ,
481+ } ) ! . trackResourceHeaders
482+
483+ expect ( result ) . toEqual ( [ { name : 'x-id' , location : 'response' } ] )
484+ } )
485+
486+ it ( 'preserves extractor' , ( ) => {
487+ const extractor = / m a x - a g e = ( \d + ) /
488+ const result = validateAndBuildRumConfiguration ( {
489+ ...DEFAULT_INIT_CONFIGURATION ,
490+ trackResourceHeaders : [ { name : 'cache-control' , extractor } ] ,
491+ } ) ! . trackResourceHeaders
492+
493+ expect ( result ) . toEqual ( [ { name : 'cache-control' , extractor } ] )
494+ } )
470495 } )
471496
472- it ( 'warns and filters invalid items in array' , ( ) => {
473- const result = validateAndBuildRumConfiguration ( {
474- ...DEFAULT_INIT_CONFIGURATION ,
475- trackResourceHeaders : [ 'valid-header' , 42 as any , 'another-valid' ] as any ,
476- } ) ! . trackResourceHeaders
497+ describe ( 'enabled with incorrect usage' , ( ) => {
498+ it ( 'warns and returns empty array for invalid value' , ( ) => {
499+ expect (
500+ validateAndBuildRumConfiguration ( { ...DEFAULT_INIT_CONFIGURATION , trackResourceHeaders : 42 as any } ) !
501+ . trackResourceHeaders
502+ ) . toEqual ( [ ] )
503+ expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith (
504+ 'trackResourceHeaders should be true or an array of MatchHeader'
505+ )
506+ } )
507+
508+ it ( 'warns and returns empty array when set to an empty array' , ( ) => {
509+ expect (
510+ validateAndBuildRumConfiguration ( { ...DEFAULT_INIT_CONFIGURATION , trackResourceHeaders : [ ] } ) !
511+ . trackResourceHeaders
512+ ) . toEqual ( [ ] )
513+ expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith (
514+ 'trackResourceHeaders is an empty array, no headers will be captured'
515+ )
516+ } )
517+
518+ it ( 'keeps valid items and skips invalid ones' , ( ) => {
519+ const result = validateAndBuildRumConfiguration ( {
520+ ...DEFAULT_INIT_CONFIGURATION ,
521+ trackResourceHeaders : [ { name : 'x-valid' } , 42 as any , { name : 'x-also-valid' } ] ,
522+ } ) ! . trackResourceHeaders
523+
524+ expect ( result ) . toEqual ( [ { name : 'x-valid' } , { name : 'x-also-valid' } ] )
525+ expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith (
526+ "trackResourceHeaders[1] should be a MatchHeader object with a 'name' property"
527+ )
528+ } )
529+
530+ it ( 'warns and skips item without name' , ( ) => {
531+ const result = validateAndBuildRumConfiguration ( {
532+ ...DEFAULT_INIT_CONFIGURATION ,
533+ trackResourceHeaders : [ { url : 'https://example.com' } as any ] ,
534+ } ) ! . trackResourceHeaders
477535
478- expect ( result ) . toEqual ( [ ...DEFAULT_TRACKED_RESOURCE_HEADERS , 'valid-header' , 'another-valid' ] )
479- expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith ( 'trackResourceHeaders[1] should be a string, RegExp, or function' )
536+ expect ( result ) . toEqual ( [ ] )
537+ expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith (
538+ "trackResourceHeaders[0] should be a MatchHeader object with a 'name' property"
539+ )
540+ } )
541+
542+ it ( 'warns and skips item with invalid url' , ( ) => {
543+ const result = validateAndBuildRumConfiguration ( {
544+ ...DEFAULT_INIT_CONFIGURATION ,
545+ trackResourceHeaders : [ { name : 'x-foo' , url : 42 as any } ] ,
546+ } ) ! . trackResourceHeaders
547+
548+ expect ( result ) . toEqual ( [ ] )
549+ expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith ( 'trackResourceHeaders[0].url should be a MatchOption' )
550+ } )
551+
552+ it ( 'warns and skips item with invalid location' , ( ) => {
553+ const result = validateAndBuildRumConfiguration ( {
554+ ...DEFAULT_INIT_CONFIGURATION ,
555+ trackResourceHeaders : [ { name : 'x-foo' , location : 'invalid' as any } ] ,
556+ } ) ! . trackResourceHeaders
557+
558+ expect ( result ) . toEqual ( [ ] )
559+ expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith (
560+ "trackResourceHeaders[0].location should be 'request', 'response', or 'any'"
561+ )
562+ } )
563+
564+ it ( 'warns and skips item with invalid extractor' , ( ) => {
565+ const result = validateAndBuildRumConfiguration ( {
566+ ...DEFAULT_INIT_CONFIGURATION ,
567+ trackResourceHeaders : [ { name : 'x-foo' , extractor : 'bad' as any } ] ,
568+ } ) ! . trackResourceHeaders
569+
570+ expect ( result ) . toEqual ( [ ] )
571+ expect ( displayWarnSpy ) . toHaveBeenCalledOnceWith ( 'trackResourceHeaders[0].extractor should be a RegExp' )
572+ } )
480573 } )
481574 } )
482575
0 commit comments