Browse Source

Merge pull request #172 from lpinca/gh-170

Parse the response body only if it is not empty
node-7
Sindre Sorhus 9 years ago
parent
commit
5a66b89af3
  1. 2
      index.js
  2. 6
      test/json.js

2
index.js

@ -101,7 +101,7 @@ function asPromise(opts) {
res.body = data;
if (opts.json && statusCode !== 204) {
if (opts.json && res.body) {
try {
res.body = JSON.parse(res.body);
} catch (e) {

6
test/json.js

@ -15,8 +15,8 @@ test.before('setup', async () => {
res.end('/');
});
s.on('/204', (req, res) => {
res.statusCode = 204;
s.on('/no-body', (req, res) => {
res.statusCode = 200;
res.end();
});
@ -38,7 +38,7 @@ test('parses response', async t => {
});
test('not parses responses without a body', async t => {
const {body} = await got(`${s.url}/204`, {json: true});
const {body} = await got(`${s.url}/no-body`, {json: true});
t.is(body, '');
});

Loading…
Cancel
Save