-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
037b81b
commit 7bc246b
Showing
2 changed files
with
53 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => { | ||
|
@@ -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'; | ||
|
@@ -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 | ||
|
@@ -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, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters