Skip to content

Commit 16aae67

Browse files
authored
fix: don't stringify empty post bodies (#79)
* fix: don't stringify empty post bodies * fix: add check for req.body and fix some tests
1 parent 05cca0d commit 16aae67

4 files changed

+28
-12
lines changed

lib/process-request.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ module.exports = (req, options = {}) => {
1616
req.headers = removeOtherProperties(req.headers, options.whitelist);
1717
}
1818

19-
// parse mimetype from content-type header, default to JSON
20-
const postData = { mimeType: 'application/json' };
21-
try {
22-
postData.mimeType = contentType.parse(req).type;
23-
} catch (e) {} // eslint-disable-line no-empty
19+
const postData = {};
20+
if (req.body && Object.keys(req.body).length > 0) {
21+
// parse mimetype from content-type header, default to JSON
22+
postData.mimeType = 'application/json';
23+
try {
24+
postData.mimeType = contentType.parse(req).type;
25+
} catch (e) {} // eslint-disable-line no-empty
2426

25-
// Per HAR, we send JSON as postData.text, not params.
26-
if (postData.mimeType === 'application/json') {
27-
postData.text = JSON.stringify(req.body);
28-
} else {
29-
postData.params = objectToArray(req.body || {});
27+
// Per HAR, we send JSON as postData.text, not params.
28+
if (postData.mimeType === 'application/json') {
29+
postData.text = JSON.stringify(req.body);
30+
} else {
31+
postData.params = objectToArray(req.body || {});
32+
}
3033
}
3134

3235
return {

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/construct-payload.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('constructPayload()', () => {
3030
.expect(({ body }) => {
3131
expect(typeof body.request.log.entries[0].request).toBe('object');
3232
expect(typeof body.request.log.entries[0].response).toBe('object');
33-
expect(body.request.log.entries[0].request.postData.text).toBe('{}');
33+
expect(body.request.log.entries[0].request.postData).toStrictEqual({});
3434
});
3535
}, 8000);
3636

test/process-request.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ function createApp(options) {
1515

1616
app.use('/test-base-path', router);
1717

18+
app.get('/*', (req, res) => res.json(processRequest(req, options)));
19+
1820
app.post('/*', (req, res) => {
1921
res.json(processRequest(req, options));
2022
});
@@ -257,8 +259,19 @@ describe('processRequest()', () => {
257259
it('#mimeType should default to application/json', () =>
258260
request(createApp())
259261
.post('/')
262+
.send({ a: 1 })
260263
.expect(({ body }) => expect(body.postData.mimeType).toBe('application/json')));
261264

265+
it('should be an empty object if request is a GET', () =>
266+
request(createApp())
267+
.get('/')
268+
.expect(({ body }) => expect(body.postData).toStrictEqual({})));
269+
270+
it('should be an empty object if req.body is empty', () =>
271+
request(createApp())
272+
.post('/')
273+
.expect(({ body }) => expect(body.postData).toStrictEqual({})));
274+
262275
it('#text should contain stringified body', () => {
263276
const body = { a: 1, b: 2 };
264277
return request(createApp())

0 commit comments

Comments
 (0)