From da4aec72238979eacbc100c651470383c8c6e86b Mon Sep 17 00:00:00 2001 From: louiszawadzki Date: Tue, 7 Nov 2023 10:39:22 +0100 Subject: [PATCH] Test all headers values match --- .../XHRProxy/__tests__/XHRProxy.test.ts | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/packages/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts b/packages/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts index 60e304edf..8ee50ad16 100644 --- a/packages/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts +++ b/packages/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts @@ -55,6 +55,10 @@ const flushPromises = () => new Promise(jest.requireActual('timers').setImmediate); let xhrProxy; +const hexToDecimal = (hex: string): string => { + return BigInt(`0x${hex}`).toString(10); +}; + beforeEach(() => { DdNativeRum.startResource.mockClear(); DdNativeRum.stopResource.mockClear(); @@ -426,7 +430,7 @@ describe('XHRProxy', () => { tracingSamplingRate: 100, firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([ { - match: 'something.fr', + match: 'example.com', propagatorTypes: [PropagatorType.DATADOG] }, { @@ -454,6 +458,67 @@ describe('XHRProxy', () => { expect(stateHeader).toBe(`dd=s:1;o:rum;p:${parentValue}`); }); + it('adds tracing headers with matching value when all headers are added', async () => { + // GIVEN + const method = 'GET'; + const url = 'https://api.example.com:443/v2/user'; + xhrProxy.onTrackingStart({ + tracingSamplingRate: 100, + firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([ + { + match: 'example.com', + propagatorTypes: [PropagatorType.DATADOG] + }, + { + match: 'example.com', + propagatorTypes: [PropagatorType.TRACECONTEXT] + }, + { + match: 'example.com', + propagatorTypes: [PropagatorType.B3] + }, + { + match: 'example.com', + propagatorTypes: [PropagatorType.B3MULTI] + } + ]) + }); + + // WHEN + const xhr = new XMLHttpRequestMock(); + xhr.open(method, url); + xhr.send(); + xhr.notifyResponseArrived(); + xhr.complete(200, 'ok'); + await flushPromises(); + + // THEN + const datadogTraceValue = xhr.requestHeaders[TRACE_ID_HEADER_KEY]; + const datadogParentValue = xhr.requestHeaders[PARENT_ID_HEADER_KEY]; + + const contextHeader = xhr.requestHeaders[TRACECONTEXT_HEADER_KEY]; + const traceContextValue = contextHeader.split('-')[1]; + const parentContextValue = contextHeader.split('-')[2]; + + const b3MultiTraceHeader = + xhr.requestHeaders[B3_MULTI_TRACE_ID_HEADER_KEY]; + const b3MultiParentHeader = + xhr.requestHeaders[B3_MULTI_SPAN_ID_HEADER_KEY]; + + const b3Header = xhr.requestHeaders[B3_HEADER_KEY]; + const traceB3Value = b3Header.split('-')[0]; + const parentB3Value = b3Header.split('-')[1]; + + expect(hexToDecimal(traceContextValue)).toBe(datadogTraceValue); + expect(hexToDecimal(parentContextValue)).toBe(datadogParentValue); + + expect(hexToDecimal(b3MultiTraceHeader)).toBe(datadogTraceValue); + expect(hexToDecimal(b3MultiParentHeader)).toBe(datadogParentValue); + + expect(hexToDecimal(traceB3Value)).toBe(datadogTraceValue); + expect(hexToDecimal(parentB3Value)).toBe(datadogParentValue); + }); + it('adds tracecontext request headers when the host is instrumented with tracecontext and request is sampled', async () => { // GIVEN const method = 'GET';