Skip to content

Commit

Permalink
Make rest adapter test pass for fetch (#6293)
Browse files Browse the repository at this point in the history
* Make rest adapter test pass for fetch

* fix eslint prettier
  • Loading branch information
Gaurav0 authored and runspired committed Aug 25, 2019
1 parent a68bfac commit 30d3db0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
36 changes: 17 additions & 19 deletions packages/-ember-data/tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2665,24 +2665,22 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) {
});
});

if (hasJQuery) {
testInDebug('warns when an empty response is returned, though a valid stringified JSON is expected', function(
assert
) {
server.post('/posts', function() {
return [201, { 'Content-Type': 'application/json' }, ''];
});

return run(() => {
return store.createRecord('post').save();
}).then(
() => {
assert.equal(true, false, 'should not have fulfilled');
},
reason => {
assert.ok(/JSON/.test(reason.message));
}
);
testInDebug('warns when an empty response is returned, though a valid stringified JSON is expected', function(
assert
) {
server.post('/posts', function() {
return [201, { 'Content-Type': 'application/json' }, ''];
});
}

return run(() => {
return store.createRecord('post').save();
}).then(
() => {
assert.equal(true, false, 'should not have fulfilled');
},
reason => {
assert.ok(/JSON/.test(reason.message));
}
);
});
});
13 changes: 13 additions & 0 deletions packages/adapter/addon/-private/utils/determine-body-promise.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { warn } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

/*
* Function that always attempts to parse the response as json, and if an error is thrown,
* returns `undefined` if the response is successful and has a status code of 204 (No Content),
Expand All @@ -22,6 +25,16 @@ export function determineBodyPromise(
) {
ret = undefined;
} else {
if (DEBUG) {
let message = `The server returned an empty string for ${requestData.method} ${requestData.url}, which cannot be parsed into a valid JSON. Return either null or {}.`;
if (payload === '') {
warn(message, true, {
id: 'ds.adapter.returned-empty-string-as-JSON',
});
throw error;
}
}

console.warn('This response was unable to be parsed as json.', payload);
}
}
Expand Down
17 changes: 9 additions & 8 deletions packages/adapter/addon/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1246,14 +1246,6 @@ function ajaxSuccess(adapter, payload, requestData, responseData) {
}

function ajaxError(adapter, payload, requestData, responseData) {
if (DEBUG) {
let message = `The server returned an empty string for ${requestData.method} ${requestData.url}, which cannot be parsed into a valid JSON. Return either null or {}.`;
let validJSONString = !(responseData.textStatus === 'parsererror' && payload === '');
warn(message, validJSONString, {
id: 'ds.adapter.returned-empty-string-as-JSON',
});
}

let error;

if (responseData.errorThrown instanceof Error) {
Expand Down Expand Up @@ -1316,6 +1308,15 @@ function ajaxErrorHandler(adapter, jqXHR, errorThrown, requestData) {
let responseData = ajaxResponseData(jqXHR);
responseData.errorThrown = errorThrown;
let payload = adapter.parseErrorResponse(jqXHR.responseText);

if (DEBUG) {
let message = `The server returned an empty string for ${requestData.method} ${requestData.url}, which cannot be parsed into a valid JSON. Return either null or {}.`;
let validJSONString = !(responseData.textStatus === 'parsererror' && payload === '');
warn(message, validJSONString, {
id: 'ds.adapter.returned-empty-string-as-JSON',
});
}

return ajaxError(adapter, payload, requestData, responseData);
}

Expand Down

0 comments on commit 30d3db0

Please sign in to comment.