Revised jsonp unit tests and added a test for when the jsonp option is set to false.

This commit is contained in:
jaubourg 2011-01-16 05:27:16 +01:00
parent 158fa822de
commit 0e93b2e24c

View file

@ -1114,17 +1114,20 @@ test("jQuery.getScript(String, Function) - no callback", function() {
});
});
test("jQuery.ajax() - JSONP, Local", function() {
expect(14);
jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
test("jQuery.ajax() - JSONP, " + label, function() {
expect(16);
var count = 0;
function plus(){ if ( ++count == 14 ) start(); }
function plus(){ if ( ++count == 16 ) start(); }
stop();
jQuery.ajax({
url: "data/jsonp.php",
dataType: "jsonp",
crossDomain: crossDomain,
success: function(data){
ok( data.data, "JSON results returned (GET, no callback)" );
plus();
@ -1138,6 +1141,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php?callback=?",
dataType: "jsonp",
crossDomain: crossDomain,
success: function(data){
ok( data.data, "JSON results returned (GET, url callback)" );
plus();
@ -1151,6 +1155,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php",
dataType: "jsonp",
crossDomain: crossDomain,
data: "callback=?",
success: function(data){
ok( data.data, "JSON results returned (GET, data callback)" );
@ -1165,6 +1170,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php?callback=??",
dataType: "jsonp",
crossDomain: crossDomain,
success: function(data){
ok( data.data, "JSON results returned (GET, url context-free callback)" );
plus();
@ -1178,6 +1184,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php",
dataType: "jsonp",
crossDomain: crossDomain,
data: "callback=??",
success: function(data){
ok( data.data, "JSON results returned (GET, data context-free callback)" );
@ -1192,6 +1199,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php/??",
dataType: "jsonp",
crossDomain: crossDomain,
success: function(data){
ok( data.data, "JSON results returned (GET, REST-like)" );
plus();
@ -1205,6 +1213,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php/???json=1",
dataType: "jsonp",
crossDomain: crossDomain,
success: function(data){
strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
plus();
@ -1218,6 +1227,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php",
dataType: "jsonp",
crossDomain: crossDomain,
data: {
callback: "?"
},
@ -1234,6 +1244,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php",
dataType: "jsonp",
crossDomain: crossDomain,
jsonp: "callback",
success: function(data){
ok( data.data, "JSON results returned (GET, data obj callback)" );
@ -1245,9 +1256,16 @@ test("jQuery.ajax() - JSONP, Local", function() {
}
});
window.jsonpResults = function(data) {
ok( data.data, "JSON results returned (GET, custom callback function)" );
window.jsonpResults = undefined;
plus();
};
jQuery.ajax({
url: "data/jsonp.php",
dataType: "jsonp",
crossDomain: crossDomain,
jsonpCallback: "jsonpResults",
success: function(data){
ok( data.data, "JSON results returned (GET, custom callback name)" );
@ -1263,6 +1281,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
type: "POST",
url: "data/jsonp.php",
dataType: "jsonp",
crossDomain: crossDomain,
success: function(data){
ok( data.data, "JSON results returned (POST, no callback)" );
plus();
@ -1278,6 +1297,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
url: "data/jsonp.php",
data: "callback=?",
dataType: "jsonp",
crossDomain: crossDomain,
success: function(data){
ok( data.data, "JSON results returned (POST, data callback)" );
plus();
@ -1293,6 +1313,7 @@ test("jQuery.ajax() - JSONP, Local", function() {
url: "data/jsonp.php",
jsonp: "callback",
dataType: "jsonp",
crossDomain: crossDomain,
success: function(data){
ok( data.data, "JSON results returned (POST, data obj callback)" );
plus();
@ -1307,93 +1328,35 @@ test("jQuery.ajax() - JSONP, Local", function() {
jQuery.ajax({
url: "data/jsonp.php",
dataType: "jsonp",
crossDomain: crossDomain,
beforeSend: function(){
strictEqual( this.cache, false, "cache must be false on JSON request" );
plus();
return false;
}
});
});
test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
expect(1);
stop();
window.jsonpResults = function(data) {
ok( data.data, "JSON results returned (GET, custom callback function)" );
window.jsonpResults = undefined;
start();
};
jQuery.ajax({
url: "data/jsonp.php",
dataType: "jsonp",
jsonpCallback: "jsonpResults"
});
});
test("jQuery.ajax() - JSONP, Remote", function() {
expect(4);
var count = 0;
function plus(){ if ( ++count == 4 ) start(); }
var base = window.location.href.replace(/[^\/]*$/, "");
stop();
jQuery.ajax({
url: base + "data/jsonp.php",
url: "data/jsonp.php?callback=XXX",
dataType: "jsonp",
jsonp: false,
jsonpCallback: "XXX",
crossDomain: crossDomain,
beforeSend: function() {
console.log( this.url );
},
success: function(data){
ok( data.data, "JSON results returned (GET, no callback)" );
console.log(data);
ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
plus();
},
error: function(data){
ok( false, "Ajax error JSON (GET, no callback)" );
console.log(data);
ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
plus();
}
});
jQuery.ajax({
url: base + "data/jsonp.php?callback=?",
dataType: "jsonp",
success: function(data){
ok( data.data, "JSON results returned (GET, url callback)" );
plus();
},
error: function(data){
ok( false, "Ajax error JSON (GET, url callback)" );
plus();
}
});
jQuery.ajax({
url: base + "data/jsonp.php",
dataType: "jsonp",
data: "callback=?",
success: function(data){
ok( data.data, "JSON results returned (GET, data callback)" );
plus();
},
error: function(data){
ok( false, "Ajax error JSON (GET, data callback)" );
plus();
}
});
jQuery.ajax({
url: base + "data/jsonp.php",
dataType: "jsonp",
jsonp: "callback",
success: function(data){
ok( data.data, "JSON results returned (GET, data obj callback)" );
plus();
},
error: function(data){
ok( false, "Ajax error JSON (GET, data obj callback)" );
plus();
}
});
});