Skip to content

Commit 7bc246b

Browse files
committed
feat(integration): add POST method test
Right now the hapi/fastify examples are not working properly for accepting an incoming POST body. This adds a test to expose that flaw, but does not yet fix it. I think this refactor makes the most sense to finish once #459 is merged in.
1 parent 037b81b commit 7bc246b

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

__tests__/integration-metrics.test.js

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ if (!process.env.EXAMPLE_SERVER) {
1414
process.exit(1);
1515
}
1616

17+
async function getBody(response) {
18+
let responseBody = '';
19+
// eslint-disable-next-line no-restricted-syntax
20+
for await (const chunk of response) {
21+
responseBody += chunk;
22+
}
23+
expect(responseBody).not.toBe('');
24+
return JSON.parse(responseBody);
25+
}
26+
1727
// https://gist.github.com/krnlde/797e5e0a6f12cc9bd563123756fc101f
1828
http.get[promisify.custom] = function getAsync(options) {
1929
return new Promise((resolve, reject) => {
@@ -28,6 +38,22 @@ http.get[promisify.custom] = function getAsync(options) {
2838
});
2939
};
3040

41+
function post(url, body, options) {
42+
return new Promise((resolve, reject) => {
43+
const request = http
44+
.request(url, { method: 'post', ...options }, response => {
45+
response.end = new Promise(res => {
46+
response.on('end', res);
47+
});
48+
resolve(response);
49+
})
50+
.on('error', reject);
51+
52+
request.write(body);
53+
request.end();
54+
});
55+
}
56+
3157
const get = promisify(http.get);
3258

3359
const randomApiKey = 'a-random-readme-api-key';
@@ -142,12 +168,7 @@ describe('Metrics SDK Integration Tests', () => {
142168
expect(req.url).toBe('/v1/request');
143169
expect(req.headers.authorization).toBe('Basic YS1yYW5kb20tcmVhZG1lLWFwaS1rZXk6');
144170

145-
let body = '';
146-
// eslint-disable-next-line no-restricted-syntax
147-
for await (const chunk of req) {
148-
body += chunk;
149-
}
150-
body = JSON.parse(body);
171+
const body = await getBody(req);
151172
const [har] = body;
152173

153174
// Check for a uuid
@@ -193,4 +214,26 @@ describe('Metrics SDK Integration Tests', () => {
193214
const responseHeaders = caseless(arrayToObject(response.headers));
194215
expect(responseHeaders.get('content-type')).toMatch(/application\/json(;\s?charset=utf-8)?/);
195216
});
217+
218+
it('should process the http POST body', async () => {
219+
const postData = JSON.stringify({ user: { email: '[email protected]' } });
220+
await post(`http://localhost:${PORT}/`, postData, {
221+
headers: {
222+
'content-type': 'application/json',
223+
},
224+
});
225+
226+
const [req] = await once(metricsServer, 'request');
227+
228+
const body = await getBody(req);
229+
const [har] = body;
230+
231+
const { request, response } = har.request.log.entries[0];
232+
expect(request.method).toBe('POST');
233+
expect(response.status).toBe(200);
234+
expect(request.postData).toStrictEqual({
235+
mimeType: 'application/json',
236+
text: postData,
237+
});
238+
});
196239
});

packages/node/examples/express/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ app.get('/', (req, res) => {
2828
res.json({ message: 'hello world' });
2929
});
3030

31+
app.post('/', express.json(), (req, res) => {
32+
res.sendStatus(200);
33+
});
34+
3135
const server = app.listen(port, 'localhost', function () {
3236
// eslint-disable-next-line no-console
3337
console.log('Example app listening at http://%s:%s', server.address().address, port);

0 commit comments

Comments
 (0)