Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Commit

Permalink
Fix 204 response on fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinBressan committed Sep 28, 2015
1 parent 15c8343 commit 9f6a927
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/es5/http/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports['default'] = function (fetch) {
}

return fetch(url, config).then(function (response) {
return response.json().then(function (json) {
return (response.status === 204 ? Promise.resolve(null) : response.json()).then(function (json) {
var headers = {};

response.headers.forEach(function (value, name) {
Expand Down
2 changes: 1 addition & 1 deletion dist/restful.js
Original file line number Diff line number Diff line change
Expand Up @@ -5482,7 +5482,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

return fetch(url, config).then(function (response) {
return response.json().then(function (json) {
return (response.status === 204 ? Promise.resolve(null) : response.json()).then(function (json) {
var headers = {};

response.headers.forEach(function (value, name) {
Expand Down
4 changes: 2 additions & 2 deletions dist/restful.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/restful.standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -5493,7 +5493,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

return fetch(url, config).then(function (response) {
return response.json().then(function (json) {
return (response.status === 204 ? Promise.resolve(null) : response.json()).then(function (json) {
var headers = {};

response.headers.forEach(function (value, name) {
Expand Down
4 changes: 2 additions & 2 deletions dist/restful.standalone.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/http/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function(fetch) {

return fetch(url, config)
.then((response) => {
return response.json().then((json) => {
return (response.status === 204 ? Promise.resolve(null) : response.json()).then((json) => {
const headers = {};

response.headers.forEach((value, name) => {
Expand Down
31 changes: 30 additions & 1 deletion test/src/http/fetchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Fetch HTTP Backend', () => {
cb('here', 'test');
},
},
json: () => Promise.resolve({ content: 'Yes' }),
json: sinon.stub().returns(Promise.resolve({ content: 'Yes' })),
status: 200,
};
fetch = sinon.stub().returns(Promise.resolve(response));
Expand Down Expand Up @@ -102,6 +102,35 @@ describe('Fetch HTTP Backend', () => {
.catch(done);
});

it('should correctly format the response when it succeed and returns 204 as status code', (done) => {
response.status = 204;
httpBackend({
data: {
me: 'you',
},
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
params: {
asc: 1,
},
url: '/url',
})
.then((_response) => {
expect(_response).to.deep.equal({
data: null,
headers: {
test: 'here',
},
statusCode: 204,
});

expect(response.json.callCount).to.equal(0);
done();
})
.catch(done);
});

it('should correctly format the error when it fails', (done) => {
response.status = 404;
response.statusText = 'Not Found';
Expand Down

0 comments on commit 9f6a927

Please sign in to comment.