From b9acb7991dff07637e226962047e65af508d8074 Mon Sep 17 00:00:00 2001 From: nicholascloud Date: Fri, 14 Jun 2013 16:03:19 -0500 Subject: [PATCH] added addtl. unit test for custom callback keys/functions --- src/request.js | 18 +++++++++--------- test/request/unit.js | 45 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/request.js b/src/request.js index 4592301..1e94528 100644 --- a/src/request.js +++ b/src/request.js @@ -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 ) { diff --git a/test/request/unit.js b/test/request/unit.js index 82e567d..20d0123 100644 --- a/test/request/unit.js +++ b/test/request/unit.js @@ -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", { @@ -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'; @@ -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';