fix($httpBackend): Ignore multiple calls to onreadystatechange with readyState=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 #5426.
This commit is contained in:
Tobias Bosch 2014-01-02 14:37:48 -08:00
parent 50bf029625
commit 4f57236614
2 changed files with 17 additions and 0 deletions

View file

@ -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;

View file

@ -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'});