Skip to content

Commit

Permalink
fix($httpBackend): Ignore multiple calls to onreadystatechange with r…
Browse files Browse the repository at this point in the history
…eadyState=4

On mobile webkit `onreadystatechange` might by called multiple times
with `readyState===4`  caused by xhrs that are resolved while the app is
in the background.

 Fixes angular#5426.
  • Loading branch information
tbosch authored and jamesdaily committed Jan 27, 2014
1 parent de569b0 commit 21bcf2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit 21bcf2a

Please sign in to comment.