2015-08-16 03:45:28 +00:00
QUnit . module ( "ajax" , {
2019-02-18 18:02:38 +00:00
afterEach : function ( ) {
2012-12-01 20:25:52 +00:00
jQuery ( document ) . off ( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" ) ;
2012-12-02 15:48:26 +00:00
moduleTeardown . apply ( this , arguments ) ;
2012-12-01 20:25:52 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-03 04:06:50 +00:00
2015-08-16 06:59:58 +00:00
( function ( ) {
QUnit . test ( "Unit Testing Environment" , function ( assert ) {
2015-08-16 03:45:28 +00:00
assert . expect ( 2 ) ;
2015-07-29 15:10:04 +00:00
2015-08-16 03:45:28 +00:00
assert . ok ( hasPHP , "Running in an environment with PHP support. The AJAX tests only run if the environment supports PHP!" ) ;
assert . ok ( ! isLocal , "Unit tests are not ran from file:// (especially in Chrome. If you must test from file:// with Chrome, run it with the --allow-file-access-from-files flag!)" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-03 04:06:50 +00:00
2022-06-28 10:39:01 +00:00
if ( ! includesModule ( "ajax" ) || ( isLocal && ! hasPHP ) ) {
2012-11-03 04:06:50 +00:00
return ;
}
2007-09-03 23:45:14 +00:00
2015-08-16 03:45:28 +00:00
function addGlobalEvents ( expected , assert ) {
2012-12-02 15:48:26 +00:00
return function ( ) {
expected = expected || "" ;
jQuery ( document ) . on ( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" , function ( e ) {
2015-08-16 06:59:58 +00:00
assert . ok ( expected . indexOf ( e . type ) !== - 1 , e . type ) ;
} ) ;
2012-12-02 15:48:26 +00:00
} ;
2012-12-01 20:25:52 +00:00
}
2012-11-26 02:31:19 +00:00
//----------- jQuery.ajax()
2010-12-09 18:34:28 +00:00
2016-04-10 19:42:44 +00:00
testIframe (
2015-08-16 03:45:28 +00:00
"XMLHttpRequest - Attempt to block tests because of dangling XHR requests (IE)" ,
"ajax/unreleasedXHR.html" ,
function ( assert ) {
assert . expect ( 1 ) ;
assert . ok ( true , "done" ) ;
2012-11-26 02:31:19 +00:00
}
2015-08-16 03:45:28 +00:00
) ;
ajaxTest ( "jQuery.ajax() - success callbacks" , 8 , function ( assert ) {
2015-11-02 17:00:28 +00:00
return {
2015-08-16 03:45:28 +00:00
setup : addGlobalEvents ( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess" , assert ) ,
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( ) {
assert . ok ( true , "beforeSend" ) ;
} ,
success : function ( ) {
assert . ok ( true , "success" ) ;
} ,
complete : function ( ) {
2015-08-16 06:59:58 +00:00
assert . ok ( true , "complete" ) ;
2015-08-16 03:45:28 +00:00
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-12-09 18:34:28 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - success callbacks - (url, options) syntax" , 8 , function ( assert ) {
return {
2015-08-16 06:59:58 +00:00
setup : addGlobalEvents ( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess" , assert ) ,
2015-08-16 03:45:28 +00:00
create : function ( options ) {
2017-08-01 16:52:45 +00:00
return jQuery . ajax ( url ( "name.html" ) , options ) ;
2015-08-16 03:45:28 +00:00
} ,
beforeSend : function ( ) {
assert . ok ( true , "beforeSend" ) ;
} ,
success : function ( ) {
assert . ok ( true , "success" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-12-09 18:34:28 +00:00
2023-02-01 12:40:55 +00:00
jQuery . each ( [ " - Same Domain" , " - Cross Domain" ] , function ( crossDomain , label ) {
ajaxTest ( "jQuery.ajax() - custom attributes for script tag" + label , 5 ,
function ( assert ) {
return {
create : function ( options ) {
var xhr ;
options . crossDomain = crossDomain ;
options . method = "POST" ;
options . dataType = "script" ;
options . scriptAttrs = { id : "jquery-ajax-test" , async : "async" } ;
xhr = jQuery . ajax ( url ( "mock.php?action=script" ) , options ) ;
assert . equal ( jQuery ( "#jquery-ajax-test" ) . attr ( "async" ) , "async" , "attr value" ) ;
return xhr ;
} ,
beforeSend : function ( _jqXhr , settings ) {
assert . strictEqual ( settings . type , "GET" , "Type changed to GET" ) ;
} ,
success : function ( ) {
assert . ok ( true , "success" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
}
) ;
ajaxTest ( "jQuery.ajax() - headers for script transport" + label , 3 ,
function ( assert ) {
return {
create : function ( options ) {
Globals . register ( "corsCallback" ) ;
window . corsCallback = function ( response ) {
assert . strictEqual ( response . headers [ "x-custom-test-header" ] ,
"test value" , "Custom header sent" ) ;
} ;
options . crossDomain = crossDomain ;
options . dataType = "script" ;
options . headers = { "x-custom-test-header" : "test value" } ;
return jQuery . ajax ( url ( "mock.php?action=script&callback=corsCallback" ) , options ) ;
} ,
success : function ( ) {
assert . ok ( true , "success" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
}
) ;
ajaxTest ( "jQuery.ajax() - scriptAttrs winning over headers" + label , 4 ,
function ( assert ) {
return {
create : function ( options ) {
var xhr ;
Globals . register ( "corsCallback" ) ;
window . corsCallback = function ( response ) {
assert . ok ( ! response . headers [ "x-custom-test-header" ] ,
"headers losing with scriptAttrs" ) ;
} ;
options . crossDomain = crossDomain ;
options . dataType = "script" ;
options . scriptAttrs = { id : "jquery-ajax-test" , async : "async" } ;
options . headers = { "x-custom-test-header" : "test value" } ;
xhr = jQuery . ajax ( url ( "mock.php?action=script&callback=corsCallback" ) , options ) ;
assert . equal ( jQuery ( "#jquery-ajax-test" ) . attr ( "async" ) , "async" , "attr value" ) ;
return xhr ;
} ,
success : function ( ) {
assert . ok ( true , "success" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
}
) ;
} ) ;
2015-09-10 10:40:00 +00:00
2021-01-26 14:58:29 +00:00
ajaxTest ( "jQuery.ajax() - execute JS when dataType option is provided" , 3 ,
2017-09-12 16:40:00 +00:00
function ( assert ) {
return {
create : function ( options ) {
2023-02-01 12:40:55 +00:00
Globals . register ( "corsCallback" ) ;
2021-01-26 14:58:29 +00:00
options . crossDomain = true ;
2017-09-12 16:40:00 +00:00
options . dataType = "script" ;
2021-01-26 14:58:29 +00:00
return jQuery . ajax ( url ( "mock.php?action=script&header=ecma" ) , options ) ;
2020-08-25 19:28:30 +00:00
} ,
2017-09-12 16:40:00 +00:00
success : function ( ) {
assert . ok ( true , "success" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
}
) ;
2021-01-26 14:58:29 +00:00
jQuery . each ( [ " - Same Domain" , " - Cross Domain" ] , function ( crossDomain , label ) {
ajaxTest ( "jQuery.ajax() - do not execute JS (gh-2432, gh-4822) " + label , 1 , function ( assert ) {
return {
url : url ( "mock.php?action=script&header" ) ,
crossDomain : crossDomain ,
success : function ( ) {
assert . ok ( true , "success" ) ;
}
} ;
} ) ;
2015-09-10 10:40:00 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - success callbacks (late binding)" , 8 , function ( assert ) {
return {
2015-08-16 06:59:58 +00:00
setup : addGlobalEvents ( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess" , assert ) ,
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( ) {
assert . ok ( true , "beforeSend" ) ;
} ,
success : true ,
afterSend : function ( request ) {
2015-08-16 06:59:58 +00:00
request . always ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "complete" ) ;
2015-08-16 06:59:58 +00:00
} ) . done ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "success" ) ;
2015-08-16 06:59:58 +00:00
} ) . fail ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( false , "error" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-12-09 18:34:28 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - success callbacks (oncomplete binding)" , 8 , function ( assert ) {
return {
2015-08-16 06:59:58 +00:00
setup : addGlobalEvents ( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess" , assert ) ,
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( ) {
assert . ok ( true , "beforeSend" ) ;
} ,
success : true ,
complete : function ( xhr ) {
2015-08-16 06:59:58 +00:00
xhr . always ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "complete" ) ;
2015-08-16 06:59:58 +00:00
} ) . done ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "success" ) ;
2015-08-16 06:59:58 +00:00
} ) . fail ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( false , "error" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-12-30 06:34:48 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - error callbacks" , 8 , function ( assert ) {
return {
2015-08-16 06:59:58 +00:00
setup : addGlobalEvents ( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError" , assert ) ,
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=wait&wait=5" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( ) {
assert . ok ( true , "beforeSend" ) ;
} ,
afterSend : function ( request ) {
request . abort ( ) ;
} ,
error : function ( ) {
assert . ok ( true , "error" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2011-01-09 05:19:27 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - textStatus and errorThrown values" , 4 , function ( assert ) {
2015-08-16 06:59:58 +00:00
return [ {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=wait&wait=5" ) ,
2012-10-15 19:53:39 +00:00
error : function ( _ , textStatus , errorThrown ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( textStatus , "abort" , "textStatus is 'abort' for abort" ) ;
assert . strictEqual ( errorThrown , "abort" , "errorThrown is 'abort' for abort" ) ;
2012-11-26 02:31:19 +00:00
} ,
afterSend : function ( request ) {
request . abort ( ) ;
2012-10-15 19:53:39 +00:00
}
2012-11-26 02:31:19 +00:00
} ,
{
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=wait&wait=5" ) ,
2012-10-15 19:53:39 +00:00
error : function ( _ , textStatus , errorThrown ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( textStatus , "mystatus" , "textStatus is 'mystatus' for abort('mystatus')" ) ;
assert . strictEqual ( errorThrown , "mystatus" , "errorThrown is 'mystatus' for abort('mystatus')" ) ;
2012-10-15 19:53:39 +00:00
} ,
2012-11-26 02:31:19 +00:00
afterSend : function ( request ) {
2015-08-16 06:59:58 +00:00
request . abort ( "mystatus" ) ;
2011-02-17 16:03:09 +00:00
}
2015-08-16 06:59:58 +00:00
} ] ;
} ) ;
2011-01-11 22:01:18 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - responseText on error" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=error" ) ,
2015-08-16 03:45:28 +00:00
error : function ( xhr ) {
assert . strictEqual ( xhr . responseText , "plain text message" , "Test jqXHR.responseText is filled for HTTP errors" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2011-01-11 22:01:18 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.ajax() - retry with jQuery.ajax( this )" , function ( assert ) {
assert . expect ( 2 ) ;
2012-10-15 19:53:39 +00:00
var previousUrl ,
2019-02-18 18:02:38 +00:00
firstTime = true ,
done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=error" ) ,
2012-10-15 19:53:39 +00:00
error : function ( ) {
if ( firstTime ) {
firstTime = false ;
jQuery . ajax ( this ) ;
} else {
2015-09-08 00:26:29 +00:00
assert . ok ( true , "Test retrying with jQuery.ajax(this) works" ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=error&x=2" ) ,
2012-10-15 19:53:39 +00:00
beforeSend : function ( ) {
if ( ! previousUrl ) {
previousUrl = this . url ;
} else {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( this . url , previousUrl , "url parameters are not re-appended" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-10-15 19:53:39 +00:00
return false ;
}
} ,
error : function ( ) {
jQuery . ajax ( this ) ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-10-15 19:53:39 +00:00
}
}
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2011-01-11 22:01:18 +00:00
2018-11-26 17:00:41 +00:00
ajaxTest ( "jQuery.ajax() - headers" , 8 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
setup : function ( ) {
2020-01-07 22:59:08 +00:00
jQuery ( document ) . on ( "ajaxSend" , function ( evt , xhr ) {
2015-08-16 03:45:28 +00:00
xhr . setRequestHeader ( "ajax-send" , "test" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
} ,
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=headers&keys=siMPle|SometHing-elsE|OthEr|Nullable|undefined|Empty|ajax-send" ) ,
2019-05-13 19:39:56 +00:00
headers : supportjQuery . extend ( {
2015-08-16 03:45:28 +00:00
"siMPle" : "value" ,
"SometHing-elsE" : "other value" ,
"OthEr" : "something else" ,
"Nullable" : null ,
"undefined" : undefined
2020-09-22 15:49:28 +00:00
// Support: IE 9 - 11+
2019-05-13 19:39:56 +00:00
// IE can receive empty headers but not send them.
} , QUnit . isIE ? { } : {
"Empty" : ""
} ) ,
2015-08-16 03:45:28 +00:00
success : function ( data , _ , xhr ) {
2019-05-13 19:39:56 +00:00
var i ,
2015-08-16 03:45:28 +00:00
requestHeaders = jQuery . extend ( this . headers , {
"ajax-send" : "test"
2015-08-16 06:59:58 +00:00
} ) ,
2015-08-16 03:45:28 +00:00
tmp = [ ] ;
for ( i in requestHeaders ) {
tmp . push ( i , ": " , requestHeaders [ i ] + "" , "\n" ) ;
}
2015-08-16 06:59:58 +00:00
tmp = tmp . join ( "" ) ;
2012-12-31 18:31:03 +00:00
2015-08-16 03:45:28 +00:00
assert . strictEqual ( data , tmp , "Headers were sent" ) ;
2015-08-16 06:59:58 +00:00
assert . strictEqual ( xhr . getResponseHeader ( "Sample-Header" ) , "Hello World" , "Sample header received" ) ;
assert . ok ( data . indexOf ( "undefined" ) < 0 , "Undefined header value was not sent" ) ;
2019-05-13 19:39:56 +00:00
assert . strictEqual ( xhr . getResponseHeader ( "Empty-Header" ) , "" , "Empty header received" ) ;
2015-08-16 06:59:58 +00:00
assert . strictEqual ( xhr . getResponseHeader ( "Sample-Header2" ) , "Hello World 2" , "Second sample header received" ) ;
2019-04-29 20:56:09 +00:00
assert . strictEqual ( xhr . getResponseHeader ( "List-Header" ) , "Item 1, Item 2" , "List header received" ) ;
assert . strictEqual ( xhr . getResponseHeader ( "constructor" ) , "prototype collision (constructor)" , "constructor header received" ) ;
2018-11-26 17:00:41 +00:00
assert . strictEqual ( xhr . getResponseHeader ( "__proto__" ) , null , "Undefined __proto__ header not received" ) ;
2012-11-26 02:31:19 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2011-01-09 03:28:42 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - Accept header" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=headers&keys=accept" ) ,
2015-08-16 03:45:28 +00:00
headers : {
Accept : "very wrong accept value"
} ,
beforeSend : function ( xhr ) {
2015-08-16 06:59:58 +00:00
xhr . setRequestHeader ( "Accept" , "*/*" ) ;
2015-08-16 03:45:28 +00:00
} ,
2012-10-15 19:53:39 +00:00
success : function ( data ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( data , "accept: */*\n" , "Test Accept header is set to last value provided" ) ;
2012-10-15 19:53:39 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2011-01-09 15:50:13 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - contentType" , 2 , function ( assert ) {
return [
{
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=headers&keys=content-type" ) ,
2015-08-16 03:45:28 +00:00
contentType : "test" ,
success : function ( data ) {
assert . strictEqual ( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" ) ;
}
2012-11-26 02:31:19 +00:00
} ,
2015-08-16 03:45:28 +00:00
{
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=headers&keys=content-type" ) ,
2015-08-16 03:45:28 +00:00
contentType : false ,
success : function ( data ) {
2015-08-16 06:59:58 +00:00
2015-08-16 03:45:28 +00:00
// Some server/interpreter combinations always supply a Content-Type to scripts
data = data || "content-type: \n" ;
assert . strictEqual ( data , "content-type: \n" , "Test content-type is not set when options.contentType===false" ) ;
}
}
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - protocol-less urls" , 1 , function ( assert ) {
return {
url : "//somedomain.com" ,
2012-10-15 19:53:39 +00:00
beforeSend : function ( xhr , settings ) {
2015-08-16 03:45:28 +00:00
assert . equal ( settings . url , location . protocol + "//somedomain.com" , "Make sure that the protocol is added." ) ;
2012-10-15 19:53:39 +00:00
return false ;
2012-11-26 02:31:19 +00:00
} ,
error : true
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
2018-09-07 14:14:01 +00:00
ajaxTest ( "jQuery.ajax() - URL fragment component preservation" , 4 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return [
{
2017-08-01 16:52:45 +00:00
url : baseURL + "name.html#foo" ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( xhr , settings ) {
2018-09-07 14:14:01 +00:00
assert . equal ( settings . url , baseURL + "name.html#foo" ,
"hash preserved for request with no query component." ) ;
2015-08-16 03:45:28 +00:00
return false ;
} ,
error : true
2012-10-15 19:53:39 +00:00
} ,
2015-08-16 03:45:28 +00:00
{
2017-08-01 16:52:45 +00:00
url : baseURL + "name.html?abc#foo" ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( xhr , settings ) {
2018-09-07 14:14:01 +00:00
assert . equal ( settings . url , baseURL + "name.html?abc#foo" ,
"hash preserved for request with query component." ) ;
2015-08-16 03:45:28 +00:00
return false ;
} ,
error : true
2012-11-26 02:31:19 +00:00
} ,
2015-08-16 03:45:28 +00:00
{
2017-08-01 16:52:45 +00:00
url : baseURL + "name.html?abc#foo" ,
2015-08-16 03:45:28 +00:00
data : {
"test" : 123
} ,
beforeSend : function ( xhr , settings ) {
2018-09-07 14:14:01 +00:00
assert . equal ( settings . url , baseURL + "name.html?abc&test=123#foo" ,
"hash preserved for request with query component and data." ) ;
2015-11-16 02:51:18 +00:00
return false ;
} ,
error : true
} ,
{
2017-08-01 16:52:45 +00:00
url : baseURL + "name.html?abc#brownies" ,
2015-11-16 02:51:18 +00:00
data : {
"devo" : "hat"
} ,
cache : false ,
beforeSend : function ( xhr , settings ) {
2018-09-07 14:14:01 +00:00
// Clear the cache-buster param value
var url = settings . url . replace ( /_=[^&#]+/ , "_=" ) ;
assert . equal ( url , baseURL + "name.html?abc&devo=hat&_=#brownies" ,
"hash preserved for cache-busting request with query component and data." ) ;
2015-08-16 03:45:28 +00:00
return false ;
} ,
error : true
}
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-01-06 17:13:56 +00:00
2016-04-26 14:28:02 +00:00
ajaxTest ( "jQuery.ajax() - traditional param encoding" , 4 , function ( assert ) {
return [
{
url : "/" ,
traditional : true ,
data : {
"devo" : "hat" ,
"answer" : 42 ,
"quux" : "a space"
} ,
beforeSend : function ( xhr , settings ) {
assert . equal ( settings . url , "/?devo=hat&answer=42&quux=a%20space" , "Simple case" ) ;
return false ;
} ,
error : true
} ,
{
url : "/" ,
traditional : true ,
data : {
"a" : [ 1 , 2 , 3 ] ,
"b[]" : [ "b1" , "b2" ]
} ,
beforeSend : function ( xhr , settings ) {
assert . equal ( settings . url , "/?a=1&a=2&a=3&b%5B%5D=b1&b%5B%5D=b2" , "Arrays" ) ;
return false ;
} ,
error : true
} ,
{
url : "/" ,
traditional : true ,
data : {
"a" : [ [ 1 , 2 ] , [ 3 , 4 ] , 5 ]
} ,
beforeSend : function ( xhr , settings ) {
assert . equal ( settings . url , "/?a=1%2C2&a=3%2C4&a=5" , "Nested arrays" ) ;
return false ;
} ,
error : true
} ,
{
url : "/" ,
traditional : true ,
data : {
"a" : [ "w" , [ [ "x" , "y" ] , "z" ] ]
} ,
cache : false ,
beforeSend : function ( xhr , settings ) {
var url = settings . url . replace ( /\d{3,}/ , "" ) ;
assert . equal ( url , "/?a=w&a=x%2Cy%2Cz&_=" , "Cache-buster" ) ;
return false ;
} ,
error : true
}
] ;
} ) ;
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - cross-domain detection" , 8 , function ( assert ) {
2012-11-26 02:31:19 +00:00
function request ( url , title , crossDomainOrOptions ) {
return jQuery . extend ( {
dataType : "jsonp" ,
url : url ,
beforeSend : function ( _ , s ) {
2015-08-16 03:45:28 +00:00
assert . ok ( crossDomainOrOptions === false ? ! s . crossDomain : s . crossDomain , title ) ;
2012-11-26 02:31:19 +00:00
return false ;
} ,
error : true
} , crossDomainOrOptions ) ;
}
2010-11-06 18:52:30 +00:00
2012-10-15 19:53:39 +00:00
var loc = document . location ,
samePort = loc . port || ( loc . protocol === "http:" ? 80 : 443 ) ,
otherPort = loc . port === 666 ? 667 : 666 ,
otherProtocol = loc . protocol === "http:" ? "https:" : "http:" ;
2012-12-31 18:31:03 +00:00
2012-11-26 02:31:19 +00:00
return [
request (
2014-12-19 12:23:02 +00:00
loc . protocol + "//" + loc . hostname + ":" + samePort ,
2012-11-26 02:31:19 +00:00
"Test matching ports are not detected as cross-domain" ,
false
) ,
request (
otherProtocol + "//" + loc . host ,
"Test different protocols are detected as cross-domain"
) ,
request (
"app:/path" ,
"Adobe AIR app:/ URL detected as cross-domain"
) ,
request (
loc . protocol + "//example.invalid:" + ( loc . port || 80 ) ,
"Test different hostnames are detected as cross-domain"
) ,
request (
loc . protocol + "//" + loc . hostname + ":" + otherPort ,
"Test different ports are detected as cross-domain"
) ,
request (
"about:blank" ,
"Test about:blank is detected as cross-domain"
) ,
request (
loc . protocol + "//" + loc . host ,
"Test forced crossDomain is detected as cross-domain" ,
{
crossDomain : true
}
2014-11-23 01:29:13 +00:00
) ,
request (
2019-10-21 17:03:48 +00:00
" https://otherdomain.com" ,
2014-11-23 01:29:13 +00:00
"Cross-domain url with leading space is detected as cross-domain"
2012-11-26 02:31:19 +00:00
)
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2009-09-15 15:19:58 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - abort" , 9 , function ( assert ) {
return {
2015-08-16 06:59:58 +00:00
setup : addGlobalEvents ( "ajaxStart ajaxStop ajaxSend ajaxError ajaxComplete" , assert ) ,
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=wait&wait=5" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( ) {
assert . ok ( true , "beforeSend" ) ;
} ,
afterSend : function ( xhr ) {
assert . strictEqual ( xhr . readyState , 1 , "XHR readyState indicates successful dispatch" ) ;
xhr . abort ( ) ;
assert . strictEqual ( xhr . readyState , 0 , "XHR readyState indicates successful abortion" ) ;
} ,
error : true ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2011-01-20 03:12:15 +00:00
2019-04-29 20:56:09 +00:00
ajaxTest ( "jQuery.ajax() - native abort" , 2 , function ( assert ) {
return {
url : url ( "mock.php?action=wait&wait=1" ) ,
xhr : function ( ) {
var xhr = new window . XMLHttpRequest ( ) ;
setTimeout ( function ( ) {
xhr . abort ( ) ;
} , 100 ) ;
return xhr ;
} ,
error : function ( xhr , msg ) {
assert . strictEqual ( msg , "error" , "Native abort triggers error callback" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
} ) ;
2017-03-23 13:33:25 +00:00
2019-04-29 20:56:09 +00:00
ajaxTest ( "jQuery.ajax() - native timeout" , 2 , function ( assert ) {
return {
url : url ( "mock.php?action=wait&wait=1" ) ,
xhr : function ( ) {
var xhr = new window . XMLHttpRequest ( ) ;
xhr . timeout = 1 ;
return xhr ;
} ,
error : function ( xhr , msg ) {
assert . strictEqual ( msg , "error" , "Native timeout triggers error callback" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
} ) ;
2015-11-02 17:00:28 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - events with context" , 12 , function ( assert ) {
2015-08-16 06:59:58 +00:00
var context = document . createElement ( "div" ) ;
2011-01-20 03:12:15 +00:00
2012-10-15 19:53:39 +00:00
function event ( e ) {
2015-08-16 03:45:28 +00:00
assert . equal ( this , context , e . type ) ;
2009-05-01 00:59:27 +00:00
}
2007-09-03 23:45:14 +00:00
2012-10-15 19:53:39 +00:00
function callback ( msg ) {
return function ( ) {
2015-08-16 03:45:28 +00:00
assert . equal ( this , context , "context is preserved on callback " + msg ) ;
2012-10-15 19:53:39 +00:00
} ;
2012-05-18 17:28:50 +00:00
}
2007-09-03 23:45:14 +00:00
2012-11-26 02:31:19 +00:00
return {
setup : function ( ) {
2015-08-16 06:59:58 +00:00
jQuery ( context ) . appendTo ( "#foo" )
2020-01-07 22:59:08 +00:00
. on ( "ajaxSend" , event )
. on ( "ajaxComplete" , event )
. on ( "ajaxError" , event )
. on ( "ajaxSuccess" , event ) ;
2012-11-26 02:31:19 +00:00
} ,
2015-08-16 06:59:58 +00:00
requests : [ {
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2012-12-05 13:54:14 +00:00
context : context ,
2015-08-16 06:59:58 +00:00
beforeSend : callback ( "beforeSend" ) ,
success : callback ( "success" ) ,
complete : callback ( "complete" )
2012-11-26 02:31:19 +00:00
} , {
2017-08-01 16:52:45 +00:00
url : url ( "404.txt" ) ,
2012-12-05 13:54:14 +00:00
context : context ,
2015-08-16 06:59:58 +00:00
beforeSend : callback ( "beforeSend" ) ,
error : callback ( "error" ) ,
complete : callback ( "complete" )
} ]
2012-11-26 02:31:19 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-31 18:31:03 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - events without context" , 3 , function ( assert ) {
2012-10-15 19:53:39 +00:00
function nocallback ( msg ) {
return function ( ) {
2015-08-16 03:45:28 +00:00
assert . equal ( typeof this . url , "string" , "context is settings on callback " + msg ) ;
2012-10-15 19:53:39 +00:00
} ;
2012-05-18 17:28:50 +00:00
}
2012-11-26 02:31:19 +00:00
return {
2017-08-01 16:52:45 +00:00
url : url ( "404.txt" ) ,
2015-08-16 06:59:58 +00:00
beforeSend : nocallback ( "beforeSend" ) ,
error : nocallback ( "error" ) ,
complete : nocallback ( "complete" )
2012-11-26 02:31:19 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-12-09 18:34:28 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-15118 - jQuery.ajax() - function without jQuery.event" , 1 , function ( assert ) {
2014-05-30 13:48:43 +00:00
var holder ;
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=json" ) ,
2014-05-30 13:48:43 +00:00
setup : function ( ) {
holder = jQuery . event ;
delete jQuery . event ;
} ,
complete : function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "Call can be made without jQuery.event" ) ;
2014-05-30 13:48:43 +00:00
jQuery . event = holder ;
} ,
success : true
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2014-05-30 13:48:43 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-15160 - jQuery.ajax() - request manually aborted in ajaxSend" , 3 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
setup : function ( ) {
jQuery ( document ) . on ( "ajaxSend" , function ( e , jqXHR ) {
jqXHR . abort ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2014-07-13 17:53:00 +00:00
2015-08-16 03:45:28 +00:00
jQuery ( document ) . on ( "ajaxError ajaxComplete" , function ( e , jqXHR ) {
assert . equal ( jqXHR . statusText , "abort" , "jqXHR.statusText equals abort on global ajaxComplete and ajaxError events" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
} ,
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
error : true ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2014-07-13 17:53:00 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - context modification" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
context : { } ,
beforeSend : function ( ) {
this . test = "foo" ;
} ,
afterSend : function ( ) {
assert . strictEqual ( this . context . test , "foo" , "Make sure the original object is maintained." ) ;
} ,
success : true
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-10-15 19:53:39 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - context modification through ajaxSetup" , 3 , function ( assert ) {
2012-11-26 02:31:19 +00:00
var obj = { } ;
return {
setup : function ( ) {
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
context : obj
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
assert . strictEqual ( jQuery . ajaxSettings . context , obj , "Make sure the context is properly set in ajaxSettings." ) ;
2012-10-15 19:53:39 +00:00
} ,
2015-08-16 06:59:58 +00:00
requests : [ {
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2012-12-05 13:54:14 +00:00
success : function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( this , obj , "Make sure the original object is maintained." ) ;
2012-11-26 02:31:19 +00:00
}
} , {
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2012-11-26 02:31:19 +00:00
context : { } ,
2012-12-05 13:54:14 +00:00
success : function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( this !== obj , "Make sure overriding context is possible." ) ;
2012-11-26 02:31:19 +00:00
}
2015-08-16 06:59:58 +00:00
} ]
2012-11-26 02:31:19 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-10-23 17:23:51 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - disabled globals" , 3 , function ( assert ) {
return {
setup : addGlobalEvents ( "" , assert ) ,
global : false ,
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( ) {
assert . ok ( true , "beforeSend" ) ;
} ,
success : function ( ) {
assert . ok ( true , "success" ) ;
} ,
complete : function ( ) {
assert . ok ( true , "complete" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2009-05-01 00:59:27 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements" , 3 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "with_fries.xml" ) ,
2015-08-16 03:45:28 +00:00
dataType : "xml" ,
success : function ( resp ) {
assert . equal ( jQuery ( "properties" , resp ) . length , 1 , "properties in responseXML" ) ;
assert . equal ( jQuery ( "jsconf" , resp ) . length , 1 , "jsconf in responseXML" ) ;
assert . equal ( jQuery ( "thing" , resp ) . length , 2 , "things in responseXML" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2009-05-01 00:59:27 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements (over JSONP)" , 3 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=xmlOverJsonp" ) ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp xml" ,
success : function ( resp ) {
assert . equal ( jQuery ( "properties" , resp ) . length , 1 , "properties in responseXML" ) ;
assert . equal ( jQuery ( "jsconf" , resp ) . length , 1 , "jsconf in responseXML" ) ;
assert . equal ( jQuery ( "thing" , resp ) . length , 2 , "things in responseXML" ) ;
2012-12-05 13:54:14 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2008-04-22 22:07:17 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - HEAD requests" , 2 , function ( assert ) {
return [
{
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
type : "HEAD" ,
success : function ( data , status , xhr ) {
assert . ok ( /Date/i . test ( xhr . getAllResponseHeaders ( ) ) , "No Date in HEAD response" ) ;
}
} ,
{
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
data : {
"whip_it" : "good"
2012-11-26 02:31:19 +00:00
} ,
2015-08-16 03:45:28 +00:00
type : "HEAD" ,
success : function ( data , status , xhr ) {
assert . ok ( /Date/i . test ( xhr . getAllResponseHeaders ( ) ) , "No Date in HEAD response with data" ) ;
2012-11-26 02:31:19 +00:00
}
2015-08-16 03:45:28 +00:00
}
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2006-12-31 15:44:59 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - beforeSend" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( ) {
this . check = true ;
} ,
success : function ( ) {
assert . ok ( this . check , "check beforeSend was executed" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2011-01-11 19:02:33 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - beforeSend, cancel request manually" , 2 , function ( assert ) {
return {
create : function ( ) {
2015-08-16 06:59:58 +00:00
return jQuery . ajax ( {
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( xhr ) {
assert . ok ( true , "beforeSend got called, canceling" ) ;
xhr . abort ( ) ;
} ,
success : function ( ) {
assert . ok ( false , "request didn't get canceled" ) ;
} ,
complete : function ( ) {
assert . ok ( false , "request didn't get canceled" ) ;
} ,
error : function ( ) {
assert . ok ( false , "request didn't get canceled" ) ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
} ,
fail : function ( _ , reason ) {
assert . strictEqual ( reason , "canceled" , "canceled request must fail with 'canceled' status text" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - dataType html" , 5 , function ( assert ) {
return {
setup : function ( ) {
2015-08-16 06:59:58 +00:00
Globals . register ( "testFoo" ) ;
Globals . register ( "testBar" ) ;
2015-08-16 03:45:28 +00:00
} ,
dataType : "html" ,
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=testHTML&baseURL=" + baseURL ) ,
2015-08-16 03:45:28 +00:00
success : function ( data ) {
assert . ok ( data . match ( /^html text/ ) , "Check content for datatype html" ) ;
2015-08-16 06:59:58 +00:00
jQuery ( "#ap" ) . html ( data ) ;
assert . strictEqual ( window [ "testFoo" ] , "foo" , "Check if script was evaluated for datatype html" ) ;
assert . strictEqual ( window [ "testBar" ] , "bar" , "Check if script src was evaluated for datatype html" ) ;
2015-08-16 03:45:28 +00:00
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
2020-08-25 19:41:06 +00:00
ajaxTest ( "jQuery.ajax() - do execute scripts if JSONP from unsuccessful responses" , 1 , function ( assert ) {
var testMsg = "Unsuccessful JSONP requests should have a JSON body" ;
return {
dataType : "jsonp" ,
url : url ( "mock.php?action=errorWithScript" ) ,
// error is the significant assertion
error : function ( xhr ) {
var expected = { "status" : 404 , "msg" : "Not Found" } ;
assert . deepEqual ( xhr . responseJSON , expected , testMsg ) ;
}
} ;
} ) ;
2019-04-26 14:25:08 +00:00
ajaxTest ( "jQuery.ajax() - do not execute scripts from unsuccessful responses (gh-4250)" , 11 , function ( assert ) {
var globalEval = jQuery . globalEval ;
var failConverters = {
"text script" : function ( ) {
assert . ok ( false , "No converter for unsuccessful response" ) ;
}
} ;
function request ( title , options ) {
var testMsg = title + ": expected file missing status" ;
return jQuery . extend ( {
beforeSend : function ( ) {
jQuery . globalEval = function ( ) {
assert . ok ( false , "Should not eval" ) ;
} ;
} ,
complete : function ( ) {
jQuery . globalEval = globalEval ;
} ,
// error is the significant assertion
error : function ( xhr ) {
assert . strictEqual ( xhr . status , 404 , testMsg ) ;
} ,
success : function ( ) {
assert . ok ( false , "Unanticipated success" ) ;
}
} , options ) ;
}
return [
request (
"HTML reply" ,
{
url : url ( "404.txt" )
}
) ,
request (
"HTML reply with dataType" ,
{
dataType : "script" ,
url : url ( "404.txt" )
}
) ,
request (
"script reply" ,
{
url : url ( "mock.php?action=errorWithScript&withScriptContentType" )
}
) ,
request (
"non-script reply" ,
{
url : url ( "mock.php?action=errorWithScript" )
}
) ,
request (
"script reply with dataType" ,
{
dataType : "script" ,
url : url ( "mock.php?action=errorWithScript&withScriptContentType" )
}
) ,
request (
"non-script reply with dataType" ,
{
dataType : "script" ,
url : url ( "mock.php?action=errorWithScript" )
}
) ,
request (
"script reply with converter" ,
{
converters : failConverters ,
url : url ( "mock.php?action=errorWithScript&withScriptContentType" )
}
) ,
request (
"non-script reply with converter" ,
{
converters : failConverters ,
url : url ( "mock.php?action=errorWithScript" )
}
) ,
request (
"script reply with converter and dataType" ,
{
converters : failConverters ,
dataType : "script" ,
url : url ( "mock.php?action=errorWithScript&withScriptContentType" )
}
) ,
request (
"non-script reply with converter and dataType" ,
{
converters : failConverters ,
dataType : "script" ,
url : url ( "mock.php?action=errorWithScript" )
}
) ,
request (
"JSONP reply with dataType" ,
{
dataType : "jsonp" ,
url : url ( "mock.php?action=errorWithScript" ) ,
beforeSend : function ( ) {
jQuery . globalEval = function ( response ) {
assert . ok ( /"status": 404, "msg": "Not Found"/ . test ( response ) , "Error object returned" ) ;
} ;
}
}
)
] ;
} ) ;
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - synchronous request" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "json_obj.js" ) ,
2015-08-16 03:45:28 +00:00
dataType : "text" ,
async : false ,
success : true ,
afterSend : function ( xhr ) {
assert . ok ( /^\{ "data"/ . test ( xhr . responseText ) , "check returned text" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - synchronous request with callbacks" , 2 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "json_obj.js" ) ,
2015-08-16 03:45:28 +00:00
async : false ,
dataType : "text" ,
success : true ,
afterSend : function ( xhr ) {
var result ;
2015-08-16 06:59:58 +00:00
xhr . done ( function ( data ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "success callback executed" ) ;
result = data ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
assert . ok ( /^\{ "data"/ . test ( result ) , "check returned text" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.ajax(), jQuery.get[Script|JSON](), jQuery.post(), pass-through request object" , function ( assert ) {
assert . expect ( 8 ) ;
var done = assert . async ( ) ;
2017-08-01 16:52:45 +00:00
var target = "name.html" ,
2015-08-16 03:45:28 +00:00
successCount = 0 ,
errorCount = 0 ,
errorEx = "" ,
2013-04-09 15:45:09 +00:00
success = function ( ) {
successCount ++ ;
} ;
jQuery ( document ) . on ( "ajaxError.passthru" , function ( e , xml ) {
2012-10-15 19:53:39 +00:00
errorCount ++ ;
2012-12-05 13:54:14 +00:00
errorEx += ": " + xml . status ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
jQuery ( document ) . one ( "ajaxStop" , function ( ) {
2015-08-16 03:45:28 +00:00
assert . equal ( successCount , 5 , "Check all ajax calls successful" ) ;
assert . equal ( errorCount , 0 , "Check no ajax errors (status" + errorEx + ")" ) ;
2015-08-16 06:59:58 +00:00
jQuery ( document ) . off ( "ajaxError.passthru" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
Globals . register ( "testBar" ) ;
assert . ok ( jQuery . get ( url ( target ) , success ) , "get" ) ;
assert . ok ( jQuery . post ( url ( target ) , success ) , "post" ) ;
2017-08-01 16:52:45 +00:00
assert . ok ( jQuery . getScript ( url ( "mock.php?action=testbar" ) , success ) , "script" ) ;
assert . ok ( jQuery . getJSON ( url ( "json_obj.js" ) , success ) , "json" ) ;
2015-08-16 06:59:58 +00:00
assert . ok ( jQuery . ajax ( {
2012-12-05 13:54:14 +00:00
url : url ( target ) ,
2012-10-15 19:53:39 +00:00
success : success
2015-08-16 06:59:58 +00:00
} ) , "generic" ) ;
} ) ;
2008-05-27 22:46:28 +00:00
2016-07-29 19:50:07 +00:00
ajaxTest ( "jQuery.ajax() - cache" , 28 , function ( assert ) {
var re = /_=(.*?)(&|$)/g ,
2017-08-01 16:52:45 +00:00
rootUrl = baseURL + "text.txt" ;
2012-12-31 18:31:03 +00:00
2012-11-26 02:31:19 +00:00
function request ( url , title ) {
return {
url : url ,
cache : false ,
beforeSend : function ( ) {
var parameter , tmp ;
2016-07-29 19:50:07 +00:00
// URL sanity check
assert . equal ( this . url . indexOf ( rootUrl ) , 0 , "root url not mangled: " + this . url ) ;
assert . equal ( /\&.*\?/ . test ( this . url ) , false , "parameter delimiters in order" ) ;
2015-08-16 06:59:58 +00:00
while ( ( tmp = re . exec ( this . url ) ) ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( parameter , undefined , title + ": only one 'no-cache' parameter" ) ;
2012-11-26 02:31:19 +00:00
parameter = tmp [ 1 ] ;
2015-08-16 03:45:28 +00:00
assert . notStrictEqual ( parameter , "tobereplaced555" , title + ": parameter (if it was there) was replaced" ) ;
2012-11-26 02:31:19 +00:00
}
return false ;
} ,
error : true
2012-10-15 19:53:39 +00:00
} ;
2012-11-26 02:31:19 +00:00
}
2012-12-31 18:31:03 +00:00
2012-11-26 02:31:19 +00:00
return [
request (
2016-07-29 19:50:07 +00:00
rootUrl ,
"no query"
) ,
request (
rootUrl + "?" ,
"empty query"
2012-11-26 02:31:19 +00:00
) ,
request (
2016-07-29 19:50:07 +00:00
rootUrl + "?pizza=true" ,
2012-11-26 02:31:19 +00:00
"1 parameter"
) ,
request (
2016-07-29 19:50:07 +00:00
rootUrl + "?_=tobereplaced555" ,
2012-11-26 02:31:19 +00:00
"_= parameter"
) ,
request (
2016-07-29 19:50:07 +00:00
rootUrl + "?pizza=true&_=tobereplaced555" ,
2012-11-26 02:31:19 +00:00
"1 parameter and _="
) ,
request (
2016-07-29 19:50:07 +00:00
rootUrl + "?_=tobereplaced555&tv=false" ,
2012-11-26 02:31:19 +00:00
"_= and 1 parameter"
) ,
request (
2016-07-29 19:50:07 +00:00
rootUrl + "?name=David&_=tobereplaced555&washere=true" ,
2012-11-26 02:31:19 +00:00
"2 parameters surrounding _="
)
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-05-02 00:22:56 +00:00
2012-11-26 02:31:19 +00:00
jQuery . each ( [ " - Same Domain" , " - Cross Domain" ] , function ( crossDomain , label ) {
2012-10-15 19:53:39 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - JSONP - Query String (?n)" + label , 4 , function ( assert ) {
return [
{
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp&callback=?" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : function ( data ) {
assert . ok ( data . data , "JSON results returned (GET, url callback)" ) ;
}
} ,
{
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp&callback=??" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : function ( data ) {
assert . ok ( data . data , "JSON results returned (GET, url context-free callback)" ) ;
}
} ,
{
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php/???action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : function ( data ) {
assert . ok ( data . data , "JSON results returned (GET, REST-like)" ) ;
}
} ,
{
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php/???action=jsonp&array=1" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : function ( data ) {
2018-01-14 08:46:20 +00:00
assert . ok ( Array . isArray ( data ) , "JSON results returned (GET, REST-like with param)" ) ;
2015-08-16 03:45:28 +00:00
}
2012-12-05 13:54:14 +00:00
}
2015-08-16 03:45:28 +00:00
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2007-08-19 23:37:26 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - JSONP - Explicit callback param" + label , 10 , function ( assert ) {
return {
setup : function ( ) {
2015-08-16 06:59:58 +00:00
Globals . register ( "functionToCleanUp" ) ;
Globals . register ( "XXX" ) ;
Globals . register ( "jsonpResults" ) ;
window [ "jsonpResults" ] = function ( data ) {
assert . ok ( data [ "data" ] , "JSON results returned (GET, custom callback function)" ) ;
2015-08-16 03:45:28 +00:00
} ;
2012-12-05 13:54:14 +00:00
} ,
2015-08-16 06:59:58 +00:00
requests : [ {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
jsonp : "callback" ,
success : function ( data ) {
2015-08-16 06:59:58 +00:00
assert . ok ( data [ "data" ] , "JSON results returned (GET, data obj callback)" ) ;
2015-08-16 03:45:28 +00:00
}
} , {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
jsonpCallback : "jsonpResults" ,
success : function ( data ) {
assert . strictEqual (
typeof window [ "jsonpResults" ] ,
"function" ,
"should not rewrite original function"
) ;
assert . ok ( data . data , "JSON results returned (GET, custom callback name)" ) ;
}
} , {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
jsonpCallback : "functionToCleanUp" ,
success : function ( data ) {
2015-08-16 06:59:58 +00:00
assert . ok ( data [ "data" ] , "JSON results returned (GET, custom callback name to be cleaned up)" ) ;
assert . strictEqual ( window [ "functionToCleanUp" ] , true , "Callback was removed (GET, custom callback name to be cleaned up)" ) ;
2015-08-16 03:45:28 +00:00
var xhr ;
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
jsonpCallback : "functionToCleanUp" ,
beforeSend : function ( jqXHR ) {
xhr = jqXHR ;
return false ;
}
2015-08-16 06:59:58 +00:00
} ) ;
xhr . fail ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "Ajax error JSON (GET, custom callback name to be cleaned up)" ) ;
2015-08-16 06:59:58 +00:00
assert . strictEqual ( window [ "functionToCleanUp" ] , true , "Callback was removed after early abort (GET, custom callback name to be cleaned up)" ) ;
} ) ;
2015-08-16 03:45:28 +00:00
}
} , {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp&callback=XXX" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
jsonp : false ,
jsonpCallback : "XXX" ,
crossDomain : crossDomain ,
beforeSend : function ( ) {
2017-08-01 16:52:45 +00:00
assert . ok ( /action=jsonp&callback=XXX&_=\d+$/ . test ( this . url ) , "The URL wasn't messed with (GET, custom callback name with no url manipulation)" ) ;
2015-08-16 03:45:28 +00:00
} ,
success : function ( data ) {
2015-08-16 06:59:58 +00:00
assert . ok ( data [ "data" ] , "JSON results returned (GET, custom callback name with no url manipulation)" ) ;
2015-08-16 03:45:28 +00:00
}
2015-08-16 06:59:58 +00:00
} ]
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2007-11-29 19:07:20 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - JSONP - Callback in data" + label , 2 , function ( assert ) {
return [
{
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
data : "callback=?" ,
success : function ( data ) {
assert . ok ( data . data , "JSON results returned (GET, data callback)" ) ;
}
} ,
{
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
data : "callback=??" ,
success : function ( data ) {
assert . ok ( data . data , "JSON results returned (GET, data context-free callback)" ) ;
}
2012-12-05 13:54:14 +00:00
}
2015-08-16 03:45:28 +00:00
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2006-11-18 13:37:01 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - JSONP - POST" + label , 3 , function ( assert ) {
return [
{
type : "POST" ,
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : function ( data ) {
2015-08-16 06:59:58 +00:00
assert . ok ( data [ "data" ] , "JSON results returned (POST, no callback)" ) ;
2015-08-16 03:45:28 +00:00
}
} ,
{
type : "POST" ,
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
data : "callback=?" ,
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : function ( data ) {
2015-08-16 06:59:58 +00:00
assert . ok ( data [ "data" ] , "JSON results returned (POST, data callback)" ) ;
2015-08-16 03:45:28 +00:00
}
} ,
{
type : "POST" ,
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
jsonp : "callback" ,
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : function ( data ) {
2015-08-16 06:59:58 +00:00
assert . ok ( data [ "data" ] , "JSON results returned (POST, data obj callback)" ) ;
2015-08-16 03:45:28 +00:00
}
2012-12-05 13:54:14 +00:00
}
2015-08-16 03:45:28 +00:00
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - JSONP" + label , 3 , function ( assert ) {
return [
{
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : function ( data ) {
assert . ok ( data . data , "JSON results returned (GET, no callback)" ) ;
}
} ,
{
create : function ( options ) {
var request = jQuery . ajax ( options ) ,
2015-08-16 06:59:58 +00:00
promise = request . then ( function ( data ) {
2015-08-16 03:45:28 +00:00
assert . ok ( data . data , "first request: JSON results returned (GET, no callback)" ) ;
2015-08-16 06:59:58 +00:00
request = jQuery . ajax ( this ) . done ( function ( data ) {
2015-08-16 03:45:28 +00:00
assert . ok ( data . data , "this re-used: JSON results returned (GET, no callback)" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
promise . abort = request . abort ;
return request ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
promise . abort = request . abort ;
return promise ;
} ,
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
success : true
2012-12-05 13:54:14 +00:00
}
2015-08-16 03:45:28 +00:00
] ;
2020-07-27 17:15:57 +00:00
} ) ;
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=??)" ) ;
}
}
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
2020-08-25 19:28:30 +00:00
testIframe (
"jQuery.ajax() - script, CSP script-src compat (gh-3969)" ,
"mock.php?action=cspAjaxScript" ,
function ( assert , jQuery , window , document , type , downloadedScriptCalled ) {
assert . expect ( 2 ) ;
assert . strictEqual ( type , "GET" , "Type changed to GET" ) ;
assert . strictEqual ( downloadedScriptCalled , true , "External script called" ) ;
}
) ;
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - script, Remote" , 2 , function ( assert ) {
return {
setup : function ( ) {
2015-08-16 06:59:58 +00:00
Globals . register ( "testBar" ) ;
2012-12-05 13:54:14 +00:00
} ,
2018-09-07 14:14:01 +00:00
url : url ( "mock.php?action=testbar" ) ,
2015-08-16 03:45:28 +00:00
dataType : "script" ,
success : function ( ) {
2015-08-16 06:59:58 +00:00
assert . strictEqual ( window [ "testBar" ] , "bar" , "Script results returned (GET, no callback)" ) ;
2012-12-05 13:54:14 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2009-05-01 00:59:27 +00:00
2020-08-25 19:28:30 +00:00
ajaxTest ( "jQuery.ajax() - script, Remote with POST" , 4 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
setup : function ( ) {
2015-08-16 06:59:58 +00:00
Globals . register ( "testBar" ) ;
2012-12-05 13:54:14 +00:00
} ,
2018-09-07 14:14:01 +00:00
url : url ( "mock.php?action=testbar" ) ,
2020-08-25 19:28:30 +00:00
beforeSend : function ( _jqXhr , settings ) {
assert . strictEqual ( settings . type , "GET" , "Type changed to GET" ) ;
} ,
2015-08-16 03:45:28 +00:00
type : "POST" ,
dataType : "script" ,
success : function ( data , status ) {
2015-08-16 06:59:58 +00:00
assert . strictEqual ( window [ "testBar" ] , "bar" , "Script results returned (POST, no callback)" ) ;
2015-08-16 03:45:28 +00:00
assert . strictEqual ( status , "success" , "Script results returned (POST, no callback)" ) ;
2012-12-05 13:54:14 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-10-15 16:37:02 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - script, Remote with scheme-less URL" , 2 , function ( assert ) {
return {
setup : function ( ) {
2015-08-16 06:59:58 +00:00
Globals . register ( "testBar" ) ;
2015-08-16 03:45:28 +00:00
} ,
2018-09-07 14:14:01 +00:00
url : url ( "mock.php?action=testbar" ) ,
2015-08-16 03:45:28 +00:00
dataType : "script" ,
success : function ( ) {
2015-08-16 06:59:58 +00:00
assert . strictEqual ( window [ "testBar" ] , "bar" , "Script results returned (GET, no callback)" ) ;
2015-08-16 03:45:28 +00:00
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2006-11-18 13:37:01 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - malformed JSON" , 2 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "badjson.js" ,
2015-08-16 03:45:28 +00:00
dataType : "json" ,
error : function ( xhr , msg , detailedMsg ) {
assert . strictEqual ( msg , "parsererror" , "A parse error occurred." ) ;
assert . ok ( /(invalid|error|exception)/i . test ( detailedMsg ) , "Detailed parsererror message provided" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - JSON by content-type" , 5 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=json" ,
2015-08-16 03:45:28 +00:00
data : {
"header" : "json" ,
2017-08-01 16:52:45 +00:00
"array" : "1"
2015-08-16 03:45:28 +00:00
} ,
success : function ( json ) {
assert . ok ( json . length >= 2 , "Check length" ) ;
2015-08-16 06:59:58 +00:00
assert . strictEqual ( json [ 0 ] [ "name" ] , "John" , "Check JSON: first, name" ) ;
assert . strictEqual ( json [ 0 ] [ "age" ] , 21 , "Check JSON: first, age" ) ;
assert . strictEqual ( json [ 1 ] [ "name" ] , "Peter" , "Check JSON: second, name" ) ;
assert . strictEqual ( json [ 1 ] [ "age" ] , 25 , "Check JSON: second, age" ) ;
2015-08-16 03:45:28 +00:00
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2006-11-18 13:37:01 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - JSON by content-type disabled with options" , 6 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=json" ) ,
2012-12-05 13:54:14 +00:00
data : {
2015-08-16 03:45:28 +00:00
"header" : "json" ,
2017-08-01 16:52:45 +00:00
"array" : "1"
2012-12-05 13:54:14 +00:00
} ,
2015-08-16 03:45:28 +00:00
contents : {
"json" : false
2012-12-05 13:54:14 +00:00
} ,
2015-08-16 03:45:28 +00:00
success : function ( text ) {
assert . strictEqual ( typeof text , "string" , "json wasn't auto-determined" ) ;
2016-02-24 22:47:19 +00:00
var json = JSON . parse ( text ) ;
2015-08-16 06:59:58 +00:00
assert . ok ( json . length >= 2 , "Check length" ) ;
assert . strictEqual ( json [ 0 ] [ "name" ] , "John" , "Check JSON: first, name" ) ;
assert . strictEqual ( json [ 0 ] [ "age" ] , 21 , "Check JSON: first, age" ) ;
assert . strictEqual ( json [ 1 ] [ "name" ] , "Peter" , "Check JSON: second, name" ) ;
assert . strictEqual ( json [ 1 ] [ "age" ] , 25 , "Check JSON: second, age" ) ;
2015-08-16 03:45:28 +00:00
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-06-15 03:08:28 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - simple get" , 1 , function ( assert ) {
return {
type : "GET" ,
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=name&name=foo" ) ,
2015-08-16 03:45:28 +00:00
success : function ( msg ) {
assert . strictEqual ( msg , "bar" , "Check for GET" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - simple post" , 1 , function ( assert ) {
return {
type : "POST" ,
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=name" ) ,
2015-08-16 03:45:28 +00:00
data : "name=peter" ,
success : function ( msg ) {
assert . strictEqual ( msg , "pan" , "Check for POST" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - data option - empty bodies for non-GET requests" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=echoData" ,
2015-08-16 03:45:28 +00:00
data : undefined ,
type : "post" ,
success : function ( result ) {
assert . strictEqual ( result , "" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2009-06-15 13:36:12 +00:00
2015-10-22 18:25:32 +00:00
ajaxTest ( "jQuery.ajax() - data - x-www-form-urlencoded (gh-2658)" , 1 , function ( assert ) {
return {
url : "bogus.html" ,
data : { devo : "A Beautiful World" } ,
type : "post" ,
beforeSend : function ( _ , s ) {
assert . strictEqual ( s . data , "devo=A+Beautiful+World" , "data is '+'-encoded" ) ;
return false ;
} ,
error : true
} ;
} ) ;
ajaxTest ( "jQuery.ajax() - data - text/plain (gh-2658)" , 1 , function ( assert ) {
return {
url : "bogus.html" ,
data : { devo : "A Beautiful World" } ,
type : "post" ,
contentType : "text/plain" ,
beforeSend : function ( _ , s ) {
assert . strictEqual ( s . data , "devo=A%20Beautiful%20World" , "data is %20-encoded" ) ;
2020-04-06 19:15:55 +00:00
return false ;
} ,
error : true
} ;
} ) ;
ajaxTest ( "jQuery.ajax() - don't escape %20 with contentType override (gh-4119)" , 1 , function ( assert ) {
return {
url : "bogus.html" ,
contentType : "application/x-www-form-urlencoded" ,
headers : { "content-type" : "application/json" } ,
method : "post" ,
dataType : "json" ,
data : "{\"val\":\"%20\"}" ,
beforeSend : function ( _ , s ) {
assert . strictEqual ( s . data , "{\"val\":\"%20\"}" , "data is not %20-encoded" ) ;
return false ;
} ,
error : true
} ;
} ) ;
ajaxTest ( "jQuery.ajax() - escape %20 with contentType override (gh-4119)" , 1 , function ( assert ) {
return {
url : "bogus.html" ,
contentType : "application/json" ,
headers : { "content-type" : "application/x-www-form-urlencoded" } ,
method : "post" ,
dataType : "json" ,
data : "{\"val\":\"%20\"}" ,
beforeSend : function ( _ , s ) {
assert . strictEqual ( s . data , "{\"val\":\"+\"}" , "data is %20-encoded" ) ;
return false ;
} ,
error : true
} ;
} ) ;
ajaxTest ( "jQuery.ajax() - override contentType with header (gh-4119)" , 1 , function ( assert ) {
return {
url : "bogus.html" ,
contentType : "application/json" ,
headers : { "content-type" : "application/x-www-form-urlencoded" } ,
beforeSend : function ( _ , s ) {
assert . strictEqual ( s . contentType , "application/x-www-form-urlencoded" ,
"contentType is overwritten" ) ;
2015-10-22 18:25:32 +00:00
return false ;
} ,
error : true
} ;
} ) ;
2017-09-12 15:24:45 +00:00
ajaxTest ( "jQuery.ajax() - data - no processing POST" , 1 , function ( assert ) {
2015-10-22 18:25:32 +00:00
return {
url : "bogus.html" ,
data : { devo : "A Beautiful World" } ,
type : "post" ,
contentType : "x-special-sauce" ,
processData : false ,
beforeSend : function ( _ , s ) {
assert . deepEqual ( s . data , { devo : "A Beautiful World" } , "data is not processed" ) ;
return false ;
} ,
error : true
2017-09-12 15:24:45 +00:00
} ;
} ) ;
ajaxTest ( "jQuery.ajax() - data - no processing GET" , 1 , function ( assert ) {
return {
url : "bogus.html" ,
data : { devo : "A Beautiful World" } ,
type : "get" ,
contentType : "x-something-else" ,
processData : false ,
beforeSend : function ( _ , s ) {
assert . deepEqual ( s . data , { devo : "A Beautiful World" } , "data is not processed" ) ;
return false ;
} ,
error : true
} ;
} ) ;
ajaxTest ( "jQuery.ajax() - data - process string with GET" , 2 , function ( assert ) {
return {
url : "bogus.html" ,
data : "a=1&b=2" ,
type : "get" ,
contentType : "x-something-else" ,
processData : false ,
beforeSend : function ( _ , s ) {
assert . equal ( s . url , "bogus.html?a=1&b=2" , "added data to url" ) ;
assert . equal ( s . data , undefined , "removed data from settings" ) ;
return false ;
} ,
error : true
2015-10-22 18:25:32 +00:00
} ;
} ) ;
2012-10-15 19:53:39 +00:00
var ifModifiedNow = new Date ( ) ;
2009-06-15 13:36:12 +00:00
2012-12-05 13:54:14 +00:00
jQuery . each (
/* jQuery.each arguments start */
{
" (cache)" : true ,
" (no cache)" : false
} ,
function ( label , cache ) {
2013-01-08 10:06:20 +00:00
jQuery . each (
{
2021-12-01 11:46:33 +00:00
"If-Modified-Since" : {
url : "mock.php?action=ims" ,
qunitMethod : "test"
} ,
"Etag" : {
url : "mock.php?action=etag" ,
// Support: TestSwarm
// TestSwarm is now proxied via Cloudflare which cuts out
// headers relevant for ETag tests, failing them. We're still
// running those tests in Karma on Chrome & Firefox (including
// Firefox ESR).
qunitMethod : QUnit . isSwarm ? "skip" : "test"
}
2013-01-08 10:06:20 +00:00
} ,
2021-12-01 11:46:33 +00:00
function ( type , data ) {
var url = baseURL + data . url + "&ts=" + ifModifiedNow ++ ;
QUnit [ data . qunitMethod ] ( "jQuery.ajax() - " + type +
" support" + label , function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 4 ) ;
var done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2012-12-05 13:54:14 +00:00
url : url ,
ifModified : true ,
cache : cache ,
2013-01-08 10:06:20 +00:00
success : function ( _ , status ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( status , "success" , "Initial status is 'success'" ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2013-01-08 10:06:20 +00:00
url : url ,
ifModified : true ,
cache : cache ,
success : function ( data , status , jqXHR ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( status , "notmodified" , "Following status is 'notmodified'" ) ;
assert . strictEqual ( jqXHR . status , 304 , "XHR status is 304" ) ;
assert . equal ( data , null , "no response body is given" ) ;
2013-01-08 10:06:20 +00:00
} ,
complete : function ( ) {
2019-02-18 18:02:38 +00:00
done ( ) ;
2013-01-08 10:06:20 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2013-01-08 10:06:20 +00:00
}
) ;
2012-12-05 13:54:14 +00:00
}
/* jQuery.each arguments end */
) ;
2010-12-09 18:34:28 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - failing cross-domain (non-existing)" , 1 , function ( assert ) {
return {
2015-08-16 06:59:58 +00:00
2015-08-16 03:45:28 +00:00
// see RFC 2606
2019-10-21 17:03:48 +00:00
url : "https://example.invalid" ,
2015-08-16 03:45:28 +00:00
error : function ( xhr , _ , e ) {
assert . ok ( true , "file not found: " + xhr . status + " => " + e ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-11-17 06:59:24 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - failing cross-domain" , 1 , function ( assert ) {
return {
2019-10-21 17:03:48 +00:00
url : "https://" + externalHost ,
2015-08-16 03:45:28 +00:00
error : function ( xhr , _ , e ) {
assert . ok ( true , "access denied: " + xhr . status + " => " + e ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2010-04-29 03:45:34 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - atom+xml" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=atom" ) ,
2015-08-16 03:45:28 +00:00
success : function ( ) {
assert . ok ( true , "success" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.ajax() - statusText" , function ( assert ) {
assert . expect ( 3 ) ;
var done = assert . async ( ) ;
2017-08-01 16:52:45 +00:00
jQuery . ajax ( url ( "mock.php?action=status&code=200&text=Hello" ) ) . done ( function ( _ , statusText , jqXHR ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( statusText , "success" , "callback status text ok for success" ) ;
2021-12-01 11:46:17 +00:00
assert . ok ( [ "Hello" , "OK" , "success" ] . indexOf ( jqXHR . statusText ) > - 1 ,
"jqXHR status text ok for success (" + jqXHR . statusText + ")" ) ;
2017-08-01 16:52:45 +00:00
jQuery . ajax ( url ( "mock.php?action=status&code=404&text=World" ) ) . fail ( function ( jqXHR , statusText ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( statusText , "error" , "callback status text ok for error" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
} ) ;
2012-12-05 13:54:14 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.ajax() - statusCode" , function ( assert ) {
assert . expect ( 20 ) ;
var done = assert . async ( ) ,
count = 12 ;
2012-12-05 13:54:14 +00:00
function countComplete ( ) {
2015-08-16 06:59:58 +00:00
if ( ! -- count ) {
2019-02-18 18:02:38 +00:00
done ( ) ;
2011-01-13 16:01:25 +00:00
}
2012-10-15 19:53:39 +00:00
}
2011-01-13 16:01:25 +00:00
2012-12-05 13:54:14 +00:00
function createStatusCodes ( name , isSuccess ) {
name = "Test " + name + " " + ( isSuccess ? "success" : "error" ) ;
2012-10-15 19:53:39 +00:00
return {
200 : function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( isSuccess , name ) ;
2012-10-15 19:53:39 +00:00
} ,
404 : function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( ! isSuccess , name ) ;
2012-10-15 19:53:39 +00:00
}
} ;
}
2012-12-05 13:54:14 +00:00
jQuery . each (
/* jQuery.each arguments start */
{
2017-08-01 16:52:45 +00:00
"name.html" : true ,
"404.txt" : false
2012-12-05 13:54:14 +00:00
} ,
function ( uri , isSuccess ) {
2015-08-16 06:59:58 +00:00
jQuery . ajax ( url ( uri ) , {
2012-12-05 13:54:14 +00:00
statusCode : createStatusCodes ( "in options" , isSuccess ) ,
complete : countComplete
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 06:59:58 +00:00
jQuery . ajax ( url ( uri ) , {
2012-12-05 13:54:14 +00:00
complete : countComplete
2015-08-16 06:59:58 +00:00
} ) . statusCode ( createStatusCodes ( "immediately with method" , isSuccess ) ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 06:59:58 +00:00
jQuery . ajax ( url ( uri ) , {
2012-12-05 13:54:14 +00:00
complete : function ( jqXHR ) {
2015-08-16 06:59:58 +00:00
jqXHR . statusCode ( createStatusCodes ( "on complete" , isSuccess ) ) ;
2012-12-05 13:54:14 +00:00
countComplete ( ) ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 06:59:58 +00:00
jQuery . ajax ( url ( uri ) , {
2012-12-05 13:54:14 +00:00
complete : function ( jqXHR ) {
2015-08-16 06:59:58 +00:00
setTimeout ( function ( ) {
jqXHR . statusCode ( createStatusCodes ( "very late binding" , isSuccess ) ) ;
2012-12-05 13:54:14 +00:00
countComplete ( ) ;
} , 100 ) ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 06:59:58 +00:00
jQuery . ajax ( url ( uri ) , {
2012-12-05 13:54:14 +00:00
statusCode : createStatusCodes ( "all (options)" , isSuccess ) ,
complete : function ( jqXHR ) {
2015-08-16 06:59:58 +00:00
jqXHR . statusCode ( createStatusCodes ( "all (on complete)" , isSuccess ) ) ;
setTimeout ( function ( ) {
jqXHR . statusCode ( createStatusCodes ( "all (very late binding)" , isSuccess ) ) ;
2012-12-05 13:54:14 +00:00
countComplete ( ) ;
} , 100 ) ;
}
2015-08-16 06:59:58 +00:00
} ) . statusCode ( createStatusCodes ( "all (immediately with method)" , isSuccess ) ) ;
2012-12-05 13:54:14 +00:00
var testString = "" ;
2015-08-16 06:59:58 +00:00
jQuery . ajax ( url ( uri ) , {
2012-12-05 13:54:14 +00:00
success : function ( a , b , jqXHR ) {
2015-08-16 03:45:28 +00:00
assert . ok ( isSuccess , "success" ) ;
2012-12-05 13:54:14 +00:00
var statusCode = { } ;
statusCode [ jqXHR . status ] = function ( ) {
testString += "B" ;
} ;
jqXHR . statusCode ( statusCode ) ;
testString += "A" ;
} ,
error : function ( jqXHR ) {
2015-08-16 03:45:28 +00:00
assert . ok ( ! isSuccess , "error" ) ;
2012-12-05 13:54:14 +00:00
var statusCode = { } ;
statusCode [ jqXHR . status ] = function ( ) {
testString += "B" ;
} ;
jqXHR . statusCode ( statusCode ) ;
testString += "A" ;
} ,
complete : function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual (
2012-12-05 13:54:14 +00:00
testString ,
"AB" ,
"Test statusCode callbacks are ordered like " + ( isSuccess ? "success" : "error" ) + " callbacks"
) ;
countComplete ( ) ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
}
/* jQuery.each arguments end*/
) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - transitive conversions" , 8 , function ( assert ) {
return [
{
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=json" ) ,
2015-08-16 03:45:28 +00:00
converters : {
"json myJson" : function ( data ) {
assert . ok ( true , "converter called" ) ;
return data ;
}
} ,
dataType : "myJson" ,
success : function ( ) {
assert . ok ( true , "Transitive conversion worked" ) ;
assert . strictEqual ( this . dataTypes [ 0 ] , "text" , "response was retrieved as text" ) ;
assert . strictEqual ( this . dataTypes [ 1 ] , "myjson" , "request expected myjson dataType" ) ;
2012-12-05 13:54:14 +00:00
}
} ,
2015-08-16 03:45:28 +00:00
{
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=json" ) ,
2015-08-16 03:45:28 +00:00
converters : {
"json myJson" : function ( data ) {
assert . ok ( true , "converter called (*)" ) ;
return data ;
}
} ,
contents : false , /* headers are wrong so we ignore them */
dataType : "* myJson" ,
success : function ( ) {
assert . ok ( true , "Transitive conversion worked (*)" ) ;
assert . strictEqual ( this . dataTypes [ 0 ] , "text" , "response was retrieved as text (*)" ) ;
assert . strictEqual ( this . dataTypes [ 1 ] , "myjson" , "request expected myjson dataType (*)" ) ;
2012-12-04 06:39:27 +00:00
}
2012-12-05 13:54:14 +00:00
}
2015-08-16 03:45:28 +00:00
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - overrideMimeType" , 2 , function ( assert ) {
return [
{
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=json" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( xhr ) {
xhr . overrideMimeType ( "application/json" ) ;
} ,
success : function ( json ) {
assert . ok ( json . data , "Mimetype overridden using beforeSend" ) ;
}
2012-11-26 02:31:19 +00:00
} ,
2015-08-16 03:45:28 +00:00
{
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=json" ) ,
2015-08-16 03:45:28 +00:00
mimeType : "application/json" ,
success : function ( json ) {
assert . ok ( json . data , "Mimetype overridden using mimeType option" ) ;
}
2012-11-26 02:31:19 +00:00
}
2015-08-16 03:45:28 +00:00
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajax() - empty json gets to error callback instead of success callback." , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=echoData" ) ,
2015-08-16 03:45:28 +00:00
error : function ( _ , _ _ , error ) {
assert . equal ( typeof error === "object" , true , "Didn't get back error object for empty json response" ) ;
} ,
dataType : "json"
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-2688 - jQuery.ajax() - beforeSend, cancel request" , 2 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
create : function ( ) {
2015-08-16 06:59:58 +00:00
return jQuery . ajax ( {
2017-08-01 16:52:45 +00:00
url : url ( "name.html" ) ,
2015-08-16 03:45:28 +00:00
beforeSend : function ( ) {
assert . ok ( true , "beforeSend got called, canceling" ) ;
return false ;
} ,
success : function ( ) {
assert . ok ( false , "request didn't get canceled" ) ;
} ,
complete : function ( ) {
assert . ok ( false , "request didn't get canceled" ) ;
} ,
error : function ( ) {
assert . ok ( false , "request didn't get canceled" ) ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
} ,
fail : function ( _ , reason ) {
assert . strictEqual ( reason , "canceled" , "canceled request must fail with 'canceled' status text" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-2806 - jQuery.ajax() - data option - evaluate function values" , 1 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=echoQuery" ,
2015-08-16 03:45:28 +00:00
data : {
key : function ( ) {
return "value" ;
}
} ,
success : function ( result ) {
2017-08-01 16:52:45 +00:00
assert . strictEqual ( result , "action=echoQuery&key=value" ) ;
2012-11-26 02:31:19 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2022-01-04 15:27:18 +00:00
QUnit . test ( "trac-7531 - jQuery.ajax() - Location object as url" , function ( assert ) {
2015-08-16 03:45:28 +00:00
assert . expect ( 1 ) ;
2015-07-29 15:10:04 +00:00
2013-04-09 15:45:09 +00:00
var xhr ,
success = false ;
2012-11-26 02:31:19 +00:00
try {
2015-08-16 06:59:58 +00:00
xhr = jQuery . ajax ( {
2012-11-26 02:31:19 +00:00
url : window . location
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
success = true ;
xhr . abort ( ) ;
2015-08-16 06:59:58 +00:00
} catch ( e ) {
2012-11-26 02:31:19 +00:00
}
2015-08-16 03:45:28 +00:00
assert . ok ( success , "document.location did not generate exception" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
jQuery . each ( [ " - Same Domain" , " - Cross Domain" ] , function ( crossDomain , label ) {
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-7578 - jQuery.ajax() - JSONP - default for cache option" + label , 1 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
beforeSend : function ( ) {
assert . strictEqual ( this . cache , false , "cache must be false on JSON request" ) ;
return false ;
} ,
error : true
} ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-11-26 02:31:19 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-8107 - jQuery.ajax() - multiple method signatures introduced in 1.5" , 4 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return [
{
create : function ( ) {
return jQuery . ajax ( ) ;
} ,
done : function ( ) {
assert . ok ( true , "With no arguments" ) ;
}
2012-11-26 02:31:19 +00:00
} ,
2015-08-16 03:45:28 +00:00
{
create : function ( ) {
2017-08-01 16:52:45 +00:00
return jQuery . ajax ( baseURL + "name.html" ) ;
2015-08-16 03:45:28 +00:00
} ,
done : function ( ) {
assert . ok ( true , "With only string URL argument" ) ;
}
2012-11-26 02:31:19 +00:00
} ,
2015-08-16 03:45:28 +00:00
{
create : function ( ) {
2017-08-01 16:52:45 +00:00
return jQuery . ajax ( baseURL + "name.html" , { } ) ;
2015-08-16 03:45:28 +00:00
} ,
done : function ( ) {
assert . ok ( true , "With string URL param and map" ) ;
}
2012-11-26 02:31:19 +00:00
} ,
2015-08-16 03:45:28 +00:00
{
create : function ( options ) {
return jQuery . ajax ( options ) ;
} ,
2017-08-01 16:52:45 +00:00
url : baseURL + "name.html" ,
2015-08-16 03:45:28 +00:00
success : function ( ) {
assert . ok ( true , "With only map" ) ;
}
2012-11-26 02:31:19 +00:00
}
2015-08-16 03:45:28 +00:00
] ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-31 18:31:03 +00:00
2012-11-26 02:31:19 +00:00
jQuery . each ( [ " - Same Domain" , " - Cross Domain" ] , function ( crossDomain , label ) {
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label , 4 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
beforeSend : function ( jqXHR , s ) {
s . callback = s . jsonpCallback ;
2015-07-10 17:58:43 +00:00
2015-08-16 03:45:28 +00:00
assert . ok ( this . callback in window , "JSONP callback name is in the window" ) ;
} ,
success : function ( ) {
var previous = this ;
2015-07-10 17:58:43 +00:00
2015-08-16 03:45:28 +00:00
assert . strictEqual (
previous . jsonpCallback ,
undefined ,
"jsonpCallback option is set back to default in callbacks"
) ;
2015-07-10 17:58:43 +00:00
2015-08-16 03:45:28 +00:00
assert . ok (
! ( this . callback in window ) ,
"JSONP callback name was removed from the window"
) ;
2015-07-10 17:58:43 +00:00
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=jsonp" ,
2015-08-16 03:45:28 +00:00
dataType : "jsonp" ,
crossDomain : crossDomain ,
beforeSend : function ( ) {
assert . strictEqual ( this . jsonpCallback , previous . callback , "JSONP callback name is re-used" ) ;
return false ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-11-26 02:31:19 +00:00
2022-01-04 15:27:18 +00:00
QUnit . test ( "trac-9887 - jQuery.ajax() - Context with circular references (trac-9887)" , function ( assert ) {
2015-08-16 03:45:28 +00:00
assert . expect ( 2 ) ;
2015-07-29 15:10:04 +00:00
2012-11-26 02:31:19 +00:00
var success = false ,
context = { } ;
context . field = context ;
try {
2012-12-05 13:54:14 +00:00
jQuery . ajax ( "non-existing" , {
2012-11-26 02:31:19 +00:00
context : context ,
beforeSend : function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( this === context , "context was not deep extended" ) ;
2012-11-26 02:31:19 +00:00
return false ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
success = true ;
} catch ( e ) {
console . log ( e ) ;
}
2015-08-16 03:45:28 +00:00
assert . ok ( success , "context with circular reference did not generate an exception" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2012-12-05 13:54:14 +00:00
jQuery . each ( [ "as argument" , "in settings object" ] , function ( inSetting , title ) {
2012-12-31 18:31:03 +00:00
2015-08-16 03:45:28 +00:00
function request ( assert , url , test ) {
2012-11-26 02:31:19 +00:00
return {
create : function ( ) {
return jQuery . ajax ( inSetting ? { url : url } : url ) ;
} ,
done : function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , ( test || url ) + " " + title ) ;
2012-11-26 02:31:19 +00:00
}
} ;
}
2012-12-31 18:31:03 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-10093 - jQuery.ajax() - falsy url " + title , 4 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return [
request ( assert , "" , "empty string" ) ,
request ( assert , false ) ,
request ( assert , null ) ,
request ( assert , undefined )
] ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-11-26 02:31:19 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-11151 - jQuery.ajax() - parse error body" , 2 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=error&json=1" ) ,
2015-08-16 03:45:28 +00:00
dataFilter : function ( string ) {
assert . ok ( false , "dataFilter called" ) ;
return string ;
} ,
error : function ( jqXHR ) {
assert . strictEqual ( jqXHR . responseText , "{ \"code\": 40, \"message\": \"Bad Request\" }" , "Error body properly set" ) ;
assert . deepEqual ( jqXHR . responseJSON , { code : 40 , message : "Bad Request" } , "Error body properly parsed" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2013-02-08 15:26:36 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-11426 - jQuery.ajax() - loading binary data shouldn't throw an exception in IE" , 1 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : url ( "1x1.jpg" ) ,
2015-08-16 03:45:28 +00:00
success : function ( data ) {
assert . ok ( data === undefined || /JFIF/ . test ( data ) , "success callback reached" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2015-10-31 15:24:04 +00:00
if ( typeof window . ArrayBuffer === "undefined" || typeof new XMLHttpRequest ( ) . responseType !== "string" ) {
QUnit . skip ( "No ArrayBuffer support in XHR" , jQuery . noop ) ;
} else {
// No built-in support for binary data, but it's easy to add via a prefilter
2016-05-10 09:12:28 +00:00
jQuery . ajaxPrefilter ( "arraybuffer" , function ( s ) {
2015-10-31 15:24:04 +00:00
s . xhrFields = { responseType : "arraybuffer" } ;
s . responseFields . arraybuffer = "response" ;
s . converters [ "binary arraybuffer" ] = true ;
2016-05-10 09:12:28 +00:00
} ) ;
2015-10-31 15:24:04 +00:00
ajaxTest ( "gh-2498 - jQuery.ajax() - binary data shouldn't throw an exception" , 2 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "1x1.jpg" ) ,
2015-10-31 15:24:04 +00:00
dataType : "arraybuffer" ,
success : function ( data , s , jqxhr ) {
assert . ok ( data instanceof window . ArrayBuffer , "correct data type" ) ;
assert . ok ( jqxhr . response instanceof window . ArrayBuffer , "data in jQXHR" ) ;
}
} ;
} ) ;
}
2022-01-04 15:27:18 +00:00
QUnit . test ( "trac-11743 - jQuery.ajax() - script, throws exception" , function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 1 ) ;
var done = assert . async ( ) ;
2013-01-02 21:55:41 +00:00
var onerror = window . onerror ;
window . onerror = function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "Exception thrown" ) ;
2013-01-02 21:55:41 +00:00
window . onerror = onerror ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2013-01-02 21:55:41 +00:00
} ;
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2017-08-01 16:52:45 +00:00
url : baseURL + "badjson.js" ,
2013-01-02 21:55:41 +00:00
dataType : "script" ,
2013-11-29 08:15:05 +00:00
throws : true
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-11-26 02:31:19 +00:00
2012-11-28 23:15:17 +00:00
jQuery . each ( [ "method" , "type" ] , function ( _ , globalOption ) {
2015-08-16 03:45:28 +00:00
function request ( assert , option ) {
2012-11-28 23:15:17 +00:00
var options = {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=echoData" ) ,
2012-12-05 13:54:14 +00:00
data : "hello" ,
2012-11-28 23:15:17 +00:00
success : function ( msg ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( msg , "hello" , "Check for POST (no override)" ) ;
2012-11-28 23:15:17 +00:00
}
} ;
if ( option ) {
options [ option ] = "GET" ;
options . success = function ( msg ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( msg , "" , "Check for no POST (overriding with " + option + ")" ) ;
2012-11-28 23:15:17 +00:00
} ;
}
return options ;
}
2015-08-16 03:45:28 +00:00
ajaxTest (
2022-01-04 15:27:18 +00:00
"trac-12004 - jQuery.ajax() - method is an alias of type - " +
2015-08-16 03:45:28 +00:00
globalOption + " set globally" , 3 ,
function ( assert ) {
return {
setup : function ( ) {
var options = { } ;
options [ globalOption ] = "POST" ;
jQuery . ajaxSetup ( options ) ;
} ,
requests : [
2015-08-16 06:59:58 +00:00
request ( assert , "type" ) ,
request ( assert , "method" ) ,
request ( assert )
2015-08-16 03:45:28 +00:00
]
} ;
}
) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-28 23:15:17 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-13276 - jQuery.ajax() - compatibility between XML documents from ajax requests and parsed string" , 1 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "dashboard.xml" ,
2015-08-16 03:45:28 +00:00
dataType : "xml" ,
success : function ( ajaxXML ) {
2015-08-16 06:59:58 +00:00
var parsedXML = jQuery ( jQuery . parseXML ( "<tab title=\"Added\">blibli</tab>" ) ) . find ( "tab" ) ;
2015-08-16 03:45:28 +00:00
ajaxXML = jQuery ( ajaxXML ) ;
try {
2015-08-16 06:59:58 +00:00
ajaxXML . find ( "infowindowtab" ) . append ( parsedXML ) ;
} catch ( e ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( e , undefined , "error" ) ;
return ;
2014-05-05 13:45:57 +00:00
}
2015-08-16 06:59:58 +00:00
assert . strictEqual ( ajaxXML . find ( "tab" ) . length , 3 , "Parsed node was added properly" ) ;
2013-01-21 01:53:50 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2013-03-13 01:06:25 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-13292 - jQuery.ajax() - converter is bypassed for 204 requests" , 3 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=status&code=204&text=No+Content" ,
2015-08-16 03:45:28 +00:00
dataType : "testing" ,
converters : {
"* testing" : function ( ) {
throw "converter was called" ;
}
} ,
success : function ( data , status , jqXHR ) {
assert . strictEqual ( jqXHR . status , 204 , "status code is 204" ) ;
assert . strictEqual ( status , "nocontent" , "status text is 'nocontent'" ) ;
assert . strictEqual ( data , undefined , "data is undefined" ) ;
} ,
error : function ( _ , status , error ) {
assert . ok ( false , "error" ) ;
assert . strictEqual ( status , "parsererror" , "Parser Error" ) ;
assert . strictEqual ( error , "converter was called" , "Converter was called" ) ;
2013-01-24 01:33:17 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2013-01-21 01:53:50 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-13388 - jQuery.ajax() - responseXML" , 3 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : url ( "with_fries.xml" ) ,
2015-08-16 03:45:28 +00:00
dataType : "xml" ,
success : function ( resp , _ , jqXHR ) {
assert . notStrictEqual ( resp , undefined , "XML document exists" ) ;
assert . ok ( "responseXML" in jqXHR , "jqXHR.responseXML exists" ) ;
assert . strictEqual ( resp , jqXHR . responseXML , "jqXHR.responseXML is set correctly" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2013-02-08 15:26:36 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-13922 - jQuery.ajax() - converter is bypassed for HEAD requests" , 3 , function ( assert ) {
2015-08-16 03:45:28 +00:00
return {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=json" ,
2015-08-16 03:45:28 +00:00
method : "HEAD" ,
data : {
header : "yes"
} ,
converters : {
"text json" : function ( ) {
throw "converter was called" ;
}
} ,
success : function ( data , status ) {
assert . ok ( true , "success" ) ;
assert . strictEqual ( status , "nocontent" , "data is undefined" ) ;
assert . strictEqual ( data , undefined , "data is undefined" ) ;
} ,
error : function ( _ , status , error ) {
assert . ok ( false , "error" ) ;
assert . strictEqual ( status , "parsererror" , "Parser Error" ) ;
assert . strictEqual ( error , "converter was called" , "Converter was called" ) ;
2013-05-23 00:11:37 +00:00
}
2015-08-16 03:45:28 +00:00
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2013-10-28 21:40:13 +00:00
2019-10-28 19:27:49 +00:00
// Chrome 78 dropped support for synchronous XHR requests inside of
// beforeunload, unload, pagehide, and visibilitychange event handlers.
// See https://bugs.chromium.org/p/chromium/issues/detail?id=952452
2020-09-02 16:04:44 +00:00
// Safari 13 did similar changes. The below check will catch them both.
2023-01-23 22:49:44 +00:00
if ( ! /webkit/i . test ( navigator . userAgent ) ) {
2019-10-28 19:27:49 +00:00
testIframe (
2022-01-04 15:27:18 +00:00
"trac-14379 - jQuery.ajax() on unload" ,
2019-10-28 19:27:49 +00:00
"ajax/onunload.html" ,
function ( assert , jQuery , window , document , status ) {
assert . expect ( 1 ) ;
assert . strictEqual ( status , "success" , "Request completed" ) ;
}
) ;
}
2015-08-16 03:45:28 +00:00
2022-01-04 15:27:18 +00:00
ajaxTest ( "trac-14683 - jQuery.ajax() - Exceptions thrown synchronously by xhr.send should be caught" , 4 , function ( assert ) {
2015-08-16 06:59:58 +00:00
return [ {
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=echoData" ,
2014-01-25 07:20:18 +00:00
method : "POST" ,
data : {
toString : function ( ) {
throw "Can't parse" ;
}
} ,
processData : false ,
done : function ( data ) {
2015-08-16 03:45:28 +00:00
assert . ok ( false , "done: " + data ) ;
2014-01-25 07:20:18 +00:00
} ,
2015-10-12 21:00:38 +00:00
fail : function ( jqXHR , status , error ) {
assert . ok ( true , "exception caught: " + error ) ;
assert . strictEqual ( jqXHR . status , 0 , "proper status code" ) ;
assert . strictEqual ( status , "error" , "proper status" ) ;
}
} , {
2019-10-21 17:03:48 +00:00
url : "https://" + externalHost + ":80q" ,
2015-10-12 21:00:38 +00:00
done : function ( data ) {
assert . ok ( false , "done: " + data ) ;
} ,
fail : function ( _ , status , error ) {
assert . ok ( true , "fail: " + status + " - " + error ) ;
}
} ] ;
} ) ;
2014-01-25 07:20:18 +00:00
2015-10-12 16:56:46 +00:00
ajaxTest ( "gh-2587 - when content-type not xml, but looks like one" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=contentType" ) ,
2015-10-12 16:56:46 +00:00
data : {
2017-08-01 16:52:45 +00:00
contentType : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
2015-10-12 16:56:46 +00:00
"response" : "<test/>"
} ,
success : function ( result ) {
assert . strictEqual (
typeof result ,
"string" ,
"Should handle it as a string, not xml"
) ;
}
} ;
} ) ;
ajaxTest ( "gh-2587 - when content-type not xml, but looks like one" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=contentType" ) ,
2015-10-12 16:56:46 +00:00
data : {
2017-08-01 16:52:45 +00:00
contentType : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
2015-10-12 16:56:46 +00:00
"response" : "<test/>"
} ,
success : function ( result ) {
assert . strictEqual (
typeof result ,
"string" ,
"Should handle it as a string, not xml"
) ;
}
} ;
} ) ;
ajaxTest ( "gh-2587 - when content-type not json, but looks like one" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=contentType" ) ,
2015-10-12 16:56:46 +00:00
data : {
2017-08-01 16:52:45 +00:00
contentType : "test/jsontest" ,
2016-05-10 09:12:28 +00:00
"response" : JSON . stringify ( { test : "test" } )
2015-10-12 16:56:46 +00:00
} ,
success : function ( result ) {
assert . strictEqual (
typeof result ,
"string" ,
"Should handle it as a string, not json"
) ;
}
} ;
} ) ;
ajaxTest ( "gh-2587 - when content-type not html, but looks like one" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=contentType" ) ,
2015-10-12 16:56:46 +00:00
data : {
2017-08-01 16:52:45 +00:00
contentType : "test/htmltest" ,
2015-10-12 16:56:46 +00:00
"response" : "<p>test</p>"
} ,
success : function ( result ) {
assert . strictEqual (
typeof result ,
"string" ,
"Should handle it as a string, not html"
) ;
}
} ;
} ) ;
ajaxTest ( "gh-2587 - when content-type not javascript, but looks like one" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=contentType" ) ,
2015-10-12 16:56:46 +00:00
data : {
2017-08-01 16:52:45 +00:00
contentType : "test/testjavascript" ,
2015-10-12 16:56:46 +00:00
"response" : "alert(1)"
} ,
success : function ( result ) {
assert . strictEqual (
typeof result ,
"string" ,
"Should handle it as a string, not javascript"
) ;
}
} ;
} ) ;
ajaxTest ( "gh-2587 - when content-type not ecmascript, but looks like one" , 1 , function ( assert ) {
return {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=contentType" ) ,
2015-10-12 16:56:46 +00:00
data : {
2017-08-01 16:52:45 +00:00
contentType : "test/testjavascript" ,
2015-10-12 16:56:46 +00:00
"response" : "alert(1)"
} ,
success : function ( result ) {
assert . strictEqual (
typeof result ,
"string" ,
"Should handle it as a string, not ecmascript"
) ;
}
} ;
} ) ;
//----------- jQuery.ajaxPrefilter()
2012-11-26 02:31:19 +00:00
2015-08-16 03:45:28 +00:00
ajaxTest ( "jQuery.ajaxPrefilter() - abort" , 1 , function ( assert ) {
return {
dataType : "prefix" ,
setup : function ( ) {
2015-08-16 06:59:58 +00:00
2015-08-16 03:45:28 +00:00
// Ensure prefix does not throw an error
2015-08-16 06:59:58 +00:00
jQuery . ajaxPrefilter ( "+prefix" , function ( options , _ , jqXHR ) {
2015-08-16 03:45:28 +00:00
if ( options . abortInPrefilter ) {
jqXHR . abort ( ) ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
} ,
abortInPrefilter : true ,
error : function ( ) {
assert . ok ( false , "error callback called" ) ;
} ,
fail : function ( _ , reason ) {
assert . strictEqual ( reason , "canceled" , "Request aborted by the prefilter must fail with 'canceled' status text" ) ;
}
} ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
//----------- jQuery.ajaxSetup()
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.ajaxSetup()" , function ( assert ) {
assert . expect ( 1 ) ;
var done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=name&name=foo" ) ,
2012-11-26 02:31:19 +00:00
success : function ( msg ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( msg , "bar" , "Check for GET" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-11-26 02:31:19 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
jQuery . ajax ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.ajaxSetup({ timeout: Number }) - with global timeout" , function ( assert ) {
assert . expect ( 2 ) ;
var done = assert . async ( ) ;
2012-12-05 13:54:14 +00:00
var passed = 0 ,
pass = function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( passed ++ < 2 , "Error callback executed" ) ;
2013-04-09 15:45:09 +00:00
if ( passed === 2 ) {
2015-08-16 06:59:58 +00:00
jQuery ( document ) . off ( "ajaxError.setupTest" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-12-05 13:54:14 +00:00
}
} ,
2013-04-09 15:45:09 +00:00
fail = function ( a , b ) {
2015-08-16 03:45:28 +00:00
assert . ok ( false , "Check for timeout failed " + a + " " + b ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-12-05 13:54:14 +00:00
} ;
jQuery ( document ) . on ( "ajaxError.setupTest" , pass ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-12-05 13:54:14 +00:00
timeout : 1000
2015-08-16 06:59:58 +00:00
} ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2012-12-05 13:54:14 +00:00
type : "GET" ,
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=wait&wait=5" ) ,
2012-12-05 13:54:14 +00:00
error : pass ,
success : fail
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-11-26 02:31:19 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.ajaxSetup({ timeout: Number }) with localtimeout" , function ( assert ) {
assert . expect ( 1 ) ;
var done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-12-05 13:54:14 +00:00
timeout : 50
2015-08-16 06:59:58 +00:00
} ) ;
jQuery . ajax ( {
2012-12-05 13:54:14 +00:00
type : "GET" ,
timeout : 15000 ,
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=wait&wait=1" ) ,
2012-12-05 13:54:14 +00:00
error : function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( false , "Check for local timeout failed" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-12-05 13:54:14 +00:00
} ,
success : function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "Check for local timeout" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-12-05 13:54:14 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-11-26 02:31:19 +00:00
2022-10-10 16:15:34 +00:00
//----------- domManip()
2012-11-26 02:31:19 +00:00
2022-10-10 16:15:34 +00:00
QUnit . test ( "trac-11264 - domManip() - no side effect because of ajaxSetup or global events" , function ( assert ) {
2015-08-16 03:45:28 +00:00
assert . expect ( 1 ) ;
2015-07-29 15:10:04 +00:00
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
type : "POST"
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2013-03-04 02:22:11 +00:00
jQuery ( document ) . on ( "ajaxStart ajaxStop" , function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( false , "Global event triggered" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2017-08-01 16:52:45 +00:00
jQuery ( "#qunit-fixture" ) . append ( "<script src='" + baseURL + "mock.php?action=script'></script>" ) ;
2012-12-05 13:54:14 +00:00
2015-08-16 06:59:58 +00:00
jQuery ( document ) . off ( "ajaxStart ajaxStop" ) ;
} ) ;
2011-01-21 02:58:28 +00:00
2015-09-08 00:26:29 +00:00
QUnit . test (
2022-01-04 15:27:18 +00:00
"jQuery#load() - always use GET method even if it overrided through ajaxSetup (trac-11264)" ,
2015-09-08 00:26:29 +00:00
function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 1 ) ;
2015-09-08 00:26:29 +00:00
var done = assert . async ( ) ;
2015-02-10 19:42:21 +00:00
2015-09-08 00:26:29 +00:00
jQuery . ajaxSetup ( {
type : "POST"
} ) ;
2015-02-10 19:42:21 +00:00
2017-08-01 16:52:45 +00:00
jQuery ( "#qunit-fixture" ) . load ( baseURL + "mock.php?action=echoMethod" , function ( method ) {
2015-09-08 00:26:29 +00:00
assert . equal ( method , "GET" ) ;
done ( ) ;
} ) ;
}
) ;
2016-04-04 19:42:01 +00:00
QUnit . test (
2019-02-18 18:02:38 +00:00
"jQuery#load() - should resolve with correct context" ,
2016-04-04 19:42:01 +00:00
function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 2 ) ;
2016-04-04 19:42:01 +00:00
var done = assert . async ( ) ;
var ps = jQuery ( "<p></p><p></p>" ) ;
var i = 0 ;
ps . appendTo ( "#qunit-fixture" ) ;
2017-08-01 16:52:45 +00:00
ps . load ( baseURL + "mock.php?action=echoMethod" , function ( ) {
2016-04-04 19:42:01 +00:00
assert . strictEqual ( this , ps [ i ++ ] ) ;
if ( i === 2 ) {
done ( ) ;
}
} ) ;
}
) ;
2015-09-08 00:26:29 +00:00
QUnit . test (
2022-10-10 16:15:34 +00:00
"trac-11402 - domManip() - script in comments are properly evaluated" ,
2015-09-08 00:26:29 +00:00
function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 2 ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#qunit-fixture" ) . load ( baseURL + "cleanScript.html" , assert . async ( ) ) ;
2015-09-08 00:26:29 +00:00
}
) ;
2011-01-21 02:58:28 +00:00
2012-11-26 02:31:19 +00:00
//----------- jQuery.get()
2011-01-21 02:58:28 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.get( String, Hash, Function ) - parse xml and use text() on nodes" , function ( assert ) {
assert . expect ( 2 ) ;
var done = assert . async ( ) ;
2017-08-01 16:52:45 +00:00
jQuery . get ( url ( "dashboard.xml" ) , function ( xml ) {
2012-12-05 13:54:14 +00:00
var content = [ ] ;
2015-08-16 06:59:58 +00:00
jQuery ( "tab" , xml ) . each ( function ( ) {
2012-12-05 13:54:14 +00:00
content . push ( jQuery ( this ) . text ( ) ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-08-16 03:45:28 +00:00
assert . strictEqual ( content [ 0 ] , "blabla" , "Check first tab" ) ;
assert . strictEqual ( content [ 1 ] , "blublu" , "Check second tab" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2011-01-21 02:58:28 +00:00
2022-01-04 15:27:18 +00:00
QUnit . test ( "trac-8277 - jQuery.get( String, Function ) - data in ajaxSettings" , function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 1 ) ;
var done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-12-05 13:54:14 +00:00
data : "helloworld"
2015-08-16 06:59:58 +00:00
} ) ;
2017-08-01 16:52:45 +00:00
jQuery . get ( url ( "mock.php?action=echoQuery" ) , function ( data ) {
2015-08-16 03:45:28 +00:00
assert . ok ( /helloworld$/ . test ( data ) , "Data from ajaxSettings was used" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2011-01-21 02:58:28 +00:00
2022-10-17 16:54:28 +00:00
QUnit . test ( "jQuery.get( String, null-ish, String ) - dataType with null callback (gh-4989)" ,
function ( assert ) {
assert . expect ( 2 ) ;
var done = assert . async ( 2 ) ;
jQuery . get ( url ( "mock.php?action=json&header" ) , null , "json" )
. then ( function ( json ) {
assert . deepEqual ( json , { data : { lang : "en" , length : 25 } } ,
"`dataType: \"json\"` applied with a `null` callback" ) ;
done ( ) ;
} ) ;
jQuery . get ( url ( "mock.php?action=json&header" ) , null , "text" )
. then ( function ( text ) {
assert . strictEqual ( text , "{\"data\":{\"lang\":\"en\",\"length\":25}}" ,
"`dataType: \"text\"` applied with a `null` callback" ) ;
done ( ) ;
} ) ;
} ) ;
2012-11-26 02:31:19 +00:00
//----------- jQuery.getJSON()
2011-02-09 16:47:33 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.getJSON( String, Hash, Function ) - JSON array" , function ( assert ) {
assert . expect ( 5 ) ;
var done = assert . async ( ) ;
2012-11-26 02:31:19 +00:00
jQuery . getJSON (
2017-08-01 16:52:45 +00:00
url ( "mock.php?action=json" ) ,
2012-11-26 02:31:19 +00:00
{
2017-08-01 16:52:45 +00:00
"array" : "1"
2012-11-26 02:31:19 +00:00
} ,
function ( json ) {
2015-08-16 03:45:28 +00:00
assert . ok ( json . length >= 2 , "Check length" ) ;
2015-08-16 06:59:58 +00:00
assert . strictEqual ( json [ 0 ] [ "name" ] , "John" , "Check JSON: first, name" ) ;
assert . strictEqual ( json [ 0 ] [ "age" ] , 21 , "Check JSON: first, age" ) ;
assert . strictEqual ( json [ 1 ] [ "name" ] , "Peter" , "Check JSON: second, name" ) ;
assert . strictEqual ( json [ 1 ] [ "age" ] , 25 , "Check JSON: second, age" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-11-26 02:31:19 +00:00
}
) ;
2015-08-16 06:59:58 +00:00
} ) ;
2011-02-09 16:47:33 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.getJSON( String, Function ) - JSON object" , function ( assert ) {
assert . expect ( 2 ) ;
var done = assert . async ( ) ;
2017-08-01 16:52:45 +00:00
jQuery . getJSON ( url ( "mock.php?action=json" ) , function ( json ) {
2015-08-16 06:59:58 +00:00
if ( json && json [ "data" ] ) {
assert . strictEqual ( json [ "data" ] [ "lang" ] , "en" , "Check JSON: lang" ) ;
assert . strictEqual ( json [ "data" ] . length , 25 , "Check JSON: length" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-12-05 13:54:14 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2011-02-09 16:47:33 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content" , function ( assert ) {
assert . expect ( 2 ) ;
var done = assert . async ( ) ;
2018-09-07 14:14:01 +00:00
var absoluteUrl = url ( "mock.php?action=json" ) ;
// Make a relative URL absolute relative to the document location
if ( ! /^[a-z][a-z0-9+.-]*:/i . test ( absoluteUrl ) ) {
// An absolute path replaces everything after the host
if ( absoluteUrl . charAt ( 0 ) === "/" ) {
absoluteUrl = window . location . href . replace ( /(:\/*[^/]*).*$/ , "$1" ) + absoluteUrl ;
// A relative path replaces the last slash-separated path segment
} else {
absoluteUrl = window . location . href . replace ( /[^/]*$/ , "" ) + absoluteUrl ;
}
}
jQuery . getJSON ( absoluteUrl , function ( json ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( json . data . lang , "en" , "Check JSON: lang" ) ;
assert . strictEqual ( json . data . length , 25 , "Check JSON: length" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2011-02-09 16:47:33 +00:00
2012-11-26 02:31:19 +00:00
//----------- jQuery.getScript()
2011-02-09 16:47:33 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.getScript( String, Function ) - with callback" ,
2015-09-08 00:26:29 +00:00
function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 2 ) ;
2015-09-08 00:26:29 +00:00
var done = assert . async ( ) ;
2011-02-04 21:19:23 +00:00
2015-09-08 00:26:29 +00:00
Globals . register ( "testBar" ) ;
2017-08-01 16:52:45 +00:00
jQuery . getScript ( url ( "mock.php?action=testbar" ) , function ( ) {
2015-09-08 00:26:29 +00:00
assert . strictEqual ( window [ "testBar" ] , "bar" , "Check if script was evaluated" ) ;
done ( ) ;
} ) ;
}
) ;
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.getScript( String, Function ) - no callback" , function ( assert ) {
assert . expect ( 1 ) ;
2015-08-16 06:59:58 +00:00
Globals . register ( "testBar" ) ;
2017-08-01 16:52:45 +00:00
jQuery . getScript ( url ( "mock.php?action=testbar" ) ) . done ( assert . async ( ) ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
2022-01-04 15:27:18 +00:00
QUnit . test ( "trac-8082 - jQuery.getScript( String, Function ) - source as responseText" , function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 2 ) ;
2015-09-08 00:26:29 +00:00
var done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
Globals . register ( "testBar" ) ;
2017-08-01 16:52:45 +00:00
jQuery . getScript ( url ( "mock.php?action=testbar" ) , function ( data , _ , jqXHR ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( data , jqXHR . responseText , "Same-domain script requests returns the source of the script" ) ;
2015-09-08 00:26:29 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2011-02-04 21:19:23 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.getScript( Object ) - with callback" , function ( assert ) {
assert . expect ( 2 ) ;
2018-01-03 16:01:26 +00:00
var done = assert . async ( ) ;
Globals . register ( "testBar" ) ;
jQuery . getScript ( {
url : url ( "mock.php?action=testbar" ) ,
success : function ( ) {
assert . strictEqual ( window [ "testBar" ] , "bar" , "Check if script was evaluated" ) ;
done ( ) ;
}
} ) ;
} ) ;
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.getScript( Object ) - no callback" , function ( assert ) {
assert . expect ( 1 ) ;
2018-01-03 16:01:26 +00:00
Globals . register ( "testBar" ) ;
jQuery . getScript ( { url : url ( "mock.php?action=testbar" ) } ) . done ( assert . async ( ) ) ;
} ) ;
2015-08-16 03:45:28 +00:00
// //----------- jQuery.fn.load()
2012-12-31 18:31:03 +00:00
2012-11-26 02:31:19 +00:00
// check if load can be called with only url
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String )" , function ( assert ) {
assert . expect ( 2 ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
beforeSend : function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( this . type , "GET" , "no data means GET request" ) ;
2012-10-15 19:53:39 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( baseURL + "name.html" , assert . async ( ) ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2011-02-04 21:19:23 +00:00
2015-08-16 03:45:28 +00:00
QUnit . test ( "jQuery.fn.load() - 404 error callbacks" , function ( assert ) {
assert . expect ( 6 ) ;
var done = assert . async ( ) ;
addGlobalEvents ( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError" , assert ) ( ) ;
2020-01-07 22:59:08 +00:00
jQuery ( document ) . on ( "ajaxStop" , done ) ;
2020-03-16 20:49:29 +00:00
jQuery ( "<div></div>" ) . load ( baseURL + "404.txt" , function ( ) {
2015-08-16 03:45:28 +00:00
assert . ok ( true , "complete" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2011-02-04 21:19:23 +00:00
2012-11-26 02:31:19 +00:00
// check if load can be called with url and null data
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String, null )" , function ( assert ) {
assert . expect ( 2 ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
beforeSend : function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( this . type , "GET" , "no data means GET request" ) ;
2012-10-15 19:53:39 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( baseURL + "name.html" , null , assert . async ( ) ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-03-07 14:39:39 +00:00
2012-11-26 02:31:19 +00:00
// check if load can be called with url and undefined data
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String, undefined )" , function ( assert ) {
assert . expect ( 2 ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
beforeSend : function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( this . type , "GET" , "no data means GET request" ) ;
2012-11-26 02:31:19 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( baseURL + "name.html" , undefined , assert . async ( ) ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-03-07 15:54:05 +00:00
2012-11-26 02:31:19 +00:00
// check if load can be called with only url
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( URL_SELECTOR )" , function ( assert ) {
assert . expect ( 1 ) ;
var done = assert . async ( ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( baseURL + "test3.html div.user" , function ( ) {
2015-08-16 06:59:58 +00:00
assert . strictEqual ( jQuery ( this ) . children ( "div" ) . length , 2 , "Verify that specific elements were injected" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-03-07 15:54:05 +00:00
2022-01-04 15:27:18 +00:00
// Selector should be trimmed to avoid leading spaces (trac-14773)
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( URL_SELECTOR with spaces )" , function ( assert ) {
assert . expect ( 1 ) ;
var done = assert . async ( ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( baseURL + "test3.html #superuser " , function ( ) {
2015-08-16 06:59:58 +00:00
assert . strictEqual ( jQuery ( this ) . children ( "div" ) . length , 1 , "Verify that specific elements were injected" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2014-03-04 02:37:56 +00:00
2022-01-04 15:27:18 +00:00
// Selector should be trimmed to avoid leading spaces (trac-14773)
2022-01-12 22:23:42 +00:00
// Selector should include any valid non-HTML whitespace (gh-3003)
QUnit . test ( "jQuery.fn.load( URL_SELECTOR with non-HTML whitespace(gh-3003) )" , function ( assert ) {
2016-09-12 16:32:02 +00:00
assert . expect ( 1 ) ;
var done = assert . async ( ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( baseURL + "test3.html #whitespace\\\\xA0 " , function ( ) {
2016-09-12 16:32:02 +00:00
assert . strictEqual ( jQuery ( this ) . children ( "div" ) . length , 1 , "Verify that specific elements were injected" ) ;
done ( ) ;
} ) ;
} ) ;
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String, Function ) - simple: inject text into DOM" , function ( assert ) {
assert . expect ( 2 ) ;
var done = assert . async ( ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( url ( "name.html" ) , function ( ) {
2015-08-16 06:59:58 +00:00
assert . ok ( /^ERROR/ . test ( jQuery ( "#first" ) . text ( ) ) , "Check if content was injected into the DOM" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-03-07 15:54:05 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String, Function ) - check scripts" , function ( assert ) {
assert . expect ( 7 ) ;
var done = assert . async ( ) ;
2012-11-26 02:31:19 +00:00
var verifyEvaluation = function ( ) {
2015-08-16 06:59:58 +00:00
assert . strictEqual ( window [ "testBar" ] , "bar" , "Check if script src was evaluated after load" ) ;
assert . strictEqual ( jQuery ( "#ap" ) . html ( ) , "bar" , "Check if script evaluation has modified DOM" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2012-11-26 02:31:19 +00:00
} ;
2012-12-03 04:32:16 +00:00
2015-08-16 06:59:58 +00:00
Globals . register ( "testFoo" ) ;
Globals . register ( "testBar" ) ;
2012-12-03 04:32:16 +00:00
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( url ( "mock.php?action=testHTML&baseURL=" + baseURL ) , function ( ) {
2015-08-16 06:59:58 +00:00
assert . ok ( jQuery ( "#first" ) . html ( ) . match ( /^html text/ ) , "Check content after loading html" ) ;
assert . strictEqual ( jQuery ( "#foo" ) . html ( ) , "foo" , "Check if script evaluation has modified DOM" ) ;
assert . strictEqual ( window [ "testFoo" ] , "foo" , "Check if script was evaluated after load" ) ;
2012-11-26 02:31:19 +00:00
setTimeout ( verifyEvaluation , 600 ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-11-26 02:31:19 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String, Function ) - check file with only a script tag" , function ( assert ) {
assert . expect ( 3 ) ;
var done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
Globals . register ( "testFoo" ) ;
2012-12-05 13:54:14 +00:00
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( url ( "test2.html" ) , function ( ) {
2015-08-16 06:59:58 +00:00
assert . strictEqual ( jQuery ( "#foo" ) . html ( ) , "foo" , "Check if script evaluation has modified DOM" ) ;
assert . strictEqual ( window [ "testFoo" ] , "foo" , "Check if script was evaluated after load" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-03-07 15:54:05 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String, Function ) - dataFilter in ajaxSettings" , function ( assert ) {
assert . expect ( 2 ) ;
var done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
dataFilter : function ( ) {
return "Hello World" ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2020-03-16 20:49:29 +00:00
jQuery ( "<div></div>" ) . load ( url ( "name.html" ) , function ( responseText ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( jQuery ( this ) . html ( ) , "Hello World" , "Test div was filled with filtered data" ) ;
assert . strictEqual ( responseText , "Hello World" , "Test callback receives filtered data" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-03-07 15:54:05 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String, Object, Function )" , function ( assert ) {
assert . expect ( 2 ) ;
var done = assert . async ( ) ;
2020-03-16 20:49:29 +00:00
jQuery ( "<div></div>" ) . load ( url ( "mock.php?action=echoHtml" ) , {
2012-12-05 13:54:14 +00:00
"bar" : "ok"
2012-11-26 02:31:19 +00:00
} , function ( ) {
2017-08-01 16:52:45 +00:00
var $node = jQuery ( this ) ;
assert . strictEqual ( $node . find ( "#method" ) . text ( ) , "POST" , "Check method" ) ;
assert . strictEqual ( $node . find ( "#data" ) . text ( ) , "bar=ok" , "Check if data is passed correctly" ) ;
2019-02-18 18:02:38 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2011-01-19 17:44:21 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load( String, String, Function )" , function ( assert ) {
assert . expect ( 2 ) ;
2015-08-16 03:45:28 +00:00
var done = assert . async ( ) ;
2020-03-16 20:49:29 +00:00
jQuery ( "<div></div>" ) . load ( url ( "mock.php?action=echoHtml" ) , "foo=3&bar=ok" , function ( ) {
2017-08-01 16:52:45 +00:00
var $node = jQuery ( this ) ;
assert . strictEqual ( $node . find ( "#method" ) . text ( ) , "GET" , "Check method" ) ;
assert . ok ( $node . find ( "#query" ) . text ( ) . match ( /foo=3&bar=ok/ ) , "Check if a string of data is passed correctly" ) ;
2015-08-16 03:45:28 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-10-16 15:37:15 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.fn.load() - callbacks get the correct parameters" , function ( assert ) {
assert . expect ( 8 ) ;
var completeArgs = { } ,
done = assert . async ( ) ;
2012-10-16 15:37:15 +00:00
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
success : function ( _ , status , jqXHR ) {
completeArgs [ this . url ] = [ jqXHR . responseText , status , jqXHR ] ;
} ,
error : function ( jqXHR , status ) {
completeArgs [ this . url ] = [ jqXHR . responseText , status , jqXHR ] ;
}
2015-08-16 06:59:58 +00:00
} ) ;
2012-10-16 15:37:15 +00:00
2012-11-26 02:31:19 +00:00
jQuery . when . apply (
jQuery ,
2015-08-16 06:59:58 +00:00
jQuery . map ( [
2012-11-26 02:31:19 +00:00
{
type : "success" ,
2017-08-01 16:52:45 +00:00
url : baseURL + "mock.php?action=echoQuery&arg=pop"
2012-11-26 02:31:19 +00:00
} ,
{
type : "error" ,
2017-08-01 16:52:45 +00:00
url : baseURL + "404.txt"
2012-11-26 02:31:19 +00:00
}
] ,
function ( options ) {
2015-08-16 06:59:58 +00:00
return jQuery . Deferred ( function ( defer ) {
jQuery ( "#foo" ) . load ( options . url , function ( ) {
2012-11-26 02:31:19 +00:00
var args = arguments ;
2015-08-16 03:45:28 +00:00
assert . strictEqual ( completeArgs [ options . url ] . length , args . length , "same number of arguments (" + options . type + ")" ) ;
2012-11-26 02:31:19 +00:00
jQuery . each ( completeArgs [ options . url ] , function ( i , value ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( args [ i ] , value , "argument #" + i + " is the same (" + options . type + ")" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
defer . resolve ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
} )
2015-08-16 03:45:28 +00:00
) . always ( done ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-10-16 15:37:15 +00:00
2022-01-04 15:27:18 +00:00
QUnit . test ( "trac-2046 - jQuery.fn.load( String, Function ) with ajaxSetup on dataType json" , function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 1 ) ;
2015-08-16 03:45:28 +00:00
var done = assert . async ( ) ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
dataType : "json"
2015-08-16 06:59:58 +00:00
} ) ;
2020-01-07 22:59:08 +00:00
jQuery ( document ) . on ( "ajaxComplete" , function ( e , xml , s ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( s . dataType , "html" , "Verify the load() dataType was html" ) ;
2015-08-16 06:59:58 +00:00
jQuery ( document ) . off ( "ajaxComplete" ) ;
2015-08-16 03:45:28 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#first" ) . load ( baseURL + "test3.html" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-10-16 15:37:15 +00:00
2022-01-04 15:27:18 +00:00
QUnit . test ( "trac-10524 - jQuery.fn.load() - data specified in ajaxSettings is merged in" , function ( assert ) {
2019-02-18 18:02:38 +00:00
assert . expect ( 1 ) ;
2015-08-16 03:45:28 +00:00
var done = assert . async ( ) ;
2012-12-05 13:54:14 +00:00
var data = {
"baz" : 1
} ;
2015-08-16 06:59:58 +00:00
jQuery . ajaxSetup ( {
2012-11-26 02:31:19 +00:00
data : {
"foo" : "bar"
}
2015-08-16 06:59:58 +00:00
} ) ;
2017-08-01 16:52:45 +00:00
jQuery ( "#foo" ) . load ( baseURL + "mock.php?action=echoQuery" , data ) ;
2020-01-07 22:59:08 +00:00
jQuery ( document ) . on ( "ajaxComplete" , function ( event , jqXHR , options ) {
2015-08-16 06:59:58 +00:00
assert . ok ( ~ options . data . indexOf ( "foo=bar" ) , "Data from ajaxSettings was used" ) ;
2015-08-16 03:45:28 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-10-16 15:37:15 +00:00
2015-08-16 03:45:28 +00:00
// //----------- jQuery.post()
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.post() - data" , function ( assert ) {
assert . expect ( 3 ) ;
2015-08-16 03:45:28 +00:00
var done = assert . async ( ) ;
2012-10-16 15:37:15 +00:00
jQuery . when (
2012-11-26 02:31:19 +00:00
jQuery . post (
2017-08-01 16:52:45 +00:00
url ( "mock.php?action=xml" ) ,
2012-11-26 02:31:19 +00:00
{
2017-08-01 16:52:45 +00:00
cal : "5-2"
2012-11-26 02:31:19 +00:00
} ,
function ( xml ) {
2015-08-16 06:59:58 +00:00
jQuery ( "math" , xml ) . each ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( jQuery ( "calculation" , this ) . text ( ) , "5-2" , "Check for XML" ) ;
assert . strictEqual ( jQuery ( "result" , this ) . text ( ) , "3" , "Check for XML" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
}
) ,
2015-08-16 06:59:58 +00:00
jQuery . ajax ( {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=echoData" ) ,
2012-11-26 02:31:19 +00:00
type : "POST" ,
data : {
2012-12-05 13:54:14 +00:00
"test" : {
"length" : 7 ,
"foo" : "bar"
2012-11-26 02:31:19 +00:00
}
} ,
success : function ( data ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( data , "test%5Blength%5D=7&test%5Bfoo%5D=bar" , "Check if a sub-object with a length param is serialized correctly" ) ;
2012-11-26 02:31:19 +00:00
}
2015-08-16 06:59:58 +00:00
} )
2017-08-01 16:52:45 +00:00
) . always ( done ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-03 04:06:50 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery.post( String, Hash, Function ) - simple with xml" , function ( assert ) {
assert . expect ( 4 ) ;
2015-08-16 03:45:28 +00:00
var done = assert . async ( ) ;
2012-11-26 02:31:19 +00:00
jQuery . when (
jQuery . post (
2017-08-01 16:52:45 +00:00
url ( "mock.php?action=xml" ) ,
2012-11-26 02:31:19 +00:00
{
2017-08-01 16:52:45 +00:00
cal : "5-2"
2012-11-26 02:31:19 +00:00
} ,
function ( xml ) {
2015-08-16 06:59:58 +00:00
jQuery ( "math" , xml ) . each ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( jQuery ( "calculation" , this ) . text ( ) , "5-2" , "Check for XML" ) ;
assert . strictEqual ( jQuery ( "result" , this ) . text ( ) , "3" , "Check for XML" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-26 02:31:19 +00:00
}
) ,
2017-08-01 16:52:45 +00:00
jQuery . post ( url ( "mock.php?action=xml&cal=5-2" ) , { } , function ( xml ) {
2015-08-16 06:59:58 +00:00
jQuery ( "math" , xml ) . each ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( jQuery ( "calculation" , this ) . text ( ) , "5-2" , "Check for XML" ) ;
assert . strictEqual ( jQuery ( "result" , this ) . text ( ) , "3" , "Check for XML" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} )
) . always ( function ( ) {
2015-08-16 03:45:28 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2012-10-16 18:51:54 +00:00
2019-02-18 18:02:38 +00:00
QUnit . test ( "jQuery[get|post]( options ) - simple with xml" , function ( assert ) {
assert . expect ( 2 ) ;
2015-08-16 03:45:28 +00:00
var done = assert . async ( ) ;
2015-01-06 03:00:12 +00:00
jQuery . when . apply ( jQuery ,
2015-08-16 06:59:58 +00:00
jQuery . map ( [ "get" , "post" ] , function ( method ) {
return jQuery [ method ] ( {
2017-08-01 16:52:45 +00:00
url : url ( "mock.php?action=xml" ) ,
2015-01-06 03:00:12 +00:00
data : {
2017-08-01 16:52:45 +00:00
cal : "5-2"
2015-01-06 03:00:12 +00:00
} ,
success : function ( xml ) {
2015-08-16 06:59:58 +00:00
jQuery ( "math" , xml ) . each ( function ( ) {
2015-08-16 03:45:28 +00:00
assert . strictEqual ( jQuery ( "result" , this ) . text ( ) , "3" , "Check for XML" ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2015-01-06 03:00:12 +00:00
}
2015-08-16 06:59:58 +00:00
} ) ;
} )
) . always ( function ( ) {
2015-08-16 03:45:28 +00:00
done ( ) ;
2015-08-16 06:59:58 +00:00
} ) ;
} ) ;
2015-01-06 03:00:12 +00:00
2012-11-26 02:31:19 +00:00
//----------- jQuery.active
2012-10-16 18:51:54 +00:00
2015-08-16 03:45:28 +00:00
QUnit . test ( "jQuery.active" , function ( assert ) {
assert . expect ( 1 ) ;
assert . ok ( jQuery . active === 0 , "ajax active counter should be zero: " + jQuery . active ) ;
2015-08-16 06:59:58 +00:00
} ) ;
2012-11-03 04:06:50 +00:00
2023-02-01 12:48:35 +00:00
ajaxTest ( "jQuery.ajax() - FormData" , 1 , function ( assert ) {
var formData = new FormData ( ) ;
formData . append ( "key1" , "value1" ) ;
formData . append ( "key2" , "value2" ) ;
return {
url : url ( "mock.php?action=formData" ) ,
method : "post" ,
data : formData ,
success : function ( data ) {
assert . strictEqual ( data , "key1 -> value1, key2 -> value2" ,
"FormData sent correctly" ) ;
}
} ;
} ) ;
ajaxTest ( "jQuery.ajax() - URLSearchParams" , 1 , function ( assert ) {
var urlSearchParams = new URLSearchParams ( ) ;
urlSearchParams . append ( "name" , "peter" ) ;
return {
url : url ( "mock.php?action=name" ) ,
method : "post" ,
data : urlSearchParams ,
success : function ( data ) {
assert . strictEqual ( data , "pan" , "URLSearchParams sent correctly" ) ;
}
} ;
} , QUnit . testUnlessIE ) ;
ajaxTest ( "jQuery.ajax() - Blob" , 1 , function ( assert ) {
var blob = new Blob ( [ "name=peter" ] , { type : "text/plain" } ) ;
return {
url : url ( "mock.php?action=name" ) ,
method : "post" ,
data : blob ,
success : function ( data ) {
assert . strictEqual ( data , "pan" , "Blob sent correctly" ) ;
}
} ;
} ) ;
2015-08-16 06:59:58 +00:00
} ) ( ) ;