import type { RumResourceEvent } from '@datadog/browser-rum'; import { type Mutable } from '../__mocks__'; import { handleResource } from '../handle-resource'; describe(`[datadog-rum] ${handleResource.name}`, () => { const cfRay = 'ray-1'; const headers = new Map([['cf-ray', cfRay]]); let event: Mutable; let context: any; function makeResourceEvent(type: 'fetch' | 'xhr', url: string) { return { type: 'resource', resource: { type, url }, } as unknown as Mutable; } const subject = () => handleResource(event, context); function itAddsCfRayToContext() { test('adds the cf-ray value to event context', () => { subject(); expect(event.context?.cfRay).toBe(cfRay); }); } function itDoesNotAddCfRay() { test('does not add a cf-ray value', () => { subject(); expect(event.context?.cfRay).toBeUndefined(); }); } describe('with a "fetch" resource', () => { beforeEach(() => { event = makeResourceEvent('fetch', window.location.toString()); context = { response: { headers } }; }); itAddsCfRayToContext(); describe('when the request targets a different domain', () => { beforeEach(() => (event = makeResourceEvent('fetch', 'https://example.com/'))); itDoesNotAddCfRay(); }); describe('without a cf-ray response header', () => { beforeEach(() => (context = { response: { headers: new Map() } })); itDoesNotAddCfRay(); }); describe('with pre-existing event context', () => { beforeEach(() => (event.context = { existing: 'keep' })); test('merges the cf-ray value without dropping existing keys', () => { subject(); expect(event.context).toEqual({ existing: 'keep', cfRay }); }); }); }); describe('with an "xhr" resource', () => { beforeEach(() => { event = makeResourceEvent('xhr', window.location.toString()); context = { xhr: { getResponseHeader: (key: string) => headers.get(key) } }; }); itAddsCfRayToContext(); }); });