Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($http): reject $http promise when timeout occurs #7688

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
21 changes: 17 additions & 4 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function $HttpBackendProvider() {

function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
var ABORTED = -1;
var TIMEOUT = -2;

// TODO(vojta): fix the signature
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
Expand Down Expand Up @@ -68,6 +69,14 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
}
});

// The fake timeout mechanism relies on xhr.abort(), so it's necessary to do this so that the
// expected behaviour occurs.
xhr.onabort = function() {
if (status === undefined) {
status = ABORTED;
}
};

// In IE6 and 7, this might be called synchronously when xhr.send below is called and the
// response is in the cache. the promise api will ensure that to the app code the api is
// always async
Expand All @@ -81,9 +90,13 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
// Safari respectively.
if (xhr && xhr.readyState == 4) {
var responseHeaders = null,
response = null;
response = null,
statusText = '';

if(status !== ABORTED) {
if (status === TIMEOUT) {
statusText = 'Timeout';
} else if(status !== ABORTED) {
statusText = xhr.statusText || '';
responseHeaders = xhr.getAllResponseHeaders();

// responseText is the old-school way of retrieving response (supported by IE8 & 9)
Expand All @@ -95,7 +108,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
status || xhr.status,
response,
responseHeaders,
xhr.statusText || '');
statusText);
}
};

Expand Down Expand Up @@ -131,7 +144,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc


function timeoutRequest() {
status = ABORTED;
status = TIMEOUT;
jsonpDone && jsonpDone();
xhr && xhr.abort();
}
Expand Down
6 changes: 3 additions & 3 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('$httpBackend', function() {

it('should not try to read response data when request is aborted', function() {
callback.andCallFake(function(status, response, headers) {
expect(status).toBe(-1);
expect(status).toBe(-2);
expect(response).toBe(null);
expect(headers).toBe(null);
});
Expand All @@ -183,7 +183,7 @@ describe('$httpBackend', function() {

it('should abort request on timeout', function() {
callback.andCallFake(function(status, response) {
expect(status).toBe(-1);
expect(status).toBe(-2);
});

$backend('GET', '/url', null, callback, {}, 2000);
Expand All @@ -204,7 +204,7 @@ describe('$httpBackend', function() {

it('should abort request on timeout promise resolution', inject(function($timeout) {
callback.andCallFake(function(status, response) {
expect(status).toBe(-1);
expect(status).toBe(-2);
});

$backend('GET', '/url', null, callback, {}, $timeout(noop, 2000));
Expand Down
14 changes: 14 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,20 @@ describe('$http', function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
}));


it('should reject promise when timeout promise resolves', inject(function($timeout) {
var onFulfilled = jasmine.createSpy('onFulfilled');
var onRejected = jasmine.createSpy('onRejected');
$httpBackend.expect('GET', '/some').respond(200);

$http({method: 'GET', url: '/some', timeout: $timeout(noop, 10)}).then(onFulfilled, onRejected);

$timeout.flush(100);

expect(onFulfilled).not.toHaveBeenCalled();
expect(onRejected).toHaveBeenCalledOnce();
}));
});


Expand Down