Skip to content

Commit c60142d

Browse files
authored
test(fastify): Add test for request body parsing in fastify (#16107)
Adding test for #16090
1 parent de8072b commit c60142d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

dev-packages/e2e-tests/test-applications/node-fastify/src/app.ts

+4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ app.get('/test-outgoing-http-external-disallowed', async function (req, res) {
119119
res.send(data);
120120
});
121121

122+
app.post('/test-post', function (req, res) {
123+
res.send({ status: 'ok', body: req.body });
124+
});
125+
122126
app.listen({ port: port });
123127

124128
// A second app so we can test header propagation between external URLs

dev-packages/e2e-tests/test-applications/node-fastify/tests/transactions.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,40 @@ test('Sends an API route transaction', async ({ baseURL }) => {
124124
origin: 'manual',
125125
});
126126
});
127+
128+
test('Captures request metadata', async ({ baseURL }) => {
129+
const transactionEventPromise = waitForTransaction('node-fastify', transactionEvent => {
130+
return (
131+
transactionEvent?.contexts?.trace?.op === 'http.server' && transactionEvent?.transaction === 'POST /test-post'
132+
);
133+
});
134+
135+
const res = await fetch(`${baseURL}/test-post`, {
136+
method: 'POST',
137+
body: JSON.stringify({ foo: 'bar', other: 1 }),
138+
headers: {
139+
'Content-Type': 'application/json',
140+
},
141+
});
142+
const resBody = await res.json();
143+
144+
expect(resBody).toEqual({ status: 'ok', body: { foo: 'bar', other: 1 } });
145+
146+
const transactionEvent = await transactionEventPromise;
147+
148+
expect(transactionEvent.request).toEqual({
149+
cookies: {},
150+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
151+
method: 'POST',
152+
headers: expect.objectContaining({
153+
'user-agent': expect.stringContaining(''),
154+
'content-type': 'application/json',
155+
}),
156+
data: JSON.stringify({
157+
foo: 'bar',
158+
other: 1,
159+
}),
160+
});
161+
162+
expect(transactionEvent.user).toEqual(undefined);
163+
});

0 commit comments

Comments
 (0)