Skip to content

Commit d7b1e87

Browse files
committed
add request tests for xhrs
1 parent b1e1c78 commit d7b1e87

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

packages/tracing/src/browser/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export interface XHRData {
6464
data: Record<string, any>;
6565
};
6666
__sentry_xhr_span_id__?: string;
67-
__sentry_own_request__: boolean;
6867
setRequestHeader?: (key: string, val: string) => void;
68+
__sentry_own_request__?: boolean;
6969
};
7070
startTimestamp: number;
7171
endTimestamp?: number;

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

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ import { Hub, makeMain } from '@sentry/hub';
33
import * as utils from '@sentry/utils';
44

55
import { Span, SpanStatus, Transaction } from '../../src';
6-
import { fetchCallback, FetchData, registerRequestInstrumentation } from '../../src/browser/request';
6+
import {
7+
fetchCallback,
8+
FetchData,
9+
registerRequestInstrumentation,
10+
xhrCallback,
11+
XHRData,
12+
} from '../../src/browser/request';
713
import { addExtensionMethods } from '../../src/hubextensions';
14+
import * as tracingUtils from '../../src/utils';
815

916
beforeAll(() => {
1017
addExtensionMethods();
@@ -14,6 +21,7 @@ beforeAll(() => {
1421
});
1522

1623
const addInstrumentationHandler = jest.spyOn(utils, 'addInstrumentationHandler');
24+
const setRequestHeader = jest.fn();
1725

1826
describe('registerRequestInstrumentation', () => {
1927
beforeEach(() => {
@@ -56,6 +64,21 @@ describe('callbacks', () => {
5664
fetchData: { url: 'http://dogs.are.great/', method: 'GET' },
5765
startTimestamp: 1356996072000,
5866
};
67+
const xhrHandlerData: XHRData = {
68+
xhr: {
69+
__sentry_xhr__: {
70+
method: 'GET',
71+
url: 'http://dogs.are.great/',
72+
status_code: 200,
73+
data: {},
74+
},
75+
__sentry_xhr_span_id__: '1231201211212012',
76+
// eslint-disable-next-line @typescript-eslint/unbound-method
77+
// setRequestHeader: XMLHttpRequest.prototype.setRequestHeader,
78+
setRequestHeader,
79+
},
80+
startTimestamp: 1353501072000,
81+
};
5982
const endTimestamp = 1356996072000;
6083

6184
beforeAll(() => {
@@ -142,4 +165,76 @@ describe('callbacks', () => {
142165
// TODO
143166
});
144167
});
168+
169+
describe('xhrCallback()', () => {
170+
it('does not create span if shouldCreateSpan returns false', () => {
171+
const spans = {};
172+
173+
xhrCallback(xhrHandlerData, neverCreateSpan, spans);
174+
175+
expect(spans).toEqual({});
176+
});
177+
178+
it('adds sentry-trace header to XHR requests', () => {
179+
xhrCallback(xhrHandlerData, alwaysCreateSpan, {});
180+
181+
expect(setRequestHeader).toHaveBeenCalledWith(
182+
'sentry-trace',
183+
expect.stringMatching(tracingUtils.TRACEPARENT_REGEXP),
184+
);
185+
});
186+
187+
it('creates and finishes XHR span on active transaction', () => {
188+
const spans = {};
189+
190+
// triggered by request being sent
191+
xhrCallback(xhrHandlerData, alwaysCreateSpan, spans);
192+
193+
const newSpan = transaction.spanRecorder?.spans[1];
194+
195+
expect(newSpan).toBeDefined();
196+
expect(newSpan).toBeInstanceOf(Span);
197+
expect(newSpan!.data).toEqual({
198+
method: 'GET',
199+
type: 'xhr',
200+
url: 'http://dogs.are.great/',
201+
});
202+
expect(newSpan!.description).toBe('GET http://dogs.are.great/');
203+
expect(newSpan!.op).toBe('http');
204+
expect(xhrHandlerData.xhr!.__sentry_xhr_span_id__).toBeDefined();
205+
expect(xhrHandlerData.xhr!.__sentry_xhr_span_id__).toEqual(newSpan?.spanId);
206+
207+
const postRequestXHRHandlerData = {
208+
...xhrHandlerData,
209+
endTimestamp,
210+
};
211+
212+
// triggered by response coming back
213+
xhrCallback(postRequestXHRHandlerData, alwaysCreateSpan, spans);
214+
215+
expect(newSpan!.endTimestamp).toBeDefined();
216+
});
217+
218+
it('sets response status on finish', () => {
219+
const spans = {};
220+
221+
// triggered by request being sent
222+
xhrCallback(xhrHandlerData, alwaysCreateSpan, spans);
223+
224+
const newSpan = transaction.spanRecorder?.spans[1];
225+
226+
expect(newSpan).toBeDefined();
227+
228+
const postRequestXHRHandlerData = {
229+
...xhrHandlerData,
230+
endTimestamp,
231+
};
232+
postRequestXHRHandlerData.xhr!.__sentry_xhr__!.status_code = 404;
233+
234+
// triggered by response coming back
235+
xhrCallback(postRequestXHRHandlerData, alwaysCreateSpan, spans);
236+
237+
expect(newSpan!.status).toBe(SpanStatus.fromHttpCode(404));
238+
});
239+
});
145240
});

0 commit comments

Comments
 (0)