Skip to content

Commit 00d5a6b

Browse files
committed
add integration test
1 parent cca060f commit 00d5a6b

File tree

6 files changed

+252
-20
lines changed

6 files changed

+252
-20
lines changed

dev-packages/node-integration-tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@types/pg": "^8.6.5",
4141
"apollo-server": "^3.11.1",
4242
"axios": "^1.6.7",
43+
"body-parser": "^1.20.3",
4344
"connect": "^3.7.0",
4445
"cors": "^2.8.5",
4546
"cron": "^3.1.6",

dev-packages/node-integration-tests/suites/express/tracing/server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ Sentry.init({
1313
// express must be required after Sentry is initialized
1414
const express = require('express');
1515
const cors = require('cors');
16+
const bodyParser = require('body-parser');
1617
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests');
1718

1819
const app = express();
1920

2021
app.use(cors());
22+
app.use(bodyParser.json());
23+
app.use(bodyParser.text());
24+
app.use(bodyParser.raw());
2125

2226
app.get('/test/express', (_req, res) => {
2327
res.send({ response: 'response 1' });
@@ -35,6 +39,10 @@ app.get(['/test/arr/:id', /\/test\/arr[0-9]*\/required(path)?(\/optionalPath)?\/
3539
res.send({ response: 'response 4' });
3640
});
3741

42+
app.post('/test-post', function (req, res) {
43+
res.send({ status: 'ok', body: req.body });
44+
});
45+
3846
Sentry.setupExpressErrorHandler(app);
3947

4048
startExpressServerAndSendPortToRunner(app);

dev-packages/node-integration-tests/suites/express/tracing/test.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,109 @@ describe('express tracing', () => {
137137
.start(done)
138138
.makeRequest('get', `/test/${segment}`);
139139
}) as any);
140+
141+
describe('request data', () => {
142+
test('correctly captures JSON request data', done => {
143+
const runner = createRunner(__dirname, 'server.js')
144+
.expect({
145+
transaction: {
146+
transaction: 'POST /test-post',
147+
request: {
148+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
149+
method: 'POST',
150+
headers: {
151+
'user-agent': expect.stringContaining(''),
152+
'content-type': 'application/json',
153+
},
154+
data: JSON.stringify({
155+
foo: 'bar',
156+
other: 1,
157+
}),
158+
},
159+
},
160+
})
161+
.start(done);
162+
163+
runner.makeRequest('post', '/test-post', {}, { foo: 'bar', other: 1 });
164+
});
165+
166+
test('correctly captures plain text request data', done => {
167+
const runner = createRunner(__dirname, 'server.js')
168+
.expect({
169+
transaction: {
170+
transaction: 'POST /test-post',
171+
request: {
172+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
173+
method: 'POST',
174+
headers: {
175+
'user-agent': expect.stringContaining(''),
176+
'content-type': 'text/plain',
177+
},
178+
data: 'some plain text',
179+
},
180+
},
181+
})
182+
.start(done);
183+
184+
runner.makeRequest(
185+
'post',
186+
'/test-post',
187+
{
188+
'Content-Type': 'text/plain',
189+
},
190+
'some plain text',
191+
);
192+
});
193+
194+
test('correctly captures text buffer request data', done => {
195+
const runner = createRunner(__dirname, 'server.js')
196+
.expect({
197+
transaction: {
198+
transaction: 'POST /test-post',
199+
request: {
200+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
201+
method: 'POST',
202+
headers: {
203+
'user-agent': expect.stringContaining(''),
204+
'content-type': 'application/octet-stream',
205+
},
206+
data: 'some plain text in buffer',
207+
},
208+
},
209+
})
210+
.start(done);
211+
212+
runner.makeRequest(
213+
'post',
214+
'/test-post',
215+
{ 'Content-Type': 'application/octet-stream' },
216+
Buffer.from('some plain text in buffer'),
217+
);
218+
});
219+
220+
test('correctly captures non-text buffer request data', done => {
221+
const runner = createRunner(__dirname, 'server.js')
222+
.expect({
223+
transaction: {
224+
transaction: 'POST /test-post',
225+
request: {
226+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
227+
method: 'POST',
228+
headers: {
229+
'user-agent': expect.stringContaining(''),
230+
'content-type': 'application/octet-stream',
231+
},
232+
// This is some non-ascii string representation
233+
data: expect.any(String),
234+
},
235+
},
236+
})
237+
.start(done);
238+
239+
const body = new Uint8Array([1, 2, 3, 4, 5]).buffer;
240+
241+
runner.makeRequest('post', '/test-post', { 'Content-Type': 'application/octet-stream' }, body);
242+
});
243+
});
140244
});
141245
});

dev-packages/node-integration-tests/suites/express/without-tracing/server.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ Sentry.init({
88
});
99

1010
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
11+
import bodyParser from 'body-parser';
1112
import express from 'express';
1213

1314
const app = express();
1415

16+
app.use(bodyParser.json());
17+
app.use(bodyParser.text());
18+
app.use(bodyParser.raw());
19+
1520
Sentry.setTag('global', 'tag');
1621

1722
app.get('/test/isolationScope/:id', (req, res) => {
@@ -24,6 +29,12 @@ app.get('/test/isolationScope/:id', (req, res) => {
2429
res.send({});
2530
});
2631

32+
app.post('/test-post', function (req, res) {
33+
Sentry.captureException(new Error('This is an exception'));
34+
35+
res.send({ status: 'ok', body: req.body });
36+
});
37+
2738
Sentry.setupExpressErrorHandler(app);
2839

2940
startExpressServerAndSendPortToRunner(app);

dev-packages/node-integration-tests/suites/express/without-tracing/test.ts

Lines changed: 125 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,133 @@ afterAll(() => {
44
cleanupChildProcesses();
55
});
66

7-
test('correctly applies isolation scope even without tracing', done => {
8-
const runner = createRunner(__dirname, 'server.ts')
9-
.expect({
10-
event: {
11-
transaction: 'GET /test/isolationScope/1',
12-
tags: {
13-
global: 'tag',
14-
'isolation-scope': 'tag',
15-
'isolation-scope-1': '1',
7+
describe('express without tracing', () => {
8+
test('correctly applies isolation scope even without tracing', done => {
9+
const runner = createRunner(__dirname, 'server.ts')
10+
.expect({
11+
event: {
12+
transaction: 'GET /test/isolationScope/1',
13+
tags: {
14+
global: 'tag',
15+
'isolation-scope': 'tag',
16+
'isolation-scope-1': '1',
17+
},
18+
// Request is correctly set
19+
request: {
20+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test\/isolationScope\/1$/),
21+
method: 'GET',
22+
headers: {
23+
'user-agent': expect.stringContaining(''),
24+
},
25+
},
1626
},
17-
// Request is correctly set
18-
request: {
19-
url: expect.stringContaining('/test/isolationScope/1'),
20-
headers: {
21-
'user-agent': expect.stringContaining(''),
27+
})
28+
.start(done);
29+
30+
runner.makeRequest('get', '/test/isolationScope/1');
31+
});
32+
33+
describe('request data', () => {
34+
test('correctly captures JSON request data', done => {
35+
const runner = createRunner(__dirname, 'server.ts')
36+
.expect({
37+
event: {
38+
transaction: 'POST /test-post',
39+
request: {
40+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
41+
method: 'POST',
42+
headers: {
43+
'user-agent': expect.stringContaining(''),
44+
'content-type': 'application/json',
45+
},
46+
data: JSON.stringify({
47+
foo: 'bar',
48+
other: 1,
49+
}),
50+
},
51+
},
52+
})
53+
.start(done);
54+
55+
runner.makeRequest('post', '/test-post', {}, { foo: 'bar', other: 1 });
56+
});
57+
58+
test('correctly captures plain text request data', done => {
59+
const runner = createRunner(__dirname, 'server.ts')
60+
.expect({
61+
event: {
62+
transaction: 'POST /test-post',
63+
request: {
64+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
65+
method: 'POST',
66+
headers: {
67+
'user-agent': expect.stringContaining(''),
68+
'content-type': 'text/plain',
69+
},
70+
data: 'some plain text',
71+
},
2272
},
73+
})
74+
.start(done);
75+
76+
runner.makeRequest(
77+
'post',
78+
'/test-post',
79+
{
80+
'Content-Type': 'text/plain',
2381
},
24-
},
25-
})
26-
.start(done);
82+
'some plain text',
83+
);
84+
});
85+
86+
test('correctly captures text buffer request data', done => {
87+
const runner = createRunner(__dirname, 'server.ts')
88+
.expect({
89+
event: {
90+
transaction: 'POST /test-post',
91+
request: {
92+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
93+
method: 'POST',
94+
headers: {
95+
'user-agent': expect.stringContaining(''),
96+
'content-type': 'application/octet-stream',
97+
},
98+
data: 'some plain text in buffer',
99+
},
100+
},
101+
})
102+
.start(done);
103+
104+
runner.makeRequest(
105+
'post',
106+
'/test-post',
107+
{ 'Content-Type': 'application/octet-stream' },
108+
Buffer.from('some plain text in buffer'),
109+
);
110+
});
111+
112+
test('correctly captures non-text buffer request data', done => {
113+
const runner = createRunner(__dirname, 'server.ts')
114+
.expect({
115+
event: {
116+
transaction: 'POST /test-post',
117+
request: {
118+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
119+
method: 'POST',
120+
headers: {
121+
'user-agent': expect.stringContaining(''),
122+
'content-type': 'application/octet-stream',
123+
},
124+
// This is some non-ascii string representation
125+
data: expect.any(String),
126+
},
127+
},
128+
})
129+
.start(done);
130+
131+
const body = new Uint8Array([1, 2, 3, 4, 5]).buffer;
27132

28-
runner.makeRequest('get', '/test/isolationScope/1');
133+
runner.makeRequest('post', '/test-post', { 'Content-Type': 'application/octet-stream' }, body);
134+
});
135+
});
29136
});

dev-packages/node-integration-tests/utils/runner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ export function createRunner(...paths: string[]) {
483483
method: 'get' | 'post',
484484
path: string,
485485
headers: Record<string, string> = {},
486+
data?: unknown,
486487
): Promise<T | undefined> {
487488
try {
488489
await waitFor(() => scenarioServerPort !== undefined);
@@ -497,7 +498,7 @@ export function createRunner(...paths: string[]) {
497498
if (method === 'get') {
498499
await axios.get(url, { headers });
499500
} else {
500-
await axios.post(url, { headers });
501+
await axios.post(url, data, { headers });
501502
}
502503
} catch (e) {
503504
return;
@@ -506,7 +507,7 @@ export function createRunner(...paths: string[]) {
506507
} else if (method === 'get') {
507508
return (await axios.get(url, { headers })).data;
508509
} else {
509-
return (await axios.post(url, { headers })).data;
510+
return (await axios.post(url, data, { headers })).data;
510511
}
511512
},
512513
};

0 commit comments

Comments
 (0)