-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
364 lines (344 loc) · 12.1 KB
/
test.ts
File metadata and controls
364 lines (344 loc) · 12.1 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { afterAll, describe, expect } from 'vitest';
import { assertSentryTransaction } from '../../../utils/assertions';
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner';
describe('express tracing', () => {
afterAll(() => {
cleanupChildProcesses();
});
createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
test('should create and send transactions for Express routes and spans for middlewares.', async () => {
const runner = createRunner()
.expect({
transaction: {
contexts: {
trace: {
span_id: expect.stringMatching(/[a-f0-9]{16}/),
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
data: {
url: expect.stringMatching(/\/test\/express$/),
'http.response.status_code': 200,
},
op: 'http.server',
status: 'ok',
},
},
spans: expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'express.name': 'corsMiddleware',
'express.type': 'middleware',
}),
description: 'corsMiddleware',
op: 'middleware.express',
origin: 'auto.http.otel.express',
}),
expect.objectContaining({
data: expect.objectContaining({
'express.name': '/test/express',
'express.type': 'request_handler',
}),
description: '/test/express',
op: 'request_handler.express',
origin: 'auto.http.otel.express',
}),
]),
},
})
.start();
runner.makeRequest('get', '/test/express');
await runner.completed();
});
test('should set a correct transaction name for routes specified in RegEx', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'GET /\\/test\\/regex/',
transaction_info: {
source: 'route',
},
contexts: {
trace: {
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
data: {
url: expect.stringMatching(/\/test\/regex$/),
'http.response.status_code': 200,
},
op: 'http.server',
status: 'ok',
},
},
},
})
.start();
runner.makeRequest('get', '/test/regex');
await runner.completed();
});
test('handles root page correctly', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'GET /',
},
})
.start();
runner.makeRequest('get', '/');
await runner.completed();
});
test('ignores 404 routes by default', async () => {
const runner = createRunner()
.expect({
// No transaction is sent for the 404 route
transaction: {
transaction: 'GET /',
},
})
.start();
runner.makeRequest('get', '/does-not-exist', { expectError: true });
runner.makeRequest('get', '/');
await runner.completed();
});
test.each([['array1'], ['array5']])(
'should set a correct transaction name for routes consisting of arrays of routes for %p',
async (segment: string) => {
const runner = await createRunner()
.expect({
transaction: {
transaction: 'GET /test/array1,/\\/test\\/array[2-9]/',
transaction_info: {
source: 'route',
},
contexts: {
trace: {
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
data: {
url: expect.stringMatching(`/test/${segment}$`),
'http.response.status_code': 200,
},
op: 'http.server',
status: 'ok',
},
},
},
})
.start();
await runner.makeRequest('get', `/test/${segment}`);
await runner.completed();
},
);
test.each([
['arr/545'],
['arr/required'],
['arr/required'],
['arr/requiredPath'],
['arr/required/lastParam'],
['arr55/required/lastParam'],
['arr/requiredPath/optionalPath/'],
['arr/requiredPath/optionalPath/lastParam'],
])('should handle more complex regexes in route arrays correctly for %p', async (segment: string) => {
const runner = await createRunner()
.expect({
transaction: {
transaction: 'GET /test/arr/:id,/\\/test\\/arr[0-9]*\\/required(path)?(\\/optionalPath)?\\/(lastParam)?/',
transaction_info: {
source: 'route',
},
contexts: {
trace: {
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
data: {
url: expect.stringMatching(`/test/${segment}$`),
'http.response.status_code': 200,
},
op: 'http.server',
status: 'ok',
},
},
},
})
.start();
await runner.makeRequest('get', `/test/${segment}`);
await runner.completed();
});
describe('request data', () => {
test('correctly captures JSON request data', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'POST /test-post',
request: {
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
method: 'POST',
headers: {
'user-agent': expect.stringContaining(''),
'content-type': 'application/json',
},
data: JSON.stringify({
foo: 'bar',
other: 1,
}),
},
},
})
.start();
runner.makeRequest('post', '/test-post', {
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify({ foo: 'bar', other: 1 }),
});
await runner.completed();
});
test('correctly captures plain text request data', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'POST /test-post',
request: {
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
method: 'POST',
headers: {
'user-agent': expect.stringContaining(''),
'content-type': 'text/plain',
},
data: 'some plain text',
},
},
})
.start();
runner.makeRequest('post', '/test-post', {
headers: { 'Content-Type': 'text/plain' },
data: 'some plain text',
});
await runner.completed();
});
test('correctly captures text buffer request data', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'POST /test-post',
request: {
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
method: 'POST',
headers: {
'user-agent': expect.stringContaining(''),
'content-type': 'application/octet-stream',
},
data: 'some plain text in buffer',
},
},
})
.start();
runner.makeRequest('post', '/test-post', {
headers: { 'Content-Type': 'application/octet-stream' },
data: Buffer.from('some plain text in buffer'),
});
await runner.completed();
});
test('correctly captures non-text buffer request data', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'POST /test-post',
request: {
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
method: 'POST',
headers: {
'user-agent': expect.stringContaining(''),
'content-type': 'application/octet-stream',
},
// This is some non-ascii string representation
data: expect.any(String),
},
},
})
.start();
const body = new Uint8Array([1, 2, 3, 4, 5]).buffer;
runner.makeRequest('post', '/test-post', {
headers: { 'Content-Type': 'application/octet-stream' },
data: body,
});
await runner.completed();
});
test('correctly ignores request data', async () => {
const runner = createRunner()
.expect({
transaction: e => {
assertSentryTransaction(e, {
transaction: 'POST /test-post-ignore-body',
request: {
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post-ignore-body$/),
method: 'POST',
headers: {
'user-agent': expect.stringContaining(''),
'content-type': 'application/octet-stream',
},
},
});
// Ensure the request body has been ignored
expect(e).have.property('request').that.does.not.have.property('data');
},
})
.start();
runner.makeRequest('post', '/test-post-ignore-body', {
headers: { 'Content-Type': 'application/octet-stream' },
data: Buffer.from('some plain text in buffer'),
});
await runner.completed();
});
});
});
describe('filter status codes', () => {
createEsmAndCjsTests(
__dirname,
'scenario-filterStatusCode.mjs',
'instrument-filterStatusCode.mjs',
(createRunner, test) => {
// We opt-out of the default 404 filtering in order to test how 404 spans are handled
test('handles 404 route correctly', async () => {
const runner = createRunner()
.expect({
transaction: {
// FIXME: This is incorrect, sadly :(
transaction: 'GET /',
contexts: {
trace: {
span_id: expect.stringMatching(/[a-f0-9]{16}/),
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
data: {
'http.response.status_code': 404,
url: expect.stringMatching(/\/does-not-exist$/),
'http.method': 'GET',
'http.url': expect.stringMatching(/\/does-not-exist$/),
'http.target': '/does-not-exist',
},
op: 'http.server',
status: 'not_found',
},
},
},
})
.start();
runner.makeRequest('get', '/does-not-exist', { expectError: true });
await runner.completed();
});
test('filters defined status codes', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'GET /',
},
})
.start();
await runner.makeRequest('get', '/499', { expectError: true });
await runner.makeRequest('get', '/300', { expectError: true });
await runner.makeRequest('get', '/399', { expectError: true });
await runner.makeRequest('get', '/');
await runner.completed();
});
},
);
});
});