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

fix($httpBackend): complete the request on timeout #14972

Closed
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/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc

xhr.onerror = requestError;
xhr.onabort = requestError;
xhr.ontimeout = requestError;

forEach(eventHandlers, function(value, key) {
xhr.addEventListener(key, value);
Expand Down
38 changes: 24 additions & 14 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ describe('$httpBackend', function() {
});

it('should not try to read response data when request is aborted', function() {
callback.and.callFake(function(status, response, headers) {
callback.and.callFake(function(status, response, headers, statusText) {
expect(status).toBe(-1);
expect(response).toBe(null);
expect(headers).toBe(null);
expect(statusText).toBe('');
});
$backend('GET', '/url', null, callback, {}, 2000);
xhr = MockXhr.$$lastInstance;
Expand All @@ -172,6 +173,22 @@ describe('$httpBackend', function() {
expect(callback).toHaveBeenCalledOnce();
});

it('should complete the request on timeout', function() {
callback.and.callFake(function(status, response, headers, statusText) {
expect(status).toBe(-1);
expect(response).toBe(null);
expect(headers).toBe(null);
expect(statusText).toBe('');
});
$backend('GET', '/url', null, callback, {});
xhr = MockXhr.$$lastInstance;

expect(callback).not.toHaveBeenCalled();

xhr.ontimeout();
expect(callback).toHaveBeenCalledOnce();
});

it('should abort request on timeout', function() {
callback.and.callFake(function(status, response) {
expect(status).toBe(-1);
Expand Down Expand Up @@ -253,6 +270,7 @@ describe('$httpBackend', function() {
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
});


it('should call $xhrFactory with method and url', function() {
var mockXhrFactory = jasmine.createSpy('mockXhrFactory').and.callFake(createMockXhr);
$backend = createHttpBackend($browser, mockXhrFactory, $browser.defer, $jsonpCallbacks, fakeDocument);
Expand Down Expand Up @@ -391,6 +409,7 @@ describe('$httpBackend', function() {
// TODO(vojta): test whether it fires "async-end" on both success and error
});


describe('protocols that return 0 status code', function() {

function respond(status, content) {
Expand All @@ -400,10 +419,12 @@ describe('$httpBackend', function() {
xhr.onload();
}


it('should convert 0 to 200 if content and file protocol', function() {
beforeEach(function() {
$backend = createHttpBackend($browser, createMockXhr);
});


it('should convert 0 to 200 if content and file protocol', function() {
$backend('GET', 'file:///whatever/index.html', null, callback);
respond(0, 'SOME CONTENT');

Expand All @@ -412,8 +433,6 @@ describe('$httpBackend', function() {
});

it('should convert 0 to 200 if content for protocols other than file', function() {
$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', 'someProtocol:///whatever/index.html', null, callback);
respond(0, 'SOME CONTENT');

Expand All @@ -422,8 +441,6 @@ describe('$httpBackend', function() {
});

it('should convert 0 to 404 if no content and file protocol', function() {
$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', 'file:///whatever/index.html', null, callback);
respond(0, '');

Expand All @@ -432,8 +449,6 @@ describe('$httpBackend', function() {
});

it('should not convert 0 to 404 if no content for protocols other than file', function() {
$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', 'someProtocol:///whatever/index.html', null, callback);
respond(0, '');

Expand All @@ -460,8 +475,6 @@ describe('$httpBackend', function() {

try {

$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', '/whatever/index.html', null, callback);
respond(0, '');

Expand All @@ -473,10 +486,7 @@ describe('$httpBackend', function() {
}
});


it('should return original backend status code if different from 0', function() {
$backend = createHttpBackend($browser, createMockXhr);

// request to http://
$backend('POST', 'http://rest_api/create_whatever', null, callback);
respond(201, '');
Expand Down