Skip to content

Commit

Permalink
Test all headers values match
Browse files Browse the repository at this point in the history
  • Loading branch information
louiszawadzki committed Nov 7, 2023
1 parent f05b1a2 commit 47c7fbd
Showing 1 changed file with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ const flushPromises = () =>
new Promise(jest.requireActual('timers').setImmediate);
let xhrProxy;

const hexToDecimal = (hex: string): string => {
return BigInt(`0x${hex}`).toString(10);
// const decimal = BigInt(`0x${hex}`).toString(10);
// const pad = '0'.repeat(length - decimal.length);
// return `${pad}${decimal}`;
};

beforeEach(() => {
DdNativeRum.startResource.mockClear();
DdNativeRum.stopResource.mockClear();
Expand Down Expand Up @@ -426,7 +433,7 @@ describe('XHRProxy', () => {
tracingSamplingRate: 100,
firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([
{
match: 'something.fr',
match: 'example.com',
propagatorTypes: [PropagatorType.DATADOG]
},
{
Expand Down Expand Up @@ -454,6 +461,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';
Expand Down

0 comments on commit 47c7fbd

Please sign in to comment.