Skip to content

Commit

Permalink
Return empty .json() object on 204. Fix node-fetch#165.
Browse files Browse the repository at this point in the history
  • Loading branch information
dandv committed Sep 23, 2016
1 parent 3e53f0c commit 69ac99d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 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
3 changes: 2 additions & 1 deletion lib/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ function Body(body, opts) {
Body.prototype.json = function() {

return this._decode().then(function(buffer) {
return JSON.parse(buffer.toString());
var string = buffer.toString();
return string === ''? {} : JSON.parse(string);
});

};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-fetch",
"version": "1.6.1",
"version": "1.6.2",
"description": "A light-weight module that brings window.fetch to node.js and io.js",
"main": "index.js",
"scripts": {
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
26 changes: 20 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,8 @@ 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 +1461,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 +1476,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 69ac99d

Please sign in to comment.