Skip to content

Commit

Permalink
Return empty .json() object on 204. Fix #165. (#166)
Browse files Browse the repository at this point in the history
Handle an edge case where 204 No Content response body is empty, but node-fetch trying to parse the empty body as-is.
  • Loading branch information
dandv authored and bitinn committed Sep 24, 2016
1 parent 3e53f0c commit 95b5893
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Changelog

# 1.x release

## v1.6.1 (master)
## v1.6.2

- Fix: response.json() returns empty object on 204 no-content response instead of throwing a syntax error

## v1.6.1

- Fix: if `res.body` is a non-stream non-formdata object, we will call `body.toString` and send it as a string
- Fix: `counter` value is incorrectly set to `follow` value when wrapping Request instance
Expand All @@ -22,7 +26,7 @@ Changelog

## v1.5.3

- Fix: handles 204 and 304 responses when body is empty but content-encoding is gzip/deflate
- Fix: handle 204 and 304 responses when body is empty but content-encoding is gzip/deflate
- Fix: allow resolving response and cloned response in any order
- Fix: avoid setting `content-length` when `form-data` body use streams
- Fix: send DELETE request with content-length when body is present
Expand Down
1 change: 1 addition & 0 deletions lib/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function Body(body, opts) {
*/
Body.prototype.json = function() {

if (this.status === 204) return Body.Promise.resolve({});
return this._decode().then(function(buffer) {
return JSON.parse(buffer.toString());
});
Expand Down
1 change: 0 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var http = require('http');
var parse = require('url').parse;
var zlib = require('zlib');
Expand Down
25 changes: 19 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// test tools
var chai = require('chai');
var cap = require('chai-as-promised');
Expand Down Expand Up @@ -441,6 +440,16 @@ describe('node-fetch', function() {
});
});

it('should return empty object on no-content response', function() {
url = base + '/no-content';
return fetch(url).then(function(res) {
return res.json().then(function(result) {
expect(result).to.be.an('object');
expect(result).to.be.empty;
});
});
});

it('should handle no content response with gzip encoding', function() {
url = base + '/no-content/gzip';
return fetch(url).then(function(res) {
Expand Down Expand Up @@ -1380,7 +1389,8 @@ describe('node-fetch', function() {
return req.text().then(function(result) {
expect(result).to.equal('a=1');
});
});
});


it('should support json() method in Request constructor', function() {
url = base;
Expand All @@ -1391,7 +1401,8 @@ describe('node-fetch', function() {
return req.json().then(function(result) {
expect(result.a).to.equal(1);
});
});
});


it('should support buffer() method in Request constructor', function() {
url = base;
Expand All @@ -1402,7 +1413,7 @@ describe('node-fetch', function() {
return req.buffer().then(function(result) {
expect(result.toString()).to.equal('a=1');
});
});
});

it('should support arbitrary url in Request constructor', function() {
url = 'anything';
Expand Down Expand Up @@ -1449,7 +1460,8 @@ describe('node-fetch', function() {
expect(body).to.have.property('text');
expect(body).to.have.property('json');
expect(body).to.have.property('buffer');
});
});


it('should create custom FetchError', function() {
var systemError = new Error('system');
Expand All @@ -1463,7 +1475,8 @@ describe('node-fetch', function() {
expect(err.type).to.equal('test-error');
expect(err.code).to.equal('ESOMEERROR');
expect(err.errno).to.equal('ESOMEERROR');
});
});


it('should support https request', function() {
this.timeout(5000);
Expand Down

0 comments on commit 95b5893

Please sign in to comment.