Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reject request promise if remove server is unavailable #1418

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const RESTController = {
const xhr = new XHR();
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4 || handled || xhr._aborted) {
promise.reject(xhr);
return;
}
handled = true;
Expand Down
10 changes: 9 additions & 1 deletion src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ describe('RESTController', () => {
});
});

it('reject response if server is unavailable', done => {
RESTController._setXHR(mockXHR([{ status: 0 }], { readyState: 1 }));
RESTController.ajax('POST', 'users', {}).then(null, err => {
expect(err.readyState).toBe(1);
done();
});
jest.runAllTimers();
});

it('retries on 5XX errors', done => {
RESTController._setXHR(
mockXHR([{ status: 500 }, { status: 500 }, { status: 200, response: { success: true } }])
Expand Down Expand Up @@ -462,7 +471,6 @@ describe('RESTController', () => {
});
RESTController.request('GET', 'classes/MyObject', {}, {});
await flushPromises();
xhr.onreadystatechange();
expect(JSON.parse(xhr.send.mock.calls[0][0])).toEqual({
_method: 'GET',
_ApplicationId: 'A',
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/test_helpers/mockXHR.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function mockXHR(results, options = {}) {
send: function () {
this.status = results[attempts].status;
this.responseText = JSON.stringify(results[attempts].response || {});
this.readyState = 4;
this.readyState = options.readyState || 4;
attempts++;
this.onreadystatechange();
this.onprogress(options.progress);
Expand Down