forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.test.ts
336 lines (254 loc) · 10.5 KB
/
request.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { BrowserClient } from '@sentry/browser';
import { Hub, makeMain } from '@sentry/core';
import * as utils from '@sentry/utils';
import { Span, spanStatusfromHttpCode, Transaction } from '../../src';
import { fetchCallback, FetchData, instrumentOutgoingRequests, xhrCallback } from '../../src/browser/request';
import { addExtensionMethods } from '../../src/hubextensions';
import * as tracingUtils from '../../src/utils';
import { getDefaultBrowserClientOptions } from '../testutils';
beforeAll(() => {
addExtensionMethods();
// @ts-ignore need to override global Request because it's not in the jest environment (even with an
// `@jest-environment jsdom` directive, for some reason)
global.Request = {};
});
const hasTracingEnabled = jest.spyOn(tracingUtils, 'hasTracingEnabled');
const addInstrumentationHandler = jest.spyOn(utils, 'addInstrumentationHandler');
const setRequestHeader = jest.fn();
describe('instrumentOutgoingRequests', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('instruments fetch and xhr requests', () => {
instrumentOutgoingRequests();
expect(addInstrumentationHandler).toHaveBeenCalledWith('fetch', expect.any(Function));
expect(addInstrumentationHandler).toHaveBeenCalledWith('xhr', expect.any(Function));
});
it('does not instrument fetch requests if traceFetch is false', () => {
instrumentOutgoingRequests({ traceFetch: false });
expect(addInstrumentationHandler).not.toHaveBeenCalledWith('fetch', expect.any(Function));
});
it('does not instrument xhr requests if traceXHR is false', () => {
instrumentOutgoingRequests({ traceXHR: false });
expect(addInstrumentationHandler).not.toHaveBeenCalledWith('xhr', expect.any(Function));
});
});
describe('callbacks', () => {
let hub: Hub;
let transaction: Transaction;
const always = () => true;
const never = () => false;
const startTimestamp = 1356996072000;
const endTimestamp = 1356996072000;
const fetchHandlerData: FetchData = {
args: ['http://dogs.are.great/', {}],
fetchData: { url: 'http://dogs.are.great/', method: 'GET' },
startTimestamp,
};
const xhrHandlerData = {
xhr: {
__sentry_xhr__: {
method: 'GET',
url: 'http://dogs.are.great/',
status_code: 200,
data: {},
},
__sentry_xhr_span_id__: '1231201211212012',
// eslint-disable-next-line @typescript-eslint/unbound-method
// setRequestHeader: XMLHttpRequest.prototype.setRequestHeader,
setRequestHeader,
},
startTimestamp,
};
beforeAll(() => {
const options = getDefaultBrowserClientOptions({ tracesSampleRate: 1 });
hub = new Hub(new BrowserClient(options));
makeMain(hub);
});
beforeEach(() => {
transaction = hub.startTransaction({ name: 'organizations/users/:userid', op: 'pageload' }) as Transaction;
hub.configureScope(scope => scope.setSpan(transaction));
});
describe('fetchCallback()', () => {
it('does not create span if shouldCreateSpan returns false', () => {
const spans = {};
fetchCallback(fetchHandlerData, never, never, spans);
expect(spans).toEqual({});
});
it('does not create span if there is no fetch data in handler data', () => {
const noFetchData = { args: fetchHandlerData.args, startTimestamp: fetchHandlerData.startTimestamp };
const spans = {};
fetchCallback(noFetchData, always, never, spans);
expect(spans).toEqual({});
});
it('does not add fetch request spans if tracing is disabled', () => {
hasTracingEnabled.mockReturnValueOnce(false);
const spans = {};
fetchCallback(fetchHandlerData, always, never, spans);
expect(spans).toEqual({});
});
it('does not add fetch request headers if tracing is disabled', () => {
hasTracingEnabled.mockReturnValueOnce(false);
// make a local copy so the global one doesn't get mutated
const handlerData: FetchData = {
args: ['http://dogs.are.great/', {}],
fetchData: { url: 'http://dogs.are.great/', method: 'GET' },
startTimestamp: 1353501072000,
};
fetchCallback(handlerData, always, never, {});
const headers = (handlerData.args[1].headers as Record<string, string>) || {};
expect(headers['sentry-trace']).not.toBeDefined();
});
it('creates and finishes fetch span on active transaction', () => {
const spans = {};
// triggered by request being sent
fetchCallback(fetchHandlerData, always, never, spans);
const newSpan = transaction.spanRecorder?.spans[1] as Span;
expect(newSpan).toBeDefined();
expect(newSpan).toBeInstanceOf(Span);
expect(newSpan.data).toEqual({
method: 'GET',
type: 'fetch',
url: 'http://dogs.are.great/',
});
expect(newSpan.description).toBe('GET http://dogs.are.great/');
expect(newSpan.op).toBe('http.client');
expect(fetchHandlerData.fetchData?.__span).toBeDefined();
const postRequestFetchHandlerData = {
...fetchHandlerData,
endTimestamp,
};
// triggered by response coming back
fetchCallback(postRequestFetchHandlerData, always, never, spans);
expect(newSpan.endTimestamp).toBeDefined();
});
it('sets response status on finish', () => {
const spans: Record<string, Span> = {};
// triggered by request being sent
fetchCallback(fetchHandlerData, always, never, spans);
const newSpan = transaction.spanRecorder?.spans[1] as Span;
expect(newSpan).toBeDefined();
const postRequestFetchHandlerData = {
...fetchHandlerData,
endTimestamp,
response: { status: 404 } as Response,
};
// triggered by response coming back
fetchCallback(postRequestFetchHandlerData, always, never, spans);
expect(newSpan.status).toBe(spanStatusfromHttpCode(404));
});
it('ignores response with no associated span', () => {
// the request might be missed somehow. E.g. if it was sent before tracing gets enabled.
const postRequestFetchHandlerData = {
...fetchHandlerData,
endTimestamp,
response: { status: 404 } as Response,
};
// in that case, the response coming back will be ignored
fetchCallback(postRequestFetchHandlerData, always, never, {});
const newSpan = transaction.spanRecorder?.spans[1];
expect(newSpan).toBeUndefined();
});
it('records outgoing propogations', () => {
const firstReqData = { ...fetchHandlerData };
const secondReqData = { ...fetchHandlerData };
expect(transaction.metadata.propagations).toBe(0);
fetchCallback(firstReqData, always, always, {});
expect(transaction.metadata.propagations).toBe(1);
fetchCallback(secondReqData, always, always, {});
expect(transaction.metadata.propagations).toBe(2);
});
it('adds sentry-trace header to fetch requests', () => {
fetchCallback(fetchHandlerData, always, always, {});
expect(fetchHandlerData.args[1].headers['sentry-trace']).toBeDefined();
});
});
describe('xhrCallback()', () => {
it('does not create span if shouldCreateSpan returns false', () => {
const spans = {};
xhrCallback(xhrHandlerData, never, never, spans);
expect(spans).toEqual({});
});
it('does not add xhr request spans if tracing is disabled', () => {
hasTracingEnabled.mockReturnValueOnce(false);
const spans = {};
xhrCallback(xhrHandlerData, always, never, spans);
expect(spans).toEqual({});
});
it('does not add xhr request headers if tracing is disabled', () => {
hasTracingEnabled.mockReturnValueOnce(false);
xhrCallback(xhrHandlerData, always, always, {});
expect(setRequestHeader).not.toHaveBeenCalled();
});
it('adds sentry-trace header to XHR requests', () => {
xhrCallback(xhrHandlerData, always, always, {});
expect(setRequestHeader).toHaveBeenCalledWith(
'sentry-trace',
expect.stringMatching(tracingUtils.TRACEPARENT_REGEXP),
);
});
it('creates and finishes XHR span on active transaction', () => {
const spans = {};
// triggered by request being sent
xhrCallback(xhrHandlerData, always, never, spans);
const newSpan = transaction.spanRecorder?.spans[1] as Span;
expect(newSpan).toBeInstanceOf(Span);
expect(newSpan.data).toEqual({
method: 'GET',
type: 'xhr',
url: 'http://dogs.are.great/',
});
expect(newSpan.description).toBe('GET http://dogs.are.great/');
expect(newSpan.op).toBe('http.client');
expect(xhrHandlerData.xhr.__sentry_xhr_span_id__).toBeDefined();
expect(xhrHandlerData.xhr.__sentry_xhr_span_id__).toEqual(newSpan?.spanId);
const postRequestXHRHandlerData = {
...xhrHandlerData,
endTimestamp,
};
// triggered by response coming back
xhrCallback(postRequestXHRHandlerData, always, never, spans);
expect(newSpan.endTimestamp).toBeDefined();
});
it('sets response status on finish', () => {
const spans = {};
// triggered by request being sent
xhrCallback(xhrHandlerData, always, never, spans);
const newSpan = transaction.spanRecorder?.spans[1] as Span;
expect(newSpan).toBeDefined();
const postRequestXHRHandlerData = {
...xhrHandlerData,
endTimestamp,
};
postRequestXHRHandlerData.xhr.__sentry_xhr__.status_code = 404;
// triggered by response coming back
xhrCallback(postRequestXHRHandlerData, always, never, spans);
expect(newSpan.status).toBe(spanStatusfromHttpCode(404));
});
it('ignores response with no associated span', () => {
// the request might be missed somehow. E.g. if it was sent before tracing gets enabled.
const postRequestXHRHandlerData = {
...{
xhr: {
__sentry_xhr__: xhrHandlerData.xhr.__sentry_xhr__,
},
},
startTimestamp,
endTimestamp,
};
// in that case, the response coming back will be ignored
xhrCallback(postRequestXHRHandlerData, always, never, {});
const newSpan = transaction.spanRecorder?.spans[1];
expect(newSpan).toBeUndefined();
});
it('records outgoing propogations', () => {
const firstReqData = { ...xhrHandlerData };
const secondReqData = { ...xhrHandlerData };
expect(transaction.metadata.propagations).toBe(0);
xhrCallback(firstReqData, always, always, {});
expect(transaction.metadata.propagations).toBe(1);
xhrCallback(secondReqData, always, always, {});
expect(transaction.metadata.propagations).toBe(2);
});
});
});