From e926483448c675b7443d2c085c49cee9d4619120 Mon Sep 17 00:00:00 2001 From: David Frank Date: Sat, 24 Sep 2016 17:12:18 +0800 Subject: [PATCH] minor format fix and comments --- lib/body.js | 6 +++++- test/server.js | 1 + test/test.js | 5 +---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/body.js b/lib/body.js index 437a3f3..e7bbe1d 100644 --- a/lib/body.js +++ b/lib/body.js @@ -39,7 +39,11 @@ function Body(body, opts) { */ Body.prototype.json = function() { - if (this.status === 204) return Body.Promise.resolve({}); + // for 204 No Content response, buffer will be empty, parsing it will throw error + if (this.status === 204) { + return Body.Promise.resolve({}); + } + return this._decode().then(function(buffer) { return JSON.parse(buffer.toString()); }); diff --git a/test/server.js b/test/server.js index 1cd0a08..08e582d 100644 --- a/test/server.js +++ b/test/server.js @@ -1,3 +1,4 @@ + var http = require('http'); var parse = require('url').parse; var zlib = require('zlib'); diff --git a/test/test.js b/test/test.js index 78fa465..6067ccd 100644 --- a/test/test.js +++ b/test/test.js @@ -1,3 +1,4 @@ + // test tools var chai = require('chai'); var cap = require('chai-as-promised'); @@ -1391,7 +1392,6 @@ describe('node-fetch', function() { }); }); - it('should support json() method in Request constructor', function() { url = base; var req = new Request(url, { @@ -1403,7 +1403,6 @@ describe('node-fetch', function() { }); }); - it('should support buffer() method in Request constructor', function() { url = base; var req = new Request(url, { @@ -1462,7 +1461,6 @@ describe('node-fetch', function() { expect(body).to.have.property('buffer'); }); - it('should create custom FetchError', function() { var systemError = new Error('system'); systemError.code = 'ESOMEERROR'; @@ -1477,7 +1475,6 @@ describe('node-fetch', function() { expect(err.errno).to.equal('ESOMEERROR'); }); - it('should support https request', function() { this.timeout(5000); url = 'https://github.com/';