From 45a636c6c22c2f7c9ffc14c78b8679d23485f9d2 Mon Sep 17 00:00:00 2001 From: Luigi Pinca <luigipinca@gmail.com> Date: Tue, 16 Feb 2016 16:00:25 +0100 Subject: [PATCH] parse the response body only if it is not empty Fixes #170 --- index.js | 2 +- test/json.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index f54f3b9..1a4d91d 100644 --- a/index.js +++ b/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) { diff --git a/test/json.js b/test/json.js index 2f0b2dc..a30b8d7 100644 --- a/test/json.js +++ b/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, ''); });