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

fix($httpBackend): Ignore multiple calls to onreadystatechange with read... #5603

Merged
merged 1 commit into from
Jan 2, 2014
Merged
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
5 changes: 5 additions & 0 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument)
// always async
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// onreadystatechange might by called multiple times
// with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
xhr.onreadystatechange = undefined;

var responseHeaders = null,
response = null;

Expand Down
12 changes: 12 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ describe('$httpBackend', function() {
expect(callback).toHaveBeenCalledOnce();
});

// onreadystatechange might by called multiple times
// with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
it('should remove onreadystatechange when it is called with readyState=4 to ignore multiple calls', function() {
$backend('GET', 'URL', null, callback);
xhr = MockXhr.$$lastInstance;

xhr.status = 200;
xhr.readyState = 4;
xhr.onreadystatechange();
expect(xhr.onreadystatechange).toBeUndefined();
});

it('should set only the requested headers', function() {
$backend('POST', 'URL', null, noop, {'X-header1': 'value1', 'X-header2': 'value2'});
Expand Down