Skip to content

Commit dcc025c

Browse files
committed
add tests for xhr/fetch sentrytrace header
1 parent 98f28d7 commit dcc025c

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

packages/tracing/test/browser/request.test.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('callbacks', () => {
6363
const fetchHandlerData: FetchData = {
6464
args: ['http://dogs.are.great/', {}],
6565
fetchData: { url: 'http://dogs.are.great/', method: 'GET' },
66-
startTimestamp: 1356996072000,
66+
startTimestamp: 2012112120121231,
6767
};
6868
const xhrHandlerData: XHRData = {
6969
xhr: {
@@ -78,18 +78,27 @@ describe('callbacks', () => {
7878
// setRequestHeader: XMLHttpRequest.prototype.setRequestHeader,
7979
setRequestHeader,
8080
},
81-
startTimestamp: 1353501072000,
81+
startTimestamp: 2012112120121231,
8282
};
83-
const endTimestamp = 1356996072000;
83+
const endTimestamp = 2013041520130908;
8484

8585
beforeAll(() => {
86-
hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
86+
hub = new Hub(
87+
new BrowserClient({
88+
dsn: 'https://[email protected]/12312012',
89+
environment: 'dogpark',
90+
release: 'off.leash.park',
91+
92+
tracesSampleRate: 1,
93+
}),
94+
);
8795
makeMain(hub);
8896
});
8997

9098
beforeEach(() => {
91-
transaction = hub.startTransaction({ name: 'organizations/users/:userid', op: 'pageload' }) as Transaction;
99+
transaction = hub.startTransaction({ name: 'meetNewDogFriend', op: 'wag.tail' }) as Transaction;
92100
hub.configureScope(scope => scope.setSpan(transaction));
101+
jest.clearAllMocks();
93102
});
94103

95104
describe('fetchCallback()', () => {
@@ -117,20 +126,17 @@ describe('callbacks', () => {
117126
expect(spans).toEqual({});
118127
});
119128

120-
it('does not add fetch request headers if tracing is disabled', () => {
129+
it('does not add tracing headers if tracing is disabled', () => {
121130
hasTracingEnabled.mockReturnValueOnce(false);
122131

123132
// make a local copy so the global one doesn't get mutated
124-
const handlerData: FetchData = {
125-
args: ['http://dogs.are.great/', {}],
126-
fetchData: { url: 'http://dogs.are.great/', method: 'GET' },
127-
startTimestamp: 1353501072000,
128-
};
133+
const handlerData = { ...fetchHandlerData };
129134

130135
fetchCallback(handlerData, alwaysCreateSpan, {});
131136

132137
const headers = (handlerData.args[1].headers as Record<string, string>) || {};
133138
expect(headers['sentry-trace']).not.toBeDefined();
139+
expect(headers['tracestate']).not.toBeDefined();
134140
});
135141

136142
it('creates and finishes fetch span on active transaction', () => {
@@ -185,8 +191,15 @@ describe('callbacks', () => {
185191
expect(newSpan!.status).toBe(SpanStatus.fromHttpCode(404));
186192
});
187193

188-
it('adds sentry-trace header to fetch requests', () => {
189-
// TODO
194+
it('adds tracing headers to fetch requests', () => {
195+
// make a local copy so the global one doesn't get mutated
196+
const handlerData = { ...fetchHandlerData };
197+
198+
fetchCallback(handlerData, alwaysCreateSpan, {});
199+
200+
const headers = (handlerData.args[1].headers as Record<string, string>) || {};
201+
expect(headers['sentry-trace']).toBeDefined();
202+
expect(headers['tracestate']).toBeDefined();
190203
});
191204
});
192205

@@ -207,21 +220,22 @@ describe('callbacks', () => {
207220
expect(spans).toEqual({});
208221
});
209222

210-
it('does not add xhr request headers if tracing is disabled', () => {
223+
it('does not add tracing headers if tracing is disabled', () => {
211224
hasTracingEnabled.mockReturnValueOnce(false);
212225

213226
xhrCallback(xhrHandlerData, alwaysCreateSpan, {});
214227

215228
expect(setRequestHeader).not.toHaveBeenCalled();
216229
});
217230

218-
it('adds sentry-trace header to XHR requests', () => {
231+
it('adds tracing headers to XHR requests', () => {
219232
xhrCallback(xhrHandlerData, alwaysCreateSpan, {});
220233

221234
expect(setRequestHeader).toHaveBeenCalledWith(
222235
'sentry-trace',
223236
expect.stringMatching(tracingUtils.TRACEPARENT_REGEXP),
224237
);
238+
expect(setRequestHeader).toHaveBeenCalledWith('tracestate', expect.any(String));
225239
});
226240

227241
it('creates and finishes XHR span on active transaction', () => {

0 commit comments

Comments
 (0)