Skip to content

Commit

Permalink
added addtl. unit test for custom callback keys/functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholascloud committed Jun 14, 2013
1 parent 9575daf commit b9acb79
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,21 @@ amplify.request.types.ajax = function( defnSettings ) {
return this.url;
}

var callbackName = 'callback';
var callbackKey = 'callback';

// possible for the callback function name to be overridden
if (this.hasOwnProperty('jsonp')) {
if (this.jsonp !== false) {
callbackName = this.jsonp;
} else {
if (this.hasOwnProperty('jsonpCallback')) {
callbackName = this.jsonpCallback;
}
}
if (this.jsonp === false) {
// if false, assume they are manually specifying the
// callback and key in the querystring, and it will
// not change (is cacheable)
return this.url;
}
callbackKey = this.jsonp;
}

// search and replace callback parameter in query string with empty string
var callbackRegex = new RegExp('&?' + callbackName + '=[^&]*&?', 'gi');
var callbackRegex = new RegExp('&?' + callbackKey + '=[^&]*&?', 'gi');
return this.url.replace(callbackRegex, '');
},
success: function( data, status ) {
Expand Down
45 changes: 42 additions & 3 deletions test/request/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ test( "cache types", function() {
});
});

asyncTest( "cache: jsonp with no specific callback", function () {
asyncTest( "cache: jsonp with auto-generated callback", function () {
expect(5);

amplify.request.define( "jsonp-cache", "ajax", {
Expand Down Expand Up @@ -1092,7 +1092,46 @@ asyncTest( "cache: jsonp with no specific callback", function () {
});
});

asyncTest( "cache: jsonp with `jsonp` parameter defining specific callback", function () {
asyncTest( "cache: jsonp with custom callback key and function", function () {
expect(5);

amplify.request.define( "jsonp-cache", "ajax", {
url: "/test/request/jsonp/custom_key?custom_key=custom_function",
dataType: "jsonp",
jsonp: false,
jsonpCallback: 'custom_function',
cache: { type: "persist", expires: 450 }
});

var timesAjaxTriggered = 0;

subscribe( "request.before.ajax", function( resource ) {
console.log('before');
equal( resource.resourceId, "jsonp-cache", "before.ajax message: resource.resourceId" );
timesAjaxTriggered += 1;
});

subscribe( "request.error", function(settings, data, status) {
console.log('error', status);
ok(false, "error message published: " + status);
});

var expectedData = {foo: 'bar'};

amplify.request( "jsonp-cache", function( actualData ) {
deepEqual( actualData, expectedData, "first request callback" );
amplify.request( "jsonp-cache", function( actualData ) {
deepEqual( actualData, expectedData, "first request callback" );
amplify.request('jsonp-cache', function (actualData) {
deepEqual(actualData, expectedData, "third request callback" );
equal(timesAjaxTriggered, 1, 'ajax should have been triggered once');
start();
});
});
});
});

asyncTest( "cache: jsonp with `jsonp` parameter defining specific callback key with auto-generated callback function", function () {
expect(5);

var callbackKey = 'call_back_key';
Expand Down Expand Up @@ -1130,7 +1169,7 @@ asyncTest( "cache: jsonp with `jsonp` parameter defining specific callback", fun
});
});

asyncTest( "cache: jsonp with `jsonpCallback` parameter defining specific callback", function () {
asyncTest( "cache: jsonp with `jsonpCallback` parameter defining specific callback function with default callback key", function () {
expect(5);

var callbackFunctionName = 'cb1234567890_0987654321';
Expand Down

0 comments on commit b9acb79

Please sign in to comment.