diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/data/mock.php | 4 | ||||
-rw-r--r-- | test/middleware-mockserver.js | 3 | ||||
-rw-r--r-- | test/unit/ajax.js | 115 |
3 files changed, 122 insertions, 0 deletions
diff --git a/test/data/mock.php b/test/data/mock.php index 5b56d02c7..a34e8f5ac 100644 --- a/test/data/mock.php +++ b/test/data/mock.php @@ -70,6 +70,10 @@ QUnit.assert.ok( true, "mock executed");'; header( 'Content-type: application/json' ); } + if ( isset( $req->query['cors'] ) ) { + header( 'Access-Control-Allow-Origin: *' ); + } + if ( isset( $req->query['array'] ) ) { echo '[ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ]'; } else { diff --git a/test/middleware-mockserver.js b/test/middleware-mockserver.js index f6196d230..e3b0bd163 100644 --- a/test/middleware-mockserver.js +++ b/test/middleware-mockserver.js @@ -81,6 +81,9 @@ var mocks = { if ( req.query.header ) { resp.writeHead( 200, { "content-type": "application/json" } ); } + if ( req.query.cors ) { + resp.writeHead( 200, { "access-control-allow-origin": "*" } ); + } if ( req.query.array ) { resp.end( JSON.stringify( [ { name: "John", age: 21 }, { name: "Peter", age: 25 } ] diff --git a/test/unit/ajax.js b/test/unit/ajax.js index e13c2713d..cd7822b73 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -1239,6 +1239,121 @@ QUnit.module( "ajax", { ]; } ); + ajaxTest( "jQuery.ajax() - no JSONP auto-promotion" + label, 4, function( assert ) { + return [ + { + url: baseURL + "mock.php?action=jsonp", + dataType: "json", + crossDomain: crossDomain, + success: function() { + assert.ok( false, "JSON parsing should have failed (no callback)" ); + }, + fail: function() { + assert.ok( true, "JSON parsing failed, JSONP not used (no callback)" ); + } + }, + { + url: baseURL + "mock.php?action=jsonp&callback=?", + dataType: "json", + crossDomain: crossDomain, + success: function() { + assert.ok( false, "JSON parsing should have failed (ULR callback)" ); + }, + fail: function() { + assert.ok( true, "JSON parsing failed, JSONP not used (URL callback)" ); + } + }, + { + url: baseURL + "mock.php?action=jsonp", + dataType: "json", + crossDomain: crossDomain, + data: "callback=?", + success: function() { + assert.ok( false, "JSON parsing should have failed (data callback=?)" ); + }, + fail: function() { + assert.ok( true, "JSON parsing failed, JSONP not used (data callback=?)" ); + } + }, + { + url: baseURL + "mock.php?action=jsonp", + dataType: "json", + crossDomain: crossDomain, + data: "callback=??", + success: function() { + assert.ok( false, "JSON parsing should have failed (data callback=??)" ); + }, + fail: function() { + assert.ok( true, "JSON parsing failed, JSONP not used (data callback=??)" ); + } + } + ]; + } ); + + ajaxTest( "jQuery.ajax() - JSON - no ? replacement" + label, 9, function( assert ) { + return [ + { + url: baseURL + "mock.php?action=json&callback=?", + dataType: "json", + crossDomain: crossDomain, + beforeSend: function( _jqXhr, settings ) { + var queryString = settings.url.replace( /^[^?]*\?/, "" ); + assert.ok( + queryString.indexOf( "jQuery" ) === -1, + "jQuery callback not inserted into the URL (URL callback)" + ); + assert.ok( + queryString.indexOf( "callback=?" ) > -1, + "\"callback=?\" present in the URL unchanged (URL callback)" + ); + }, + success: function( data ) { + assert.ok( data.data, "JSON results returned (URL callback)" ); + } + }, + { + url: baseURL + "mock.php?action=json", + dataType: "json", + crossDomain: crossDomain, + data: "callback=?", + beforeSend: function( _jqXhr, settings ) { + var queryString = settings.url.replace( /^[^?]*\?/, "" ); + assert.ok( + queryString.indexOf( "jQuery" ) === -1, + "jQuery callback not inserted into the URL (data callback=?)" + ); + assert.ok( + queryString.indexOf( "callback=?" ) > -1, + "\"callback=?\" present in the URL unchanged (data callback=?)" + ); + }, + success: function( data ) { + assert.ok( data.data, "JSON results returned (data callback=?)" ); + } + }, + { + url: baseURL + "mock.php?action=json", + dataType: "json", + crossDomain: crossDomain, + data: "callback=??", + beforeSend: function( _jqXhr, settings ) { + var queryString = settings.url.replace( /^[^?]*\?/, "" ); + assert.ok( + queryString.indexOf( "jQuery" ) === -1, + "jQuery callback not inserted into the URL (data callback=??)" + ); + assert.ok( + queryString.indexOf( "callback=??" ) > -1, + "\"callback=?\" present in the URL unchanged (data callback=??)" + ); + }, + success: function( data ) { + assert.ok( data.data, "JSON results returned (data callback=??)" ); + } + } + ]; + } ); + } ); ajaxTest( "jQuery.ajax() - script, Remote", 2, function( assert ) { |