Skip to content

Commit

Permalink
feat(integration): add POST method test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
domharrington committed Aug 24, 2022
1 parent 037b81b commit 7bc246b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
55 changes: 49 additions & 6 deletions __tests__/integration-metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ if (!process.env.EXAMPLE_SERVER) {
process.exit(1);
}

async function getBody(response) {
let responseBody = '';
// eslint-disable-next-line no-restricted-syntax
for await (const chunk of response) {
responseBody += chunk;
}
expect(responseBody).not.toBe('');
return JSON.parse(responseBody);
}

// https://gist.github.com/krnlde/797e5e0a6f12cc9bd563123756fc101f
http.get[promisify.custom] = function getAsync(options) {
return new Promise((resolve, reject) => {
Expand All @@ -28,6 +38,22 @@ http.get[promisify.custom] = function getAsync(options) {
});
};

function post(url, body, options) {
return new Promise((resolve, reject) => {
const request = http
.request(url, { method: 'post', ...options }, response => {
response.end = new Promise(res => {
response.on('end', res);
});
resolve(response);
})
.on('error', reject);

request.write(body);
request.end();
});
}

const get = promisify(http.get);

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

let body = '';
// eslint-disable-next-line no-restricted-syntax
for await (const chunk of req) {
body += chunk;
}
body = JSON.parse(body);
const body = await getBody(req);
const [har] = body;

// Check for a uuid
Expand Down Expand Up @@ -193,4 +214,26 @@ describe('Metrics SDK Integration Tests', () => {
const responseHeaders = caseless(arrayToObject(response.headers));
expect(responseHeaders.get('content-type')).toMatch(/application\/json(;\s?charset=utf-8)?/);
});

it('should process the http POST body', async () => {
const postData = JSON.stringify({ user: { email: '[email protected]' } });
await post(`http://localhost:${PORT}/`, postData, {
headers: {
'content-type': 'application/json',
},
});

const [req] = await once(metricsServer, 'request');

const body = await getBody(req);
const [har] = body;

const { request, response } = har.request.log.entries[0];
expect(request.method).toBe('POST');
expect(response.status).toBe(200);
expect(request.postData).toStrictEqual({
mimeType: 'application/json',
text: postData,
});
});
});
4 changes: 4 additions & 0 deletions packages/node/examples/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ app.get('/', (req, res) => {
res.json({ message: 'hello world' });
});

app.post('/', express.json(), (req, res) => {
res.sendStatus(200);
});

const server = app.listen(port, 'localhost', function () {
// eslint-disable-next-line no-console
console.log('Example app listening at http://%s:%s', server.address().address, port);
Expand Down

0 comments on commit 7bc246b

Please sign in to comment.