fix($httpBackend): fix for jsonp requests

This commit is contained in:
Igor Minar 2012-01-09 10:36:51 -08:00 committed by Vojta Jina
parent 11cb9423a7
commit 7146f70636
5 changed files with 14 additions and 13 deletions

View file

@ -147,8 +147,8 @@ angular.module.ngMock.$Browser = function() {
};
self.$$scripts = [];
self.addJs = function(url, domId, done) {
var script = {url: url, id: domId, done: done};
self.addJs = function(url, done) {
var script = {url: url, done: done};
self.$$scripts.push(script);
return script;
};

View file

@ -38,7 +38,8 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locati
callbacks[callbackId].data = data;
};
var script = $browser.addJs(url.replace('JSON_CALLBACK', callbackId), null, function() {
var script = $browser.addJs(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
function() {
if (callbacks[callbackId].data) {
completeRequest(callback, 200, callbacks[callbackId].data);
} else {

View file

@ -6,16 +6,14 @@ describe('mocks', function() {
describe('addJs', function() {
it('should store url, id, done', inject(function($browser) {
it('should store url, done', inject(function($browser) {
var url = 'some.js',
id = 'js-id',
done = noop;
$browser.addJs(url, id, done);
$browser.addJs(url, done);
var script = $browser.$$scripts.shift();
expect(script.url).toBe(url);
expect(script.id).toBe(id);
expect(script.done).toBe(done);
}));

View file

@ -19,7 +19,7 @@ describe('$httpBackend', function() {
beforeEach(inject(function($injector) {
callbacks = {};
callbacks = {counter: 0};
$browser = $injector.get('$browser');
fakeBody = {removeChild: jasmine.createSpy('body.removeChild')};
$backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeBody);
@ -115,6 +115,9 @@ describe('$httpBackend', function() {
describe('JSONP', function() {
var SCRIPT_URL = /([^\?]*)\?cb=angular\.callbacks\.(.*)/;
it('should add script tag for JSONP request', function() {
callback.andCallFake(function(status, response) {
expect(status).toBe(200);
@ -125,10 +128,10 @@ describe('$httpBackend', function() {
expect($browser.$$scripts.length).toBe(1);
var script = $browser.$$scripts.shift(),
url = script.url.split('?cb=');
url = script.url.match(SCRIPT_URL);
expect(url[0]).toBe('http://example.org/path');
callbacks[url[1]]('some-data');
expect(url[1]).toBe('http://example.org/path');
callbacks[url[2]]('some-data');
script.done();
expect(callback).toHaveBeenCalledOnce();
@ -140,7 +143,7 @@ describe('$httpBackend', function() {
expect($browser.$$scripts.length).toBe(1);
var script = $browser.$$scripts.shift(),
callbackId = script.url.split('?cb=')[1];
callbackId = script.url.match(SCRIPT_URL)[2];
callbacks[callbackId]('some-data');
script.done();

View file

@ -244,7 +244,6 @@ describe('widget', function() {
$rootScope.tpl = template;
$rootScope.value = value;
});
$browser.defer.flush();
};
}