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

JSONP requests with the same callback name #1548

Closed
wants to merge 4 commits into from
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: 2 additions & 2 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
url = url || $browser.url();

if (lowercase(method) == 'jsonp') {
var callbackId = '_' + (callbacks.counter++).toString(36);
var callbackId = url.indexOf('JSON_SINGLE_CALLBACK') === -1 ? '_' + (callbacks.counter++).toString(36) : 'single';
callbacks[callbackId] = function(data) {
callbacks[callbackId].data = data;
};

jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId).replace('JSON_SINGLE_CALLBACK', 'angular.callbacks.single'),
function() {
if (callbacks[callbackId].data) {
completeRequest(callback, 200, callbacks[callbackId].data);
Expand Down
25 changes: 25 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,31 @@ describe('$httpBackend', function() {
expect(fakeDocument.$$scripts[0].src).toBe($browser.url());
});

it('should always call callback with the same name when use JSON_SINGLE_CALLBACK', function() {
callback.andCallFake(function(status, response) {
expect(status).toBe(200);
expect(response).toBe('some-data');
});

$backend('JSONP', 'http://example.org/path?cb=JSON_SINGLE_CALLBACK', null, callback);
expect(fakeDocument.$$scripts.length).toBe(1);

var script = fakeDocument.$$scripts.shift(),
url = script.src.match(/([^\?]*)\?cb=angular\.callbacks\.single/);

expect(url[1]).toBe('http://example.org/path');
callbacks['single']('some-data');

if (script.onreadystatechange) {
script.readyState = 'complete';
script.onreadystatechange();
} else {
script.onload()
}

expect(callback).toHaveBeenCalledOnce();
});


// TODO(vojta): test whether it fires "async-start"
// TODO(vojta): test whether it fires "async-end" on both success and error
Expand Down