Skip to content

Commit 56d233e

Browse files
committed
This allows plain text body responses
Before we were always calling JSON.parse on it, which was failing for plain text
1 parent 74cb022 commit 56d233e

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

lib/process-response.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ module.exports = (res, options = {}) => {
1111
let body;
1212
try {
1313
body = JSON.parse(res._body); // eslint-disable-line no-underscore-dangle
14-
} catch (e) {
15-
// Non JSON body, don't attempt to send it off
16-
}
1714

18-
if (options.blacklist && body) {
19-
body = removeProperties(body, options.blacklist);
20-
}
15+
// Only apply blacklist/whitelist if it's an object
16+
if (options.blacklist) {
17+
body = removeProperties(body, options.blacklist);
18+
}
2119

22-
if (options.whitelist && body) {
23-
body = removeOtherProperties(body, options.whitelist);
20+
if (options.whitelist) {
21+
body = removeOtherProperties(body, options.whitelist);
22+
}
23+
} catch (e) {
24+
// Non JSON body
25+
body = res._body; // eslint-disable-line no-underscore-dangle
2426
}
2527

2628
return {

test/process-response.test.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function testResponse(assertion, response) {
1414

1515
// This is done in the main middleware by
1616
// overwriting res.write/end
17-
res._body = JSON.stringify(response); // eslint-disable-line no-underscore-dangle
17+
res._body = response; // eslint-disable-line no-underscore-dangle
1818

1919
res.json(response);
2020
});
@@ -36,7 +36,7 @@ describe('processResponse()', () => {
3636
);
3737
return done();
3838
},
39-
{ password: '123456', apiKey: 'abcdef', another: 'Hello world' },
39+
JSON.stringify({ password: '123456', apiKey: 'abcdef', another: 'Hello world' }),
4040
);
4141
});
4242

@@ -49,7 +49,21 @@ describe('processResponse()', () => {
4949
);
5050
return done();
5151
},
52-
{ password: '123456', apiKey: 'abcdef', another: 'Hello world' },
52+
JSON.stringify({ password: '123456', apiKey: 'abcdef', another: 'Hello world' }),
53+
);
54+
});
55+
56+
it('should not be applied for plain text bodies', done => {
57+
const body = 'hello world: dasdsas';
58+
testResponse(
59+
res => {
60+
assert.deepEqual(
61+
processResponse(res, { blacklist: ['password', 'apiKey'] }).content.text,
62+
JSON.stringify(body),
63+
);
64+
return done();
65+
},
66+
body,
5367
);
5468
});
5569
});
@@ -96,6 +110,14 @@ describe('processResponse()', () => {
96110

97111
it('#text', done => {
98112
const body = { a: 1, b: 2, c: 3 };
113+
testResponse(res => {
114+
assert.deepEqual(processResponse(res).content.text, JSON.stringify(body));
115+
return done();
116+
}, JSON.stringify(body));
117+
});
118+
119+
it('#text should work with plain text body', done => {
120+
const body = 'hello world: dasdsas';
99121
testResponse(res => {
100122
assert.deepEqual(processResponse(res).content.text, JSON.stringify(body));
101123
return done();

0 commit comments

Comments
 (0)