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

fix($httpBackend): compare timeoutId with undefined in completeRequest #10177

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
4 changes: 3 additions & 1 deletion src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc

function completeRequest(callback, status, response, headersString, statusText) {
// cancel timeout and subsequent timeout promise resolution
timeoutId && $browserDefer.cancel(timeoutId);
if (timeoutId !== undefined) {
$browserDefer.cancel(timeoutId);
}
jsonpDone = xhr = null;

callback(status, response, headersString, statusText);
Expand Down
47 changes: 9 additions & 38 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,7 @@
describe('$httpBackend', function() {

var $backend, $browser, callbacks,
xhr, fakeDocument, callback,
fakeTimeoutId = 0;

// TODO(vojta): should be replaced by $defer mock
function fakeTimeout(fn, delay) {
fakeTimeout.fns.push(fn);
fakeTimeout.delays.push(delay);
fakeTimeout.ids.push(++fakeTimeoutId);
return fakeTimeoutId;
}

fakeTimeout.fns = [];
fakeTimeout.delays = [];
fakeTimeout.ids = [];
fakeTimeout.flush = function() {
var len = fakeTimeout.fns.length;
fakeTimeout.delays = [];
fakeTimeout.ids = [];
while (len--) fakeTimeout.fns.shift()();
};
fakeTimeout.cancel = function(id) {
var i = fakeTimeout.ids.indexOf(id);
if (i >= 0) {
fakeTimeout.fns.splice(i, 1);
fakeTimeout.delays.splice(i, 1);
fakeTimeout.ids.splice(i, 1);
return true;
}
return false;
};
xhr, fakeDocument, callback;


beforeEach(inject(function($injector) {
Expand All @@ -57,7 +28,7 @@ describe('$httpBackend', function() {
})
}
};
$backend = createHttpBackend($browser, createMockXhr, fakeTimeout, callbacks, fakeDocument);
$backend = createHttpBackend($browser, createMockXhr, $browser.defer, callbacks, fakeDocument);
callback = jasmine.createSpy('done');
}));

Expand Down Expand Up @@ -154,7 +125,7 @@ describe('$httpBackend', function() {
xhr = MockXhr.$$lastInstance;
spyOn(xhr, 'abort');

fakeTimeout.flush();
$browser.defer.flush();
expect(xhr.abort).toHaveBeenCalledOnce();

xhr.status = 0;
Expand All @@ -171,9 +142,9 @@ describe('$httpBackend', function() {
xhr = MockXhr.$$lastInstance;
spyOn(xhr, 'abort');

expect(fakeTimeout.delays[0]).toBe(2000);
expect($browser.deferredFns[0].time).toBe(2000);

fakeTimeout.flush();
$browser.defer.flush();
expect(xhr.abort).toHaveBeenCalledOnce();

xhr.status = 0;
Expand Down Expand Up @@ -227,13 +198,13 @@ describe('$httpBackend', function() {
xhr = MockXhr.$$lastInstance;
spyOn(xhr, 'abort');

expect(fakeTimeout.delays[0]).toBe(2000);
expect($browser.deferredFns[0].time).toBe(2000);

xhr.status = 200;
xhr.onload();
expect(callback).toHaveBeenCalledOnce();

expect(fakeTimeout.delays.length).toBe(0);
expect($browser.deferredFns.length).toBe(0);
expect(xhr.abort).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -342,12 +313,12 @@ describe('$httpBackend', function() {

$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback, null, 2000);
expect(fakeDocument.$$scripts.length).toBe(1);
expect(fakeTimeout.delays[0]).toBe(2000);
expect($browser.deferredFns[0].time).toBe(2000);

var script = fakeDocument.$$scripts.shift(),
callbackId = script.src.match(SCRIPT_URL)[2];

fakeTimeout.flush();
$browser.defer.flush();
expect(fakeDocument.$$scripts.length).toBe(0);
expect(callback).toHaveBeenCalledOnce();

Expand Down