Brak opisu

ajax.js 83KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944
  1. QUnit.module( "ajax", {
  2. afterEach: function() {
  3. jQuery( document ).off( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" );
  4. moduleTeardown.apply( this, arguments );
  5. }
  6. } );
  7. ( function() {
  8. QUnit.test( "Unit Testing Environment", function( assert ) {
  9. assert.expect( 2 );
  10. assert.ok( hasPHP, "Running in an environment with PHP support. The AJAX tests only run if the environment supports PHP!" );
  11. 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!)" );
  12. } );
  13. if ( !jQuery.ajax || ( isLocal && !hasPHP ) ) {
  14. return;
  15. }
  16. function addGlobalEvents( expected, assert ) {
  17. return function() {
  18. expected = expected || "";
  19. jQuery( document ).on( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess", function( e ) {
  20. assert.ok( expected.indexOf( e.type ) !== -1, e.type );
  21. } );
  22. };
  23. }
  24. //----------- jQuery.ajax()
  25. testIframe(
  26. "XMLHttpRequest - Attempt to block tests because of dangling XHR requests (IE)",
  27. "ajax/unreleasedXHR.html",
  28. function( assert ) {
  29. assert.expect( 1 );
  30. assert.ok( true, "done" );
  31. }
  32. );
  33. ajaxTest( "jQuery.ajax() - success callbacks", 8, function( assert ) {
  34. return {
  35. setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
  36. url: url( "name.html" ),
  37. beforeSend: function() {
  38. assert.ok( true, "beforeSend" );
  39. },
  40. success: function() {
  41. assert.ok( true, "success" );
  42. },
  43. complete: function() {
  44. assert.ok( true, "complete" );
  45. }
  46. };
  47. } );
  48. ajaxTest( "jQuery.ajax() - success callbacks - (url, options) syntax", 8, function( assert ) {
  49. return {
  50. setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
  51. create: function( options ) {
  52. return jQuery.ajax( url( "name.html" ), options );
  53. },
  54. beforeSend: function() {
  55. assert.ok( true, "beforeSend" );
  56. },
  57. success: function() {
  58. assert.ok( true, "success" );
  59. },
  60. complete: function() {
  61. assert.ok( true, "complete" );
  62. }
  63. };
  64. } );
  65. ajaxTest( "jQuery.ajax() - execute js for crossOrigin when dataType option is provided", 3,
  66. function( assert ) {
  67. return {
  68. create: function( options ) {
  69. options.crossDomain = true;
  70. options.dataType = "script";
  71. return jQuery.ajax( url( "mock.php?action=script&header=ecma" ), options );
  72. },
  73. success: function() {
  74. assert.ok( true, "success" );
  75. },
  76. complete: function() {
  77. assert.ok( true, "complete" );
  78. }
  79. };
  80. }
  81. );
  82. ajaxTest( "jQuery.ajax() - custom attributes for script tag", 4,
  83. function( assert ) {
  84. return {
  85. create: function( options ) {
  86. var xhr;
  87. options.dataType = "script";
  88. options.scriptAttrs = { id: "jquery-ajax-test", async: "async" };
  89. xhr = jQuery.ajax( url( "mock.php?action=script" ), options );
  90. assert.equal( jQuery( "#jquery-ajax-test" ).attr( "async" ), "async", "attr value" );
  91. return xhr;
  92. },
  93. success: function() {
  94. assert.ok( true, "success" );
  95. },
  96. complete: function() {
  97. assert.ok( true, "complete" );
  98. }
  99. };
  100. }
  101. );
  102. ajaxTest( "jQuery.ajax() - do not execute js (crossOrigin)", 2, function( assert ) {
  103. return {
  104. create: function( options ) {
  105. options.crossDomain = true;
  106. return jQuery.ajax( url( "mock.php?action=script&header" ), options );
  107. },
  108. success: function() {
  109. assert.ok( true, "success" );
  110. },
  111. fail: function() {
  112. if ( jQuery.support.cors === false ) {
  113. assert.ok( true, "fail" );
  114. }
  115. },
  116. complete: function() {
  117. assert.ok( true, "complete" );
  118. }
  119. };
  120. } );
  121. ajaxTest( "jQuery.ajax() - success callbacks (late binding)", 8, function( assert ) {
  122. return {
  123. setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
  124. url: url( "name.html" ),
  125. beforeSend: function() {
  126. assert.ok( true, "beforeSend" );
  127. },
  128. success: true,
  129. afterSend: function( request ) {
  130. request.always( function() {
  131. assert.ok( true, "complete" );
  132. } ).done( function() {
  133. assert.ok( true, "success" );
  134. } ).fail( function() {
  135. assert.ok( false, "error" );
  136. } );
  137. }
  138. };
  139. } );
  140. ajaxTest( "jQuery.ajax() - success callbacks (oncomplete binding)", 8, function( assert ) {
  141. return {
  142. setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
  143. url: url( "name.html" ),
  144. beforeSend: function() {
  145. assert.ok( true, "beforeSend" );
  146. },
  147. success: true,
  148. complete: function( xhr ) {
  149. xhr.always( function() {
  150. assert.ok( true, "complete" );
  151. } ).done( function() {
  152. assert.ok( true, "success" );
  153. } ).fail( function() {
  154. assert.ok( false, "error" );
  155. } );
  156. }
  157. };
  158. } );
  159. ajaxTest( "jQuery.ajax() - error callbacks", 8, function( assert ) {
  160. return {
  161. setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError", assert ),
  162. url: url( "mock.php?action=wait&wait=5" ),
  163. beforeSend: function() {
  164. assert.ok( true, "beforeSend" );
  165. },
  166. afterSend: function( request ) {
  167. request.abort();
  168. },
  169. error: function() {
  170. assert.ok( true, "error" );
  171. },
  172. complete: function() {
  173. assert.ok( true, "complete" );
  174. }
  175. };
  176. } );
  177. ajaxTest( "jQuery.ajax() - textStatus and errorThrown values", 4, function( assert ) {
  178. return [ {
  179. url: url( "mock.php?action=wait&wait=5" ),
  180. error: function( _, textStatus, errorThrown ) {
  181. assert.strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
  182. assert.strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort" );
  183. },
  184. afterSend: function( request ) {
  185. request.abort();
  186. }
  187. },
  188. {
  189. url: url( "mock.php?action=wait&wait=5" ),
  190. error: function( _, textStatus, errorThrown ) {
  191. assert.strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
  192. assert.strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')" );
  193. },
  194. afterSend: function( request ) {
  195. request.abort( "mystatus" );
  196. }
  197. } ];
  198. } );
  199. ajaxTest( "jQuery.ajax() - responseText on error", 1, function( assert ) {
  200. return {
  201. url: url( "mock.php?action=error" ),
  202. error: function( xhr ) {
  203. assert.strictEqual( xhr.responseText, "plain text message", "Test jqXHR.responseText is filled for HTTP errors" );
  204. }
  205. };
  206. } );
  207. QUnit.test( "jQuery.ajax() - retry with jQuery.ajax( this )", function( assert ) {
  208. assert.expect( 2 );
  209. var previousUrl,
  210. firstTime = true,
  211. done = assert.async();
  212. jQuery.ajax( {
  213. url: url( "mock.php?action=error" ),
  214. error: function() {
  215. if ( firstTime ) {
  216. firstTime = false;
  217. jQuery.ajax( this );
  218. } else {
  219. assert.ok( true, "Test retrying with jQuery.ajax(this) works" );
  220. jQuery.ajax( {
  221. url: url( "mock.php?action=error&x=2" ),
  222. beforeSend: function() {
  223. if ( !previousUrl ) {
  224. previousUrl = this.url;
  225. } else {
  226. assert.strictEqual( this.url, previousUrl, "url parameters are not re-appended" );
  227. done();
  228. return false;
  229. }
  230. },
  231. error: function() {
  232. jQuery.ajax( this );
  233. }
  234. } );
  235. }
  236. }
  237. } );
  238. } );
  239. ajaxTest( "jQuery.ajax() - headers", 8, function( assert ) {
  240. return {
  241. setup: function() {
  242. jQuery( document ).on( "ajaxSend", function( evt, xhr ) {
  243. xhr.setRequestHeader( "ajax-send", "test" );
  244. } );
  245. },
  246. url: url( "mock.php?action=headers&keys=siMPle|SometHing-elsE|OthEr|Nullable|undefined|Empty|ajax-send" ),
  247. headers: {
  248. "siMPle": "value",
  249. "SometHing-elsE": "other value",
  250. "OthEr": "something else",
  251. "Nullable": null,
  252. "undefined": undefined
  253. // Support: IE 9 - 11, Edge 12 - 14 only
  254. // Not all browsers allow empty-string headers
  255. //"Empty": ""
  256. },
  257. success: function( data, _, xhr ) {
  258. var i, emptyHeader,
  259. isAndroid = /android 4\.[0-3]/i.test( navigator.userAgent ),
  260. requestHeaders = jQuery.extend( this.headers, {
  261. "ajax-send": "test"
  262. } ),
  263. tmp = [];
  264. for ( i in requestHeaders ) {
  265. tmp.push( i, ": ", requestHeaders[ i ] + "", "\n" );
  266. }
  267. tmp = tmp.join( "" );
  268. assert.strictEqual( data, tmp, "Headers were sent" );
  269. assert.strictEqual( xhr.getResponseHeader( "Sample-Header" ), "Hello World", "Sample header received" );
  270. assert.ok( data.indexOf( "undefined" ) < 0, "Undefined header value was not sent" );
  271. emptyHeader = xhr.getResponseHeader( "Empty-Header" );
  272. if ( emptyHeader === null ) {
  273. assert.ok( true, "Firefox doesn't support empty headers" );
  274. } else {
  275. assert.strictEqual( emptyHeader, "", "Empty header received" );
  276. }
  277. assert.strictEqual( xhr.getResponseHeader( "Sample-Header2" ), "Hello World 2", "Second sample header received" );
  278. if ( isAndroid ) {
  279. // Support: Android 4.0-4.3 only
  280. // Android Browser only returns the last value for each header
  281. // so there's no way for jQuery get all parts.
  282. assert.ok( true, "Android doesn't support repeated header names" );
  283. } else {
  284. assert.strictEqual( xhr.getResponseHeader( "List-Header" ), "Item 1, Item 2", "List header received" );
  285. }
  286. if ( isAndroid && QUnit.isSwarm ) {
  287. // Support: Android 4.0-4.3 on BrowserStack only
  288. // Android Browser versions provided by BrowserStack fail this test
  289. // while locally fired emulators don't, even when they connect
  290. // to TestSwarm. Just skip the test there to avoid a red build.
  291. assert.ok( true, "BrowserStack's Android fails the \"prototype collision (constructor)\" test" );
  292. } else {
  293. assert.strictEqual( xhr.getResponseHeader( "constructor" ), "prototype collision (constructor)", "constructor header received" );
  294. }
  295. assert.strictEqual( xhr.getResponseHeader( "__proto__" ), null, "Undefined __proto__ header not received" );
  296. }
  297. };
  298. } );
  299. ajaxTest( "jQuery.ajax() - Accept header", 1, function( assert ) {
  300. return {
  301. url: url( "mock.php?action=headers&keys=accept" ),
  302. headers: {
  303. Accept: "very wrong accept value"
  304. },
  305. beforeSend: function( xhr ) {
  306. xhr.setRequestHeader( "Accept", "*/*" );
  307. },
  308. success: function( data ) {
  309. assert.strictEqual( data, "accept: */*\n", "Test Accept header is set to last value provided" );
  310. }
  311. };
  312. } );
  313. ajaxTest( "jQuery.ajax() - contentType", 2, function( assert ) {
  314. return [
  315. {
  316. url: url( "mock.php?action=headers&keys=content-type" ),
  317. contentType: "test",
  318. success: function( data ) {
  319. assert.strictEqual( data, "content-type: test\n", "Test content-type is sent when options.contentType is set" );
  320. }
  321. },
  322. {
  323. url: url( "mock.php?action=headers&keys=content-type" ),
  324. contentType: false,
  325. success: function( data ) {
  326. // Some server/interpreter combinations always supply a Content-Type to scripts
  327. data = data || "content-type: \n";
  328. assert.strictEqual( data, "content-type: \n", "Test content-type is not set when options.contentType===false" );
  329. }
  330. }
  331. ];
  332. } );
  333. ajaxTest( "jQuery.ajax() - protocol-less urls", 1, function( assert ) {
  334. return {
  335. url: "//somedomain.com",
  336. beforeSend: function( xhr, settings ) {
  337. assert.equal( settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added." );
  338. return false;
  339. },
  340. error: true
  341. };
  342. } );
  343. ajaxTest( "jQuery.ajax() - URL fragment component preservation", 4, function( assert ) {
  344. return [
  345. {
  346. url: baseURL + "name.html#foo",
  347. beforeSend: function( xhr, settings ) {
  348. assert.equal( settings.url, baseURL + "name.html#foo",
  349. "hash preserved for request with no query component." );
  350. return false;
  351. },
  352. error: true
  353. },
  354. {
  355. url: baseURL + "name.html?abc#foo",
  356. beforeSend: function( xhr, settings ) {
  357. assert.equal( settings.url, baseURL + "name.html?abc#foo",
  358. "hash preserved for request with query component." );
  359. return false;
  360. },
  361. error: true
  362. },
  363. {
  364. url: baseURL + "name.html?abc#foo",
  365. data: {
  366. "test": 123
  367. },
  368. beforeSend: function( xhr, settings ) {
  369. assert.equal( settings.url, baseURL + "name.html?abc&test=123#foo",
  370. "hash preserved for request with query component and data." );
  371. return false;
  372. },
  373. error: true
  374. },
  375. {
  376. url: baseURL + "name.html?abc#brownies",
  377. data: {
  378. "devo": "hat"
  379. },
  380. cache: false,
  381. beforeSend: function( xhr, settings ) {
  382. // Clear the cache-buster param value
  383. var url = settings.url.replace( /_=[^&#]+/, "_=" );
  384. assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies",
  385. "hash preserved for cache-busting request with query component and data." );
  386. return false;
  387. },
  388. error: true
  389. }
  390. ];
  391. } );
  392. ajaxTest( "jQuery.ajax() - traditional param encoding", 4, function( assert ) {
  393. return [
  394. {
  395. url: "/",
  396. traditional: true,
  397. data: {
  398. "devo": "hat",
  399. "answer": 42,
  400. "quux": "a space"
  401. },
  402. beforeSend: function( xhr, settings ) {
  403. assert.equal( settings.url, "/?devo=hat&answer=42&quux=a%20space", "Simple case" );
  404. return false;
  405. },
  406. error: true
  407. },
  408. {
  409. url: "/",
  410. traditional: true,
  411. data: {
  412. "a": [ 1, 2, 3 ],
  413. "b[]": [ "b1", "b2" ]
  414. },
  415. beforeSend: function( xhr, settings ) {
  416. assert.equal( settings.url, "/?a=1&a=2&a=3&b%5B%5D=b1&b%5B%5D=b2", "Arrays" );
  417. return false;
  418. },
  419. error: true
  420. },
  421. {
  422. url: "/",
  423. traditional: true,
  424. data: {
  425. "a": [ [ 1, 2 ], [ 3, 4 ], 5 ]
  426. },
  427. beforeSend: function( xhr, settings ) {
  428. assert.equal( settings.url, "/?a=1%2C2&a=3%2C4&a=5", "Nested arrays" );
  429. return false;
  430. },
  431. error: true
  432. },
  433. {
  434. url: "/",
  435. traditional: true,
  436. data: {
  437. "a": [ "w", [ [ "x", "y" ], "z" ] ]
  438. },
  439. cache: false,
  440. beforeSend: function( xhr, settings ) {
  441. var url = settings.url.replace( /\d{3,}/, "" );
  442. assert.equal( url, "/?a=w&a=x%2Cy%2Cz&_=", "Cache-buster" );
  443. return false;
  444. },
  445. error: true
  446. }
  447. ];
  448. } );
  449. ajaxTest( "jQuery.ajax() - cross-domain detection", 8, function( assert ) {
  450. function request( url, title, crossDomainOrOptions ) {
  451. return jQuery.extend( {
  452. dataType: "jsonp",
  453. url: url,
  454. beforeSend: function( _, s ) {
  455. assert.ok( crossDomainOrOptions === false ? !s.crossDomain : s.crossDomain, title );
  456. return false;
  457. },
  458. error: true
  459. }, crossDomainOrOptions );
  460. }
  461. var loc = document.location,
  462. samePort = loc.port || ( loc.protocol === "http:" ? 80 : 443 ),
  463. otherPort = loc.port === 666 ? 667 : 666,
  464. otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
  465. return [
  466. request(
  467. loc.protocol + "//" + loc.hostname + ":" + samePort,
  468. "Test matching ports are not detected as cross-domain",
  469. false
  470. ),
  471. request(
  472. otherProtocol + "//" + loc.host,
  473. "Test different protocols are detected as cross-domain"
  474. ),
  475. request(
  476. "app:/path",
  477. "Adobe AIR app:/ URL detected as cross-domain"
  478. ),
  479. request(
  480. loc.protocol + "//example.invalid:" + ( loc.port || 80 ),
  481. "Test different hostnames are detected as cross-domain"
  482. ),
  483. request(
  484. loc.protocol + "//" + loc.hostname + ":" + otherPort,
  485. "Test different ports are detected as cross-domain"
  486. ),
  487. request(
  488. "about:blank",
  489. "Test about:blank is detected as cross-domain"
  490. ),
  491. request(
  492. loc.protocol + "//" + loc.host,
  493. "Test forced crossDomain is detected as cross-domain",
  494. {
  495. crossDomain: true
  496. }
  497. ),
  498. request(
  499. " http://otherdomain.com",
  500. "Cross-domain url with leading space is detected as cross-domain"
  501. )
  502. ];
  503. } );
  504. ajaxTest( "jQuery.ajax() - abort", 9, function( assert ) {
  505. return {
  506. setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxError ajaxComplete", assert ),
  507. url: url( "mock.php?action=wait&wait=5" ),
  508. beforeSend: function() {
  509. assert.ok( true, "beforeSend" );
  510. },
  511. afterSend: function( xhr ) {
  512. assert.strictEqual( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
  513. xhr.abort();
  514. assert.strictEqual( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
  515. },
  516. error: true,
  517. complete: function() {
  518. assert.ok( true, "complete" );
  519. }
  520. };
  521. } );
  522. if ( !/android 4\.0/i.test( navigator.userAgent ) ) {
  523. ajaxTest( "jQuery.ajax() - native abort", 2, function( assert ) {
  524. return {
  525. url: url( "mock.php?action=wait&wait=1" ),
  526. xhr: function() {
  527. var xhr = new window.XMLHttpRequest();
  528. setTimeout( function() {
  529. xhr.abort();
  530. }, 100 );
  531. return xhr;
  532. },
  533. error: function( xhr, msg ) {
  534. assert.strictEqual( msg, "error", "Native abort triggers error callback" );
  535. },
  536. complete: function() {
  537. assert.ok( true, "complete" );
  538. }
  539. };
  540. } );
  541. }
  542. // Support: Android <= 4.0 - 4.3 only
  543. // Android 4.0-4.3 does not have ontimeout on an xhr
  544. if ( "ontimeout" in new window.XMLHttpRequest() ) {
  545. ajaxTest( "jQuery.ajax() - native timeout", 2, function( assert ) {
  546. return {
  547. url: url( "mock.php?action=wait&wait=1" ),
  548. xhr: function() {
  549. var xhr = new window.XMLHttpRequest();
  550. xhr.timeout = 1;
  551. return xhr;
  552. },
  553. error: function( xhr, msg ) {
  554. assert.strictEqual( msg, "error", "Native timeout triggers error callback" );
  555. },
  556. complete: function() {
  557. assert.ok( true, "complete" );
  558. }
  559. };
  560. } );
  561. }
  562. ajaxTest( "jQuery.ajax() - events with context", 12, function( assert ) {
  563. var context = document.createElement( "div" );
  564. function event( e ) {
  565. assert.equal( this, context, e.type );
  566. }
  567. function callback( msg ) {
  568. return function() {
  569. assert.equal( this, context, "context is preserved on callback " + msg );
  570. };
  571. }
  572. return {
  573. setup: function() {
  574. jQuery( context ).appendTo( "#foo" )
  575. .on( "ajaxSend", event )
  576. .on( "ajaxComplete", event )
  577. .on( "ajaxError", event )
  578. .on( "ajaxSuccess", event );
  579. },
  580. requests: [ {
  581. url: url( "name.html" ),
  582. context: context,
  583. beforeSend: callback( "beforeSend" ),
  584. success: callback( "success" ),
  585. complete: callback( "complete" )
  586. }, {
  587. url: url( "404.txt" ),
  588. context: context,
  589. beforeSend: callback( "beforeSend" ),
  590. error: callback( "error" ),
  591. complete: callback( "complete" )
  592. } ]
  593. };
  594. } );
  595. ajaxTest( "jQuery.ajax() - events without context", 3, function( assert ) {
  596. function nocallback( msg ) {
  597. return function() {
  598. assert.equal( typeof this.url, "string", "context is settings on callback " + msg );
  599. };
  600. }
  601. return {
  602. url: url( "404.txt" ),
  603. beforeSend: nocallback( "beforeSend" ),
  604. error: nocallback( "error" ),
  605. complete: nocallback( "complete" )
  606. };
  607. } );
  608. ajaxTest( "#15118 - jQuery.ajax() - function without jQuery.event", 1, function( assert ) {
  609. var holder;
  610. return {
  611. url: url( "mock.php?action=json" ),
  612. setup: function() {
  613. holder = jQuery.event;
  614. delete jQuery.event;
  615. },
  616. complete: function() {
  617. assert.ok( true, "Call can be made without jQuery.event" );
  618. jQuery.event = holder;
  619. },
  620. success: true
  621. };
  622. } );
  623. ajaxTest( "#15160 - jQuery.ajax() - request manually aborted in ajaxSend", 3, function( assert ) {
  624. return {
  625. setup: function() {
  626. jQuery( document ).on( "ajaxSend", function( e, jqXHR ) {
  627. jqXHR.abort();
  628. } );
  629. jQuery( document ).on( "ajaxError ajaxComplete", function( e, jqXHR ) {
  630. assert.equal( jqXHR.statusText, "abort", "jqXHR.statusText equals abort on global ajaxComplete and ajaxError events" );
  631. } );
  632. },
  633. url: url( "name.html" ),
  634. error: true,
  635. complete: function() {
  636. assert.ok( true, "complete" );
  637. }
  638. };
  639. } );
  640. ajaxTest( "jQuery.ajax() - context modification", 1, function( assert ) {
  641. return {
  642. url: url( "name.html" ),
  643. context: {},
  644. beforeSend: function() {
  645. this.test = "foo";
  646. },
  647. afterSend: function() {
  648. assert.strictEqual( this.context.test, "foo", "Make sure the original object is maintained." );
  649. },
  650. success: true
  651. };
  652. } );
  653. ajaxTest( "jQuery.ajax() - context modification through ajaxSetup", 3, function( assert ) {
  654. var obj = {};
  655. return {
  656. setup: function() {
  657. jQuery.ajaxSetup( {
  658. context: obj
  659. } );
  660. assert.strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
  661. },
  662. requests: [ {
  663. url: url( "name.html" ),
  664. success: function() {
  665. assert.strictEqual( this, obj, "Make sure the original object is maintained." );
  666. }
  667. }, {
  668. url: url( "name.html" ),
  669. context: {},
  670. success: function() {
  671. assert.ok( this !== obj, "Make sure overriding context is possible." );
  672. }
  673. } ]
  674. };
  675. } );
  676. ajaxTest( "jQuery.ajax() - disabled globals", 3, function( assert ) {
  677. return {
  678. setup: addGlobalEvents( "", assert ),
  679. global: false,
  680. url: url( "name.html" ),
  681. beforeSend: function() {
  682. assert.ok( true, "beforeSend" );
  683. },
  684. success: function() {
  685. assert.ok( true, "success" );
  686. },
  687. complete: function() {
  688. assert.ok( true, "complete" );
  689. }
  690. };
  691. } );
  692. ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements", 3, function( assert ) {
  693. return {
  694. url: url( "with_fries.xml" ),
  695. dataType: "xml",
  696. success: function( resp ) {
  697. assert.equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
  698. assert.equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
  699. assert.equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
  700. }
  701. };
  702. } );
  703. ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements (over JSONP)", 3, function( assert ) {
  704. return {
  705. url: url( "mock.php?action=xmlOverJsonp" ),
  706. dataType: "jsonp xml",
  707. success: function( resp ) {
  708. assert.equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
  709. assert.equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
  710. assert.equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
  711. }
  712. };
  713. } );
  714. ajaxTest( "jQuery.ajax() - HEAD requests", 2, function( assert ) {
  715. return [
  716. {
  717. url: url( "name.html" ),
  718. type: "HEAD",
  719. success: function( data, status, xhr ) {
  720. assert.ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response" );
  721. }
  722. },
  723. {
  724. url: url( "name.html" ),
  725. data: {
  726. "whip_it": "good"
  727. },
  728. type: "HEAD",
  729. success: function( data, status, xhr ) {
  730. assert.ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response with data" );
  731. }
  732. }
  733. ];
  734. } );
  735. ajaxTest( "jQuery.ajax() - beforeSend", 1, function( assert ) {
  736. return {
  737. url: url( "name.html" ),
  738. beforeSend: function() {
  739. this.check = true;
  740. },
  741. success: function() {
  742. assert.ok( this.check, "check beforeSend was executed" );
  743. }
  744. };
  745. } );
  746. ajaxTest( "jQuery.ajax() - beforeSend, cancel request manually", 2, function( assert ) {
  747. return {
  748. create: function() {
  749. return jQuery.ajax( {
  750. url: url( "name.html" ),
  751. beforeSend: function( xhr ) {
  752. assert.ok( true, "beforeSend got called, canceling" );
  753. xhr.abort();
  754. },
  755. success: function() {
  756. assert.ok( false, "request didn't get canceled" );
  757. },
  758. complete: function() {
  759. assert.ok( false, "request didn't get canceled" );
  760. },
  761. error: function() {
  762. assert.ok( false, "request didn't get canceled" );
  763. }
  764. } );
  765. },
  766. fail: function( _, reason ) {
  767. assert.strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
  768. }
  769. };
  770. } );
  771. ajaxTest( "jQuery.ajax() - dataType html", 5, function( assert ) {
  772. return {
  773. setup: function() {
  774. Globals.register( "testFoo" );
  775. Globals.register( "testBar" );
  776. },
  777. dataType: "html",
  778. url: url( "mock.php?action=testHTML&baseURL=" + baseURL ),
  779. success: function( data ) {
  780. assert.ok( data.match( /^html text/ ), "Check content for datatype html" );
  781. jQuery( "#ap" ).html( data );
  782. assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated for datatype html" );
  783. assert.strictEqual( window[ "testBar" ], "bar", "Check if script src was evaluated for datatype html" );
  784. }
  785. };
  786. } );
  787. ajaxTest( "jQuery.ajax() - do execute scripts if JSONP from unsuccessful responses", 1, function( assert ) {
  788. var testMsg = "Unsuccessful JSONP requests should have a JSON body";
  789. return {
  790. dataType: "jsonp",
  791. url: url( "mock.php?action=errorWithScript" ),
  792. // error is the significant assertion
  793. error: function( xhr ) {
  794. var expected = { "status": 404, "msg": "Not Found" };
  795. assert.deepEqual( xhr.responseJSON, expected, testMsg );
  796. }
  797. };
  798. } );
  799. ajaxTest( "jQuery.ajax() - do not execute scripts from unsuccessful responses (gh-4250)", 11, function( assert ) {
  800. var globalEval = jQuery.globalEval;
  801. var failConverters = {
  802. "text script": function() {
  803. assert.ok( false, "No converter for unsuccessful response" );
  804. }
  805. };
  806. function request( title, options ) {
  807. var testMsg = title + ": expected file missing status";
  808. return jQuery.extend( {
  809. beforeSend: function() {
  810. jQuery.globalEval = function() {
  811. assert.ok( false, "Should not eval" );
  812. };
  813. },
  814. complete: function() {
  815. jQuery.globalEval = globalEval;
  816. },
  817. // error is the significant assertion
  818. error: function( xhr ) {
  819. assert.strictEqual( xhr.status, 404, testMsg );
  820. },
  821. success: function() {
  822. assert.ok( false, "Unanticipated success" );
  823. }
  824. }, options );
  825. }
  826. return [
  827. request(
  828. "HTML reply",
  829. {
  830. url: url( "404.txt" )
  831. }
  832. ),
  833. request(
  834. "HTML reply with dataType",
  835. {
  836. dataType: "script",
  837. url: url( "404.txt" )
  838. }
  839. ),
  840. request(
  841. "script reply",
  842. {
  843. url: url( "mock.php?action=errorWithScript&withScriptContentType" )
  844. }
  845. ),
  846. request(
  847. "non-script reply",
  848. {
  849. url: url( "mock.php?action=errorWithScript" )
  850. }
  851. ),
  852. request(
  853. "script reply with dataType",
  854. {
  855. dataType: "script",
  856. url: url( "mock.php?action=errorWithScript&withScriptContentType" )
  857. }
  858. ),
  859. request(
  860. "non-script reply with dataType",
  861. {
  862. dataType: "script",
  863. url: url( "mock.php?action=errorWithScript" )
  864. }
  865. ),
  866. request(
  867. "script reply with converter",
  868. {
  869. converters: failConverters,
  870. url: url( "mock.php?action=errorWithScript&withScriptContentType" )
  871. }
  872. ),
  873. request(
  874. "non-script reply with converter",
  875. {
  876. converters: failConverters,
  877. url: url( "mock.php?action=errorWithScript" )
  878. }
  879. ),
  880. request(
  881. "script reply with converter and dataType",
  882. {
  883. converters: failConverters,
  884. dataType: "script",
  885. url: url( "mock.php?action=errorWithScript&withScriptContentType" )
  886. }
  887. ),
  888. request(
  889. "non-script reply with converter and dataType",
  890. {
  891. converters: failConverters,
  892. dataType: "script",
  893. url: url( "mock.php?action=errorWithScript" )
  894. }
  895. ),
  896. request(
  897. "JSONP reply with dataType",
  898. {
  899. dataType: "jsonp",
  900. url: url( "mock.php?action=errorWithScript" ),
  901. beforeSend: function() {
  902. jQuery.globalEval = function( response ) {
  903. assert.ok( /"status": 404, "msg": "Not Found"/.test( response ), "Error object returned" );
  904. };
  905. }
  906. }
  907. )
  908. ];
  909. } );
  910. ajaxTest( "jQuery.ajax() - synchronous request", 1, function( assert ) {
  911. return {
  912. url: url( "json_obj.js" ),
  913. dataType: "text",
  914. async: false,
  915. success: true,
  916. afterSend: function( xhr ) {
  917. assert.ok( /^\{ "data"/.test( xhr.responseText ), "check returned text" );
  918. }
  919. };
  920. } );
  921. ajaxTest( "jQuery.ajax() - synchronous request with callbacks", 2, function( assert ) {
  922. return {
  923. url: url( "json_obj.js" ),
  924. async: false,
  925. dataType: "text",
  926. success: true,
  927. afterSend: function( xhr ) {
  928. var result;
  929. xhr.done( function( data ) {
  930. assert.ok( true, "success callback executed" );
  931. result = data;
  932. } );
  933. assert.ok( /^\{ "data"/.test( result ), "check returned text" );
  934. }
  935. };
  936. } );
  937. QUnit.test( "jQuery.ajax(), jQuery.get[Script|JSON](), jQuery.post(), pass-through request object", function( assert ) {
  938. assert.expect( 8 );
  939. var done = assert.async();
  940. var target = "name.html",
  941. successCount = 0,
  942. errorCount = 0,
  943. errorEx = "",
  944. success = function() {
  945. successCount++;
  946. };
  947. jQuery( document ).on( "ajaxError.passthru", function( e, xml ) {
  948. errorCount++;
  949. errorEx += ": " + xml.status;
  950. } );
  951. jQuery( document ).one( "ajaxStop", function() {
  952. assert.equal( successCount, 5, "Check all ajax calls successful" );
  953. assert.equal( errorCount, 0, "Check no ajax errors (status" + errorEx + ")" );
  954. jQuery( document ).off( "ajaxError.passthru" );
  955. done();
  956. } );
  957. Globals.register( "testBar" );
  958. assert.ok( jQuery.get( url( target ), success ), "get" );
  959. assert.ok( jQuery.post( url( target ), success ), "post" );
  960. assert.ok( jQuery.getScript( url( "mock.php?action=testbar" ), success ), "script" );
  961. assert.ok( jQuery.getJSON( url( "json_obj.js" ), success ), "json" );
  962. assert.ok( jQuery.ajax( {
  963. url: url( target ),
  964. success: success
  965. } ), "generic" );
  966. } );
  967. ajaxTest( "jQuery.ajax() - cache", 28, function( assert ) {
  968. var re = /_=(.*?)(&|$)/g,
  969. rootUrl = baseURL + "text.txt";
  970. function request( url, title ) {
  971. return {
  972. url: url,
  973. cache: false,
  974. beforeSend: function() {
  975. var parameter, tmp;
  976. // URL sanity check
  977. assert.equal( this.url.indexOf( rootUrl ), 0, "root url not mangled: " + this.url );
  978. assert.equal( /\&.*\?/.test( this.url ), false, "parameter delimiters in order" );
  979. while ( ( tmp = re.exec( this.url ) ) ) {
  980. assert.strictEqual( parameter, undefined, title + ": only one 'no-cache' parameter" );
  981. parameter = tmp[ 1 ];
  982. assert.notStrictEqual( parameter, "tobereplaced555", title + ": parameter (if it was there) was replaced" );
  983. }
  984. return false;
  985. },
  986. error: true
  987. };
  988. }
  989. return [
  990. request(
  991. rootUrl,
  992. "no query"
  993. ),
  994. request(
  995. rootUrl + "?",
  996. "empty query"
  997. ),
  998. request(
  999. rootUrl + "?pizza=true",
  1000. "1 parameter"
  1001. ),
  1002. request(
  1003. rootUrl + "?_=tobereplaced555",
  1004. "_= parameter"
  1005. ),
  1006. request(
  1007. rootUrl + "?pizza=true&_=tobereplaced555",
  1008. "1 parameter and _="
  1009. ),
  1010. request(
  1011. rootUrl + "?_=tobereplaced555&tv=false",
  1012. "_= and 1 parameter"
  1013. ),
  1014. request(
  1015. rootUrl + "?name=David&_=tobereplaced555&washere=true",
  1016. "2 parameters surrounding _="
  1017. )
  1018. ];
  1019. } );
  1020. jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
  1021. ajaxTest( "jQuery.ajax() - JSONP - Query String (?n)" + label, 4, function( assert ) {
  1022. return [
  1023. {
  1024. url: baseURL + "mock.php?action=jsonp&callback=?",
  1025. dataType: "jsonp",
  1026. crossDomain: crossDomain,
  1027. success: function( data ) {
  1028. assert.ok( data.data, "JSON results returned (GET, url callback)" );
  1029. }
  1030. },
  1031. {
  1032. url: baseURL + "mock.php?action=jsonp&callback=??",
  1033. dataType: "jsonp",
  1034. crossDomain: crossDomain,
  1035. success: function( data ) {
  1036. assert.ok( data.data, "JSON results returned (GET, url context-free callback)" );
  1037. }
  1038. },
  1039. {
  1040. url: baseURL + "mock.php/???action=jsonp",
  1041. dataType: "jsonp",
  1042. crossDomain: crossDomain,
  1043. success: function( data ) {
  1044. assert.ok( data.data, "JSON results returned (GET, REST-like)" );
  1045. }
  1046. },
  1047. {
  1048. url: baseURL + "mock.php/???action=jsonp&array=1",
  1049. dataType: "jsonp",
  1050. crossDomain: crossDomain,
  1051. success: function( data ) {
  1052. assert.ok( Array.isArray( data ), "JSON results returned (GET, REST-like with param)" );
  1053. }
  1054. }
  1055. ];
  1056. } );
  1057. ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 10, function( assert ) {
  1058. return {
  1059. setup: function() {
  1060. Globals.register( "functionToCleanUp" );
  1061. Globals.register( "XXX" );
  1062. Globals.register( "jsonpResults" );
  1063. window[ "jsonpResults" ] = function( data ) {
  1064. assert.ok( data[ "data" ], "JSON results returned (GET, custom callback function)" );
  1065. };
  1066. },
  1067. requests: [ {
  1068. url: baseURL + "mock.php?action=jsonp",
  1069. dataType: "jsonp",
  1070. crossDomain: crossDomain,
  1071. jsonp: "callback",
  1072. success: function( data ) {
  1073. assert.ok( data[ "data" ], "JSON results returned (GET, data obj callback)" );
  1074. }
  1075. }, {
  1076. url: baseURL + "mock.php?action=jsonp",
  1077. dataType: "jsonp",
  1078. crossDomain: crossDomain,
  1079. jsonpCallback: "jsonpResults",
  1080. success: function( data ) {
  1081. assert.strictEqual(
  1082. typeof window[ "jsonpResults" ],
  1083. "function",
  1084. "should not rewrite original function"
  1085. );
  1086. assert.ok( data.data, "JSON results returned (GET, custom callback name)" );
  1087. }
  1088. }, {
  1089. url: baseURL + "mock.php?action=jsonp",
  1090. dataType: "jsonp",
  1091. crossDomain: crossDomain,
  1092. jsonpCallback: "functionToCleanUp",
  1093. success: function( data ) {
  1094. assert.ok( data[ "data" ], "JSON results returned (GET, custom callback name to be cleaned up)" );
  1095. assert.strictEqual( window[ "functionToCleanUp" ], true, "Callback was removed (GET, custom callback name to be cleaned up)" );
  1096. var xhr;
  1097. jQuery.ajax( {
  1098. url: baseURL + "mock.php?action=jsonp",
  1099. dataType: "jsonp",
  1100. crossDomain: crossDomain,
  1101. jsonpCallback: "functionToCleanUp",
  1102. beforeSend: function( jqXHR ) {
  1103. xhr = jqXHR;
  1104. return false;
  1105. }
  1106. } );
  1107. xhr.fail( function() {
  1108. assert.ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
  1109. assert.strictEqual( window[ "functionToCleanUp" ], true, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
  1110. } );
  1111. }
  1112. }, {
  1113. url: baseURL + "mock.php?action=jsonp&callback=XXX",
  1114. dataType: "jsonp",
  1115. jsonp: false,
  1116. jsonpCallback: "XXX",
  1117. crossDomain: crossDomain,
  1118. beforeSend: function() {
  1119. assert.ok( /action=jsonp&callback=XXX&_=\d+$/.test( this.url ), "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
  1120. },
  1121. success: function( data ) {
  1122. assert.ok( data[ "data" ], "JSON results returned (GET, custom callback name with no url manipulation)" );
  1123. }
  1124. } ]
  1125. };
  1126. } );
  1127. ajaxTest( "jQuery.ajax() - JSONP - Callback in data" + label, 2, function( assert ) {
  1128. return [
  1129. {
  1130. url: baseURL + "mock.php?action=jsonp",
  1131. dataType: "jsonp",
  1132. crossDomain: crossDomain,
  1133. data: "callback=?",
  1134. success: function( data ) {
  1135. assert.ok( data.data, "JSON results returned (GET, data callback)" );
  1136. }
  1137. },
  1138. {
  1139. url: baseURL + "mock.php?action=jsonp",
  1140. dataType: "jsonp",
  1141. crossDomain: crossDomain,
  1142. data: "callback=??",
  1143. success: function( data ) {
  1144. assert.ok( data.data, "JSON results returned (GET, data context-free callback)" );
  1145. }
  1146. }
  1147. ];
  1148. } );
  1149. ajaxTest( "jQuery.ajax() - JSONP - POST" + label, 3, function( assert ) {
  1150. return [
  1151. {
  1152. type: "POST",
  1153. url: baseURL + "mock.php?action=jsonp",
  1154. dataType: "jsonp",
  1155. crossDomain: crossDomain,
  1156. success: function( data ) {
  1157. assert.ok( data[ "data" ], "JSON results returned (POST, no callback)" );
  1158. }
  1159. },
  1160. {
  1161. type: "POST",
  1162. url: baseURL + "mock.php?action=jsonp",
  1163. data: "callback=?",
  1164. dataType: "jsonp",
  1165. crossDomain: crossDomain,
  1166. success: function( data ) {
  1167. assert.ok( data[ "data" ], "JSON results returned (POST, data callback)" );
  1168. }
  1169. },
  1170. {
  1171. type: "POST",
  1172. url: baseURL + "mock.php?action=jsonp",
  1173. jsonp: "callback",
  1174. dataType: "jsonp",
  1175. crossDomain: crossDomain,
  1176. success: function( data ) {
  1177. assert.ok( data[ "data" ], "JSON results returned (POST, data obj callback)" );
  1178. }
  1179. }
  1180. ];
  1181. } );
  1182. ajaxTest( "jQuery.ajax() - JSONP" + label, 3, function( assert ) {
  1183. return [
  1184. {
  1185. url: baseURL + "mock.php?action=jsonp",
  1186. dataType: "jsonp",
  1187. crossDomain: crossDomain,
  1188. success: function( data ) {
  1189. assert.ok( data.data, "JSON results returned (GET, no callback)" );
  1190. }
  1191. },
  1192. {
  1193. create: function( options ) {
  1194. var request = jQuery.ajax( options ),
  1195. promise = request.then( function( data ) {
  1196. assert.ok( data.data, "first request: JSON results returned (GET, no callback)" );
  1197. request = jQuery.ajax( this ).done( function( data ) {
  1198. assert.ok( data.data, "this re-used: JSON results returned (GET, no callback)" );
  1199. } );
  1200. promise.abort = request.abort;
  1201. return request;
  1202. } );
  1203. promise.abort = request.abort;
  1204. return promise;
  1205. },
  1206. url: baseURL + "mock.php?action=jsonp",
  1207. dataType: "jsonp",
  1208. crossDomain: crossDomain,
  1209. success: true
  1210. }
  1211. ];
  1212. } );
  1213. } );
  1214. ajaxTest( "jQuery.ajax() - script, Remote", 2, function( assert ) {
  1215. return {
  1216. setup: function() {
  1217. Globals.register( "testBar" );
  1218. },
  1219. url: url( "mock.php?action=testbar" ),
  1220. dataType: "script",
  1221. success: function() {
  1222. assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
  1223. }
  1224. };
  1225. } );
  1226. ajaxTest( "jQuery.ajax() - script, Remote with POST", 3, function( assert ) {
  1227. return {
  1228. setup: function() {
  1229. Globals.register( "testBar" );
  1230. },
  1231. url: url( "mock.php?action=testbar" ),
  1232. type: "POST",
  1233. dataType: "script",
  1234. success: function( data, status ) {
  1235. assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (POST, no callback)" );
  1236. assert.strictEqual( status, "success", "Script results returned (POST, no callback)" );
  1237. }
  1238. };
  1239. } );
  1240. ajaxTest( "jQuery.ajax() - script, Remote with scheme-less URL", 2, function( assert ) {
  1241. return {
  1242. setup: function() {
  1243. Globals.register( "testBar" );
  1244. },
  1245. url: url( "mock.php?action=testbar" ),
  1246. dataType: "script",
  1247. success: function() {
  1248. assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
  1249. }
  1250. };
  1251. } );
  1252. ajaxTest( "jQuery.ajax() - malformed JSON", 2, function( assert ) {
  1253. return {
  1254. url: baseURL + "badjson.js",
  1255. dataType: "json",
  1256. error: function( xhr, msg, detailedMsg ) {
  1257. assert.strictEqual( msg, "parsererror", "A parse error occurred." );
  1258. assert.ok( /(invalid|error|exception)/i.test( detailedMsg ), "Detailed parsererror message provided" );
  1259. }
  1260. };
  1261. } );
  1262. ajaxTest( "jQuery.ajax() - script by content-type", 2, function() {
  1263. return [
  1264. {
  1265. url: baseURL + "mock.php?action=script",
  1266. data: {
  1267. "header": "script"
  1268. },
  1269. success: true
  1270. },
  1271. {
  1272. url: baseURL + "mock.php?action=script",
  1273. data: {
  1274. "header": "ecma"
  1275. },
  1276. success: true
  1277. }
  1278. ];
  1279. } );
  1280. ajaxTest( "jQuery.ajax() - JSON by content-type", 5, function( assert ) {
  1281. return {
  1282. url: baseURL + "mock.php?action=json",
  1283. data: {
  1284. "header": "json",
  1285. "array": "1"
  1286. },
  1287. success: function( json ) {
  1288. assert.ok( json.length >= 2, "Check length" );
  1289. assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
  1290. assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
  1291. assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
  1292. assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
  1293. }
  1294. };
  1295. } );
  1296. ajaxTest( "jQuery.ajax() - JSON by content-type disabled with options", 6, function( assert ) {
  1297. return {
  1298. url: url( "mock.php?action=json" ),
  1299. data: {
  1300. "header": "json",
  1301. "array": "1"
  1302. },
  1303. contents: {
  1304. "json": false
  1305. },
  1306. success: function( text ) {
  1307. assert.strictEqual( typeof text, "string", "json wasn't auto-determined" );
  1308. var json = JSON.parse( text );
  1309. assert.ok( json.length >= 2, "Check length" );
  1310. assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
  1311. assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
  1312. assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
  1313. assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
  1314. }
  1315. };
  1316. } );
  1317. ajaxTest( "jQuery.ajax() - simple get", 1, function( assert ) {
  1318. return {
  1319. type: "GET",
  1320. url: url( "mock.php?action=name&name=foo" ),
  1321. success: function( msg ) {
  1322. assert.strictEqual( msg, "bar", "Check for GET" );
  1323. }
  1324. };
  1325. } );
  1326. ajaxTest( "jQuery.ajax() - simple post", 1, function( assert ) {
  1327. return {
  1328. type: "POST",
  1329. url: url( "mock.php?action=name" ),
  1330. data: "name=peter",
  1331. success: function( msg ) {
  1332. assert.strictEqual( msg, "pan", "Check for POST" );
  1333. }
  1334. };
  1335. } );
  1336. ajaxTest( "jQuery.ajax() - data option - empty bodies for non-GET requests", 1, function( assert ) {
  1337. return {
  1338. url: baseURL + "mock.php?action=echoData",
  1339. data: undefined,
  1340. type: "post",
  1341. success: function( result ) {
  1342. assert.strictEqual( result, "" );
  1343. }
  1344. };
  1345. } );
  1346. ajaxTest( "jQuery.ajax() - data - x-www-form-urlencoded (gh-2658)", 1, function( assert ) {
  1347. return {
  1348. url: "bogus.html",
  1349. data: { devo: "A Beautiful World" },
  1350. type: "post",
  1351. beforeSend: function( _, s ) {
  1352. assert.strictEqual( s.data, "devo=A+Beautiful+World", "data is '+'-encoded" );
  1353. return false;
  1354. },
  1355. error: true
  1356. };
  1357. } );
  1358. ajaxTest( "jQuery.ajax() - data - text/plain (gh-2658)", 1, function( assert ) {
  1359. return {
  1360. url: "bogus.html",
  1361. data: { devo: "A Beautiful World" },
  1362. type: "post",
  1363. contentType: "text/plain",
  1364. beforeSend: function( _, s ) {
  1365. assert.strictEqual( s.data, "devo=A%20Beautiful%20World", "data is %20-encoded" );
  1366. return false;
  1367. },
  1368. error: true
  1369. };
  1370. } );
  1371. ajaxTest( "jQuery.ajax() - don't escape %20 with contentType override (gh-4119)", 1, function( assert ) {
  1372. return {
  1373. url: "bogus.html",
  1374. contentType: "application/x-www-form-urlencoded",
  1375. headers: { "content-type": "application/json" },
  1376. method: "post",
  1377. dataType: "json",
  1378. data: "{\"val\":\"%20\"}",
  1379. beforeSend: function( _, s ) {
  1380. assert.strictEqual( s.data, "{\"val\":\"%20\"}", "data is not %20-encoded" );
  1381. return false;
  1382. },
  1383. error: true
  1384. };
  1385. } );
  1386. ajaxTest( "jQuery.ajax() - escape %20 with contentType override (gh-4119)", 1, function( assert ) {
  1387. return {
  1388. url: "bogus.html",
  1389. contentType: "application/json",
  1390. headers: { "content-type": "application/x-www-form-urlencoded" },
  1391. method: "post",
  1392. dataType: "json",
  1393. data: "{\"val\":\"%20\"}",
  1394. beforeSend: function( _, s ) {
  1395. assert.strictEqual( s.data, "{\"val\":\"+\"}", "data is %20-encoded" );
  1396. return false;
  1397. },
  1398. error: true
  1399. };
  1400. } );
  1401. ajaxTest( "jQuery.ajax() - override contentType with header (gh-4119)", 1, function( assert ) {
  1402. return {
  1403. url: "bogus.html",
  1404. contentType: "application/json",
  1405. headers: { "content-type": "application/x-www-form-urlencoded" },
  1406. beforeSend: function( _, s ) {
  1407. assert.strictEqual( s.contentType, "application/x-www-form-urlencoded",
  1408. "contentType is overwritten" );
  1409. return false;
  1410. },
  1411. error: true
  1412. };
  1413. } );
  1414. ajaxTest( "jQuery.ajax() - data - no processing POST", 1, function( assert ) {
  1415. return {
  1416. url: "bogus.html",
  1417. data: { devo: "A Beautiful World" },
  1418. type: "post",
  1419. contentType: "x-special-sauce",
  1420. processData: false,
  1421. beforeSend: function( _, s ) {
  1422. assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" );
  1423. return false;
  1424. },
  1425. error: true
  1426. };
  1427. } );
  1428. ajaxTest( "jQuery.ajax() - data - no processing GET", 1, function( assert ) {
  1429. return {
  1430. url: "bogus.html",
  1431. data: { devo: "A Beautiful World" },
  1432. type: "get",
  1433. contentType: "x-something-else",
  1434. processData: false,
  1435. beforeSend: function( _, s ) {
  1436. assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" );
  1437. return false;
  1438. },
  1439. error: true
  1440. };
  1441. } );
  1442. ajaxTest( "jQuery.ajax() - data - process string with GET", 2, function( assert ) {
  1443. return {
  1444. url: "bogus.html",
  1445. data: "a=1&b=2",
  1446. type: "get",
  1447. contentType: "x-something-else",
  1448. processData: false,
  1449. beforeSend: function( _, s ) {
  1450. assert.equal( s.url, "bogus.html?a=1&b=2", "added data to url" );
  1451. assert.equal( s.data, undefined, "removed data from settings" );
  1452. return false;
  1453. },
  1454. error: true
  1455. };
  1456. } );
  1457. var ifModifiedNow = new Date();
  1458. jQuery.each(
  1459. /* jQuery.each arguments start */
  1460. {
  1461. " (cache)": true,
  1462. " (no cache)": false
  1463. },
  1464. function( label, cache ) {
  1465. jQuery.each(
  1466. {
  1467. "If-Modified-Since": "mock.php?action=ims",
  1468. "Etag": "mock.php?action=etag"
  1469. },
  1470. function( type, url ) {
  1471. url = baseURL + url + "&ts=" + ifModifiedNow++;
  1472. QUnit.test( "jQuery.ajax() - " + type + " support" + label, function( assert ) {
  1473. assert.expect( 4 );
  1474. var done = assert.async();
  1475. jQuery.ajax( {
  1476. url: url,
  1477. ifModified: true,
  1478. cache: cache,
  1479. success: function( _, status ) {
  1480. assert.strictEqual( status, "success", "Initial status is 'success'" );
  1481. jQuery.ajax( {
  1482. url: url,
  1483. ifModified: true,
  1484. cache: cache,
  1485. success: function( data, status, jqXHR ) {
  1486. assert.strictEqual( status, "notmodified", "Following status is 'notmodified'" );
  1487. assert.strictEqual( jqXHR.status, 304, "XHR status is 304" );
  1488. assert.equal( data, null, "no response body is given" );
  1489. },
  1490. complete: function() {
  1491. done();
  1492. }
  1493. } );
  1494. }
  1495. } );
  1496. } );
  1497. }
  1498. );
  1499. }
  1500. /* jQuery.each arguments end */
  1501. );
  1502. ajaxTest( "jQuery.ajax() - failing cross-domain (non-existing)", 1, function( assert ) {
  1503. return {
  1504. // see RFC 2606
  1505. url: "http://example.invalid",
  1506. error: function( xhr, _, e ) {
  1507. assert.ok( true, "file not found: " + xhr.status + " => " + e );
  1508. }
  1509. };
  1510. } );
  1511. ajaxTest( "jQuery.ajax() - failing cross-domain", 1, function( assert ) {
  1512. return {
  1513. url: "http://" + externalHost,
  1514. error: function( xhr, _, e ) {
  1515. assert.ok( true, "access denied: " + xhr.status + " => " + e );
  1516. }
  1517. };
  1518. } );
  1519. ajaxTest( "jQuery.ajax() - atom+xml", 1, function( assert ) {
  1520. return {
  1521. url: url( "mock.php?action=atom" ),
  1522. success: function() {
  1523. assert.ok( true, "success" );
  1524. }
  1525. };
  1526. } );
  1527. QUnit.test( "jQuery.ajax() - statusText", function( assert ) {
  1528. assert.expect( 3 );
  1529. var done = assert.async();
  1530. jQuery.ajax( url( "mock.php?action=status&code=200&text=Hello" ) ).done( function( _, statusText, jqXHR ) {
  1531. assert.strictEqual( statusText, "success", "callback status text ok for success" );
  1532. assert.ok( jqXHR.statusText === "Hello" || jqXHR.statusText === "OK", "jqXHR status text ok for success (" + jqXHR.statusText + ")" );
  1533. jQuery.ajax( url( "mock.php?action=status&code=404&text=World" ) ).fail( function( jqXHR, statusText ) {
  1534. assert.strictEqual( statusText, "error", "callback status text ok for error" );
  1535. done();
  1536. } );
  1537. } );
  1538. } );
  1539. QUnit.test( "jQuery.ajax() - statusCode", function( assert ) {
  1540. assert.expect( 20 );
  1541. var done = assert.async(),
  1542. count = 12;
  1543. function countComplete() {
  1544. if ( !--count ) {
  1545. done();
  1546. }
  1547. }
  1548. function createStatusCodes( name, isSuccess ) {
  1549. name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
  1550. return {
  1551. 200: function() {
  1552. assert.ok( isSuccess, name );
  1553. },
  1554. 404: function() {
  1555. assert.ok( !isSuccess, name );
  1556. }
  1557. };
  1558. }
  1559. jQuery.each(
  1560. /* jQuery.each arguments start */
  1561. {
  1562. "name.html": true,
  1563. "404.txt": false
  1564. },
  1565. function( uri, isSuccess ) {
  1566. jQuery.ajax( url( uri ), {
  1567. statusCode: createStatusCodes( "in options", isSuccess ),
  1568. complete: countComplete
  1569. } );
  1570. jQuery.ajax( url( uri ), {
  1571. complete: countComplete
  1572. } ).statusCode( createStatusCodes( "immediately with method", isSuccess ) );
  1573. jQuery.ajax( url( uri ), {
  1574. complete: function( jqXHR ) {
  1575. jqXHR.statusCode( createStatusCodes( "on complete", isSuccess ) );
  1576. countComplete();
  1577. }
  1578. } );
  1579. jQuery.ajax( url( uri ), {
  1580. complete: function( jqXHR ) {
  1581. setTimeout( function() {
  1582. jqXHR.statusCode( createStatusCodes( "very late binding", isSuccess ) );
  1583. countComplete();
  1584. }, 100 );
  1585. }
  1586. } );
  1587. jQuery.ajax( url( uri ), {
  1588. statusCode: createStatusCodes( "all (options)", isSuccess ),
  1589. complete: function( jqXHR ) {
  1590. jqXHR.statusCode( createStatusCodes( "all (on complete)", isSuccess ) );
  1591. setTimeout( function() {
  1592. jqXHR.statusCode( createStatusCodes( "all (very late binding)", isSuccess ) );
  1593. countComplete();
  1594. }, 100 );
  1595. }
  1596. } ).statusCode( createStatusCodes( "all (immediately with method)", isSuccess ) );
  1597. var testString = "";
  1598. jQuery.ajax( url( uri ), {
  1599. success: function( a, b, jqXHR ) {
  1600. assert.ok( isSuccess, "success" );
  1601. var statusCode = {};
  1602. statusCode[ jqXHR.status ] = function() {
  1603. testString += "B";
  1604. };
  1605. jqXHR.statusCode( statusCode );
  1606. testString += "A";
  1607. },
  1608. error: function( jqXHR ) {
  1609. assert.ok( !isSuccess, "error" );
  1610. var statusCode = {};
  1611. statusCode[ jqXHR.status ] = function() {
  1612. testString += "B";
  1613. };
  1614. jqXHR.statusCode( statusCode );
  1615. testString += "A";
  1616. },
  1617. complete: function() {
  1618. assert.strictEqual(
  1619. testString,
  1620. "AB",
  1621. "Test statusCode callbacks are ordered like " + ( isSuccess ? "success" : "error" ) + " callbacks"
  1622. );
  1623. countComplete();
  1624. }
  1625. } );
  1626. }
  1627. /* jQuery.each arguments end*/
  1628. );
  1629. } );
  1630. ajaxTest( "jQuery.ajax() - transitive conversions", 8, function( assert ) {
  1631. return [
  1632. {
  1633. url: url( "mock.php?action=json" ),
  1634. converters: {
  1635. "json myJson": function( data ) {
  1636. assert.ok( true, "converter called" );
  1637. return data;
  1638. }
  1639. },
  1640. dataType: "myJson",
  1641. success: function() {
  1642. assert.ok( true, "Transitive conversion worked" );
  1643. assert.strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text" );
  1644. assert.strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType" );
  1645. }
  1646. },
  1647. {
  1648. url: url( "mock.php?action=json" ),
  1649. converters: {
  1650. "json myJson": function( data ) {
  1651. assert.ok( true, "converter called (*)" );
  1652. return data;
  1653. }
  1654. },
  1655. contents: false, /* headers are wrong so we ignore them */
  1656. dataType: "* myJson",
  1657. success: function() {
  1658. assert.ok( true, "Transitive conversion worked (*)" );
  1659. assert.strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text (*)" );
  1660. assert.strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType (*)" );
  1661. }
  1662. }
  1663. ];
  1664. } );
  1665. ajaxTest( "jQuery.ajax() - overrideMimeType", 2, function( assert ) {
  1666. return [
  1667. {
  1668. url: url( "mock.php?action=json" ),
  1669. beforeSend: function( xhr ) {
  1670. xhr.overrideMimeType( "application/json" );
  1671. },
  1672. success: function( json ) {
  1673. assert.ok( json.data, "Mimetype overridden using beforeSend" );
  1674. }
  1675. },
  1676. {
  1677. url: url( "mock.php?action=json" ),
  1678. mimeType: "application/json",
  1679. success: function( json ) {
  1680. assert.ok( json.data, "Mimetype overridden using mimeType option" );
  1681. }
  1682. }
  1683. ];
  1684. } );
  1685. ajaxTest( "jQuery.ajax() - empty json gets to error callback instead of success callback.", 1, function( assert ) {
  1686. return {
  1687. url: url( "mock.php?action=echoData" ),
  1688. error: function( _, __, error ) {
  1689. assert.equal( typeof error === "object", true, "Didn't get back error object for empty json response" );
  1690. },
  1691. dataType: "json"
  1692. };
  1693. } );
  1694. ajaxTest( "#2688 - jQuery.ajax() - beforeSend, cancel request", 2, function( assert ) {
  1695. return {
  1696. create: function() {
  1697. return jQuery.ajax( {
  1698. url: url( "name.html" ),
  1699. beforeSend: function() {
  1700. assert.ok( true, "beforeSend got called, canceling" );
  1701. return false;
  1702. },
  1703. success: function() {
  1704. assert.ok( false, "request didn't get canceled" );
  1705. },
  1706. complete: function() {
  1707. assert.ok( false, "request didn't get canceled" );
  1708. },
  1709. error: function() {
  1710. assert.ok( false, "request didn't get canceled" );
  1711. }
  1712. } );
  1713. },
  1714. fail: function( _, reason ) {
  1715. assert.strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
  1716. }
  1717. };
  1718. } );
  1719. ajaxTest( "#2806 - jQuery.ajax() - data option - evaluate function values", 1, function( assert ) {
  1720. return {
  1721. url: baseURL + "mock.php?action=echoQuery",
  1722. data: {
  1723. key: function() {
  1724. return "value";
  1725. }
  1726. },
  1727. success: function( result ) {
  1728. assert.strictEqual( result, "action=echoQuery&key=value" );
  1729. }
  1730. };
  1731. } );
  1732. QUnit.test( "#7531 - jQuery.ajax() - Location object as url", function( assert ) {
  1733. assert.expect( 1 );
  1734. var xhr,
  1735. success = false;
  1736. try {
  1737. xhr = jQuery.ajax( {
  1738. url: window.location
  1739. } );
  1740. success = true;
  1741. xhr.abort();
  1742. } catch ( e ) {
  1743. }
  1744. assert.ok( success, "document.location did not generate exception" );
  1745. } );
  1746. jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
  1747. ajaxTest( "#7578 - jQuery.ajax() - JSONP - default for cache option" + label, 1, function( assert ) {
  1748. return {
  1749. url: baseURL + "mock.php?action=jsonp",
  1750. dataType: "jsonp",
  1751. crossDomain: crossDomain,
  1752. beforeSend: function() {
  1753. assert.strictEqual( this.cache, false, "cache must be false on JSON request" );
  1754. return false;
  1755. },
  1756. error: true
  1757. };
  1758. } );
  1759. } );
  1760. ajaxTest( "#8107 - jQuery.ajax() - multiple method signatures introduced in 1.5", 4, function( assert ) {
  1761. return [
  1762. {
  1763. create: function() {
  1764. return jQuery.ajax();
  1765. },
  1766. done: function() {
  1767. assert.ok( true, "With no arguments" );
  1768. }
  1769. },
  1770. {
  1771. create: function() {
  1772. return jQuery.ajax( baseURL + "name.html" );
  1773. },
  1774. done: function() {
  1775. assert.ok( true, "With only string URL argument" );
  1776. }
  1777. },
  1778. {
  1779. create: function() {
  1780. return jQuery.ajax( baseURL + "name.html", {} );
  1781. },
  1782. done: function() {
  1783. assert.ok( true, "With string URL param and map" );
  1784. }
  1785. },
  1786. {
  1787. create: function( options ) {
  1788. return jQuery.ajax( options );
  1789. },
  1790. url: baseURL + "name.html",
  1791. success: function() {
  1792. assert.ok( true, "With only map" );
  1793. }
  1794. }
  1795. ];
  1796. } );
  1797. jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
  1798. ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 4, function( assert ) {
  1799. return {
  1800. url: baseURL + "mock.php?action=jsonp",
  1801. dataType: "jsonp",
  1802. crossDomain: crossDomain,
  1803. beforeSend: function( jqXHR, s ) {
  1804. s.callback = s.jsonpCallback;
  1805. assert.ok( this.callback in window, "JSONP callback name is in the window" );
  1806. },
  1807. success: function() {
  1808. var previous = this;
  1809. assert.strictEqual(
  1810. previous.jsonpCallback,
  1811. undefined,
  1812. "jsonpCallback option is set back to default in callbacks"
  1813. );
  1814. assert.ok(
  1815. !( this.callback in window ),
  1816. "JSONP callback name was removed from the window"
  1817. );
  1818. jQuery.ajax( {
  1819. url: baseURL + "mock.php?action=jsonp",
  1820. dataType: "jsonp",
  1821. crossDomain: crossDomain,
  1822. beforeSend: function() {
  1823. assert.strictEqual( this.jsonpCallback, previous.callback, "JSONP callback name is re-used" );
  1824. return false;
  1825. }
  1826. } );
  1827. }
  1828. };
  1829. } );
  1830. } );
  1831. QUnit.test( "#9887 - jQuery.ajax() - Context with circular references (#9887)", function( assert ) {
  1832. assert.expect( 2 );
  1833. var success = false,
  1834. context = {};
  1835. context.field = context;
  1836. try {
  1837. jQuery.ajax( "non-existing", {
  1838. context: context,
  1839. beforeSend: function() {
  1840. assert.ok( this === context, "context was not deep extended" );
  1841. return false;
  1842. }
  1843. } );
  1844. success = true;
  1845. } catch ( e ) {
  1846. console.log( e );
  1847. }
  1848. assert.ok( success, "context with circular reference did not generate an exception" );
  1849. } );
  1850. jQuery.each( [ "as argument", "in settings object" ], function( inSetting, title ) {
  1851. function request( assert, url, test ) {
  1852. return {
  1853. create: function() {
  1854. return jQuery.ajax( inSetting ? { url: url } : url );
  1855. },
  1856. done: function() {
  1857. assert.ok( true, ( test || url ) + " " + title );
  1858. }
  1859. };
  1860. }
  1861. ajaxTest( "#10093 - jQuery.ajax() - falsy url " + title, 4, function( assert ) {
  1862. return [
  1863. request( assert, "", "empty string" ),
  1864. request( assert, false ),
  1865. request( assert, null ),
  1866. request( assert, undefined )
  1867. ];
  1868. } );
  1869. } );
  1870. ajaxTest( "#11151 - jQuery.ajax() - parse error body", 2, function( assert ) {
  1871. return {
  1872. url: url( "mock.php?action=error&json=1" ),
  1873. dataFilter: function( string ) {
  1874. assert.ok( false, "dataFilter called" );
  1875. return string;
  1876. },
  1877. error: function( jqXHR ) {
  1878. assert.strictEqual( jqXHR.responseText, "{ \"code\": 40, \"message\": \"Bad Request\" }", "Error body properly set" );
  1879. assert.deepEqual( jqXHR.responseJSON, { code: 40, message: "Bad Request" }, "Error body properly parsed" );
  1880. }
  1881. };
  1882. } );
  1883. ajaxTest( "#11426 - jQuery.ajax() - loading binary data shouldn't throw an exception in IE", 1, function( assert ) {
  1884. return {
  1885. url: url( "1x1.jpg" ),
  1886. success: function( data ) {
  1887. assert.ok( data === undefined || /JFIF/.test( data ), "success callback reached" );
  1888. }
  1889. };
  1890. } );
  1891. if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().responseType !== "string" ) {
  1892. QUnit.skip( "No ArrayBuffer support in XHR", jQuery.noop );
  1893. } else {
  1894. // No built-in support for binary data, but it's easy to add via a prefilter
  1895. jQuery.ajaxPrefilter( "arraybuffer", function( s ) {
  1896. s.xhrFields = { responseType: "arraybuffer" };
  1897. s.responseFields.arraybuffer = "response";
  1898. s.converters[ "binary arraybuffer" ] = true;
  1899. } );
  1900. ajaxTest( "gh-2498 - jQuery.ajax() - binary data shouldn't throw an exception", 2, function( assert ) {
  1901. return {
  1902. url: url( "1x1.jpg" ),
  1903. dataType: "arraybuffer",
  1904. success: function( data, s, jqxhr ) {
  1905. assert.ok( data instanceof window.ArrayBuffer, "correct data type" );
  1906. assert.ok( jqxhr.response instanceof window.ArrayBuffer, "data in jQXHR" );
  1907. }
  1908. };
  1909. } );
  1910. }
  1911. QUnit.test( "#11743 - jQuery.ajax() - script, throws exception", function( assert ) {
  1912. assert.expect( 1 );
  1913. var done = assert.async();
  1914. var onerror = window.onerror;
  1915. window.onerror = function() {
  1916. assert.ok( true, "Exception thrown" );
  1917. window.onerror = onerror;
  1918. done();
  1919. };
  1920. jQuery.ajax( {
  1921. url: baseURL + "badjson.js",
  1922. dataType: "script",
  1923. throws: true
  1924. } );
  1925. } );
  1926. jQuery.each( [ "method", "type" ], function( _, globalOption ) {
  1927. function request( assert, option ) {
  1928. var options = {
  1929. url: url( "mock.php?action=echoData" ),
  1930. data: "hello",
  1931. success: function( msg ) {
  1932. assert.strictEqual( msg, "hello", "Check for POST (no override)" );
  1933. }
  1934. };
  1935. if ( option ) {
  1936. options[ option ] = "GET";
  1937. options.success = function( msg ) {
  1938. assert.strictEqual( msg, "", "Check for no POST (overriding with " + option + ")" );
  1939. };
  1940. }
  1941. return options;
  1942. }
  1943. ajaxTest(
  1944. "#12004 - jQuery.ajax() - method is an alias of type - " +
  1945. globalOption + " set globally", 3,
  1946. function( assert ) {
  1947. return {
  1948. setup: function() {
  1949. var options = {};
  1950. options[ globalOption ] = "POST";
  1951. jQuery.ajaxSetup( options );
  1952. },
  1953. requests: [
  1954. request( assert, "type" ),
  1955. request( assert, "method" ),
  1956. request( assert )
  1957. ]
  1958. };
  1959. }
  1960. );
  1961. } );
  1962. ajaxTest( "#13276 - jQuery.ajax() - compatibility between XML documents from ajax requests and parsed string", 1, function( assert ) {
  1963. return {
  1964. url: baseURL + "dashboard.xml",
  1965. dataType: "xml",
  1966. success: function( ajaxXML ) {
  1967. var parsedXML = jQuery( jQuery.parseXML( "<tab title=\"Added\">blibli</tab>" ) ).find( "tab" );
  1968. ajaxXML = jQuery( ajaxXML );
  1969. try {
  1970. ajaxXML.find( "infowindowtab" ).append( parsedXML );
  1971. } catch ( e ) {
  1972. assert.strictEqual( e, undefined, "error" );
  1973. return;
  1974. }
  1975. assert.strictEqual( ajaxXML.find( "tab" ).length, 3, "Parsed node was added properly" );
  1976. }
  1977. };
  1978. } );
  1979. ajaxTest( "#13292 - jQuery.ajax() - converter is bypassed for 204 requests", 3, function( assert ) {
  1980. return {
  1981. url: baseURL + "mock.php?action=status&code=204&text=No+Content",
  1982. dataType: "testing",
  1983. converters: {
  1984. "* testing": function() {
  1985. throw "converter was called";
  1986. }
  1987. },
  1988. success: function( data, status, jqXHR ) {
  1989. assert.strictEqual( jqXHR.status, 204, "status code is 204" );
  1990. assert.strictEqual( status, "nocontent", "status text is 'nocontent'" );
  1991. assert.strictEqual( data, undefined, "data is undefined" );
  1992. },
  1993. error: function( _, status, error ) {
  1994. assert.ok( false, "error" );
  1995. assert.strictEqual( status, "parsererror", "Parser Error" );
  1996. assert.strictEqual( error, "converter was called", "Converter was called" );
  1997. }
  1998. };
  1999. } );
  2000. ajaxTest( "#13388 - jQuery.ajax() - responseXML", 3, function( assert ) {
  2001. return {
  2002. url: url( "with_fries.xml" ),
  2003. dataType: "xml",
  2004. success: function( resp, _, jqXHR ) {
  2005. assert.notStrictEqual( resp, undefined, "XML document exists" );
  2006. assert.ok( "responseXML" in jqXHR, "jqXHR.responseXML exists" );
  2007. assert.strictEqual( resp, jqXHR.responseXML, "jqXHR.responseXML is set correctly" );
  2008. }
  2009. };
  2010. } );
  2011. ajaxTest( "#13922 - jQuery.ajax() - converter is bypassed for HEAD requests", 3, function( assert ) {
  2012. return {
  2013. url: baseURL + "mock.php?action=json",
  2014. method: "HEAD",
  2015. data: {
  2016. header: "yes"
  2017. },
  2018. converters: {
  2019. "text json": function() {
  2020. throw "converter was called";
  2021. }
  2022. },
  2023. success: function( data, status ) {
  2024. assert.ok( true, "success" );
  2025. assert.strictEqual( status, "nocontent", "data is undefined" );
  2026. assert.strictEqual( data, undefined, "data is undefined" );
  2027. },
  2028. error: function( _, status, error ) {
  2029. assert.ok( false, "error" );
  2030. assert.strictEqual( status, "parsererror", "Parser Error" );
  2031. assert.strictEqual( error, "converter was called", "Converter was called" );
  2032. }
  2033. };
  2034. } );
  2035. // Chrome 78 dropped support for synchronous XHR requests inside of
  2036. // beforeunload, unload, pagehide, and visibilitychange event handlers.
  2037. // See https://bugs.chromium.org/p/chromium/issues/detail?id=952452
  2038. // Safari 13 did similar changes. The below check will catch them both.
  2039. // Edge Legacy fakes Chrome which fakes Safari in their user agents so we need
  2040. // to exclude Edge specifically here so that the test continues to run there.
  2041. if ( !/safari/i.test( navigator.userAgent ) || /edge\//i.test( navigator.userAgent ) ) {
  2042. testIframe(
  2043. "#14379 - jQuery.ajax() on unload",
  2044. "ajax/onunload.html",
  2045. function( assert, jQuery, window, document, status ) {
  2046. assert.expect( 1 );
  2047. assert.strictEqual( status, "success", "Request completed" );
  2048. }
  2049. );
  2050. }
  2051. ajaxTest( "#14683 - jQuery.ajax() - Exceptions thrown synchronously by xhr.send should be caught", 4, function( assert ) {
  2052. return [ {
  2053. url: baseURL + "mock.php?action=echoData",
  2054. method: "POST",
  2055. data: {
  2056. toString: function() {
  2057. throw "Can't parse";
  2058. }
  2059. },
  2060. processData: false,
  2061. done: function( data ) {
  2062. assert.ok( false, "done: " + data );
  2063. },
  2064. fail: function( jqXHR, status, error ) {
  2065. assert.ok( true, "exception caught: " + error );
  2066. assert.strictEqual( jqXHR.status, 0, "proper status code" );
  2067. assert.strictEqual( status, "error", "proper status" );
  2068. }
  2069. }, {
  2070. url: "http://" + externalHost + ":80q",
  2071. done: function( data ) {
  2072. assert.ok( false, "done: " + data );
  2073. },
  2074. fail: function( _, status, error ) {
  2075. assert.ok( true, "fail: " + status + " - " + error );
  2076. }
  2077. } ];
  2078. } );
  2079. ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
  2080. return {
  2081. url: url( "mock.php?action=contentType" ),
  2082. data: {
  2083. contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  2084. "response": "<test/>"
  2085. },
  2086. success: function( result ) {
  2087. assert.strictEqual(
  2088. typeof result,
  2089. "string",
  2090. "Should handle it as a string, not xml"
  2091. );
  2092. }
  2093. };
  2094. } );
  2095. ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
  2096. return {
  2097. url: url( "mock.php?action=contentType" ),
  2098. data: {
  2099. contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  2100. "response": "<test/>"
  2101. },
  2102. success: function( result ) {
  2103. assert.strictEqual(
  2104. typeof result,
  2105. "string",
  2106. "Should handle it as a string, not xml"
  2107. );
  2108. }
  2109. };
  2110. } );
  2111. ajaxTest( "gh-2587 - when content-type not json, but looks like one", 1, function( assert ) {
  2112. return {
  2113. url: url( "mock.php?action=contentType" ),
  2114. data: {
  2115. contentType: "test/jsontest",
  2116. "response": JSON.stringify( { test: "test" } )
  2117. },
  2118. success: function( result ) {
  2119. assert.strictEqual(
  2120. typeof result,
  2121. "string",
  2122. "Should handle it as a string, not json"
  2123. );
  2124. }
  2125. };
  2126. } );
  2127. ajaxTest( "gh-2587 - when content-type not html, but looks like one", 1, function( assert ) {
  2128. return {
  2129. url: url( "mock.php?action=contentType" ),
  2130. data: {
  2131. contentType: "test/htmltest",
  2132. "response": "<p>test</p>"
  2133. },
  2134. success: function( result ) {
  2135. assert.strictEqual(
  2136. typeof result,
  2137. "string",
  2138. "Should handle it as a string, not html"
  2139. );
  2140. }
  2141. };
  2142. } );
  2143. ajaxTest( "gh-2587 - when content-type not javascript, but looks like one", 1, function( assert ) {
  2144. return {
  2145. url: url( "mock.php?action=contentType" ),
  2146. data: {
  2147. contentType: "test/testjavascript",
  2148. "response": "alert(1)"
  2149. },
  2150. success: function( result ) {
  2151. assert.strictEqual(
  2152. typeof result,
  2153. "string",
  2154. "Should handle it as a string, not javascript"
  2155. );
  2156. }
  2157. };
  2158. } );
  2159. ajaxTest( "gh-2587 - when content-type not ecmascript, but looks like one", 1, function( assert ) {
  2160. return {
  2161. url: url( "mock.php?action=contentType" ),
  2162. data: {
  2163. contentType: "test/testjavascript",
  2164. "response": "alert(1)"
  2165. },
  2166. success: function( result ) {
  2167. assert.strictEqual(
  2168. typeof result,
  2169. "string",
  2170. "Should handle it as a string, not ecmascript"
  2171. );
  2172. }
  2173. };
  2174. } );
  2175. //----------- jQuery.ajaxPrefilter()
  2176. ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, function( assert ) {
  2177. return {
  2178. dataType: "prefix",
  2179. setup: function() {
  2180. // Ensure prefix does not throw an error
  2181. jQuery.ajaxPrefilter( "+prefix", function( options, _, jqXHR ) {
  2182. if ( options.abortInPrefilter ) {
  2183. jqXHR.abort();
  2184. }
  2185. } );
  2186. },
  2187. abortInPrefilter: true,
  2188. error: function() {
  2189. assert.ok( false, "error callback called" );
  2190. },
  2191. fail: function( _, reason ) {
  2192. assert.strictEqual( reason, "canceled", "Request aborted by the prefilter must fail with 'canceled' status text" );
  2193. }
  2194. };
  2195. } );
  2196. //----------- jQuery.ajaxSetup()
  2197. QUnit.test( "jQuery.ajaxSetup()", function( assert ) {
  2198. assert.expect( 1 );
  2199. var done = assert.async();
  2200. jQuery.ajaxSetup( {
  2201. url: url( "mock.php?action=name&name=foo" ),
  2202. success: function( msg ) {
  2203. assert.strictEqual( msg, "bar", "Check for GET" );
  2204. done();
  2205. }
  2206. } );
  2207. jQuery.ajax();
  2208. } );
  2209. QUnit.test( "jQuery.ajaxSetup({ timeout: Number }) - with global timeout", function( assert ) {
  2210. assert.expect( 2 );
  2211. var done = assert.async();
  2212. var passed = 0,
  2213. pass = function() {
  2214. assert.ok( passed++ < 2, "Error callback executed" );
  2215. if ( passed === 2 ) {
  2216. jQuery( document ).off( "ajaxError.setupTest" );
  2217. done();
  2218. }
  2219. },
  2220. fail = function( a, b ) {
  2221. assert.ok( false, "Check for timeout failed " + a + " " + b );
  2222. done();
  2223. };
  2224. jQuery( document ).on( "ajaxError.setupTest", pass );
  2225. jQuery.ajaxSetup( {
  2226. timeout: 1000
  2227. } );
  2228. jQuery.ajax( {
  2229. type: "GET",
  2230. url: url( "mock.php?action=wait&wait=5" ),
  2231. error: pass,
  2232. success: fail
  2233. } );
  2234. } );
  2235. QUnit.test( "jQuery.ajaxSetup({ timeout: Number }) with localtimeout", function( assert ) {
  2236. assert.expect( 1 );
  2237. var done = assert.async();
  2238. jQuery.ajaxSetup( {
  2239. timeout: 50
  2240. } );
  2241. jQuery.ajax( {
  2242. type: "GET",
  2243. timeout: 15000,
  2244. url: url( "mock.php?action=wait&wait=1" ),
  2245. error: function() {
  2246. assert.ok( false, "Check for local timeout failed" );
  2247. done();
  2248. },
  2249. success: function() {
  2250. assert.ok( true, "Check for local timeout" );
  2251. done();
  2252. }
  2253. } );
  2254. } );
  2255. //----------- jQuery.domManip()
  2256. QUnit.test( "#11264 - jQuery.domManip() - no side effect because of ajaxSetup or global events", function( assert ) {
  2257. assert.expect( 1 );
  2258. jQuery.ajaxSetup( {
  2259. type: "POST"
  2260. } );
  2261. jQuery( document ).on( "ajaxStart ajaxStop", function() {
  2262. assert.ok( false, "Global event triggered" );
  2263. } );
  2264. jQuery( "#qunit-fixture" ).append( "<script src='" + baseURL + "mock.php?action=script'></script>" );
  2265. jQuery( document ).off( "ajaxStart ajaxStop" );
  2266. } );
  2267. QUnit.test(
  2268. "jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)",
  2269. function( assert ) {
  2270. assert.expect( 1 );
  2271. var done = assert.async();
  2272. jQuery.ajaxSetup( {
  2273. type: "POST"
  2274. } );
  2275. jQuery( "#qunit-fixture" ).load( baseURL + "mock.php?action=echoMethod", function( method ) {
  2276. assert.equal( method, "GET" );
  2277. done();
  2278. } );
  2279. }
  2280. );
  2281. QUnit.test(
  2282. "jQuery#load() - should resolve with correct context",
  2283. function( assert ) {
  2284. assert.expect( 2 );
  2285. var done = assert.async();
  2286. var ps = jQuery( "<p></p><p></p>" );
  2287. var i = 0;
  2288. ps.appendTo( "#qunit-fixture" );
  2289. ps.load( baseURL + "mock.php?action=echoMethod", function() {
  2290. assert.strictEqual( this, ps[ i++ ] );
  2291. if ( i === 2 ) {
  2292. done();
  2293. }
  2294. } );
  2295. }
  2296. );
  2297. QUnit.test(
  2298. "#11402 - jQuery.domManip() - script in comments are properly evaluated",
  2299. function( assert ) {
  2300. assert.expect( 2 );
  2301. jQuery( "#qunit-fixture" ).load( baseURL + "cleanScript.html", assert.async() );
  2302. }
  2303. );
  2304. //----------- jQuery.get()
  2305. QUnit.test( "jQuery.get( String, Hash, Function ) - parse xml and use text() on nodes", function( assert ) {
  2306. assert.expect( 2 );
  2307. var done = assert.async();
  2308. jQuery.get( url( "dashboard.xml" ), function( xml ) {
  2309. var content = [];
  2310. jQuery( "tab", xml ).each( function() {
  2311. content.push( jQuery( this ).text() );
  2312. } );
  2313. assert.strictEqual( content[ 0 ], "blabla", "Check first tab" );
  2314. assert.strictEqual( content[ 1 ], "blublu", "Check second tab" );
  2315. done();
  2316. } );
  2317. } );
  2318. QUnit.test( "#8277 - jQuery.get( String, Function ) - data in ajaxSettings", function( assert ) {
  2319. assert.expect( 1 );
  2320. var done = assert.async();
  2321. jQuery.ajaxSetup( {
  2322. data: "helloworld"
  2323. } );
  2324. jQuery.get( url( "mock.php?action=echoQuery" ), function( data ) {
  2325. assert.ok( /helloworld$/.test( data ), "Data from ajaxSettings was used" );
  2326. done();
  2327. } );
  2328. } );
  2329. //----------- jQuery.getJSON()
  2330. QUnit.test( "jQuery.getJSON( String, Hash, Function ) - JSON array", function( assert ) {
  2331. assert.expect( 5 );
  2332. var done = assert.async();
  2333. jQuery.getJSON(
  2334. url( "mock.php?action=json" ),
  2335. {
  2336. "array": "1"
  2337. },
  2338. function( json ) {
  2339. assert.ok( json.length >= 2, "Check length" );
  2340. assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
  2341. assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
  2342. assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
  2343. assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
  2344. done();
  2345. }
  2346. );
  2347. } );
  2348. QUnit.test( "jQuery.getJSON( String, Function ) - JSON object", function( assert ) {
  2349. assert.expect( 2 );
  2350. var done = assert.async();
  2351. jQuery.getJSON( url( "mock.php?action=json" ), function( json ) {
  2352. if ( json && json[ "data" ] ) {
  2353. assert.strictEqual( json[ "data" ][ "lang" ], "en", "Check JSON: lang" );
  2354. assert.strictEqual( json[ "data" ].length, 25, "Check JSON: length" );
  2355. done();
  2356. }
  2357. } );
  2358. } );
  2359. QUnit.test( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content", function( assert ) {
  2360. assert.expect( 2 );
  2361. var done = assert.async();
  2362. var absoluteUrl = url( "mock.php?action=json" );
  2363. // Make a relative URL absolute relative to the document location
  2364. if ( !/^[a-z][a-z0-9+.-]*:/i.test( absoluteUrl ) ) {
  2365. // An absolute path replaces everything after the host
  2366. if ( absoluteUrl.charAt( 0 ) === "/" ) {
  2367. absoluteUrl = window.location.href.replace( /(:\/*[^/]*).*$/, "$1" ) + absoluteUrl;
  2368. // A relative path replaces the last slash-separated path segment
  2369. } else {
  2370. absoluteUrl = window.location.href.replace( /[^/]*$/, "" ) + absoluteUrl;
  2371. }
  2372. }
  2373. jQuery.getJSON( absoluteUrl, function( json ) {
  2374. assert.strictEqual( json.data.lang, "en", "Check JSON: lang" );
  2375. assert.strictEqual( json.data.length, 25, "Check JSON: length" );
  2376. done();
  2377. } );
  2378. } );
  2379. //----------- jQuery.getScript()
  2380. QUnit.test( "jQuery.getScript( String, Function ) - with callback",
  2381. function( assert ) {
  2382. assert.expect( 2 );
  2383. var done = assert.async();
  2384. Globals.register( "testBar" );
  2385. jQuery.getScript( url( "mock.php?action=testbar" ), function() {
  2386. assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
  2387. done();
  2388. } );
  2389. }
  2390. );
  2391. QUnit.test( "jQuery.getScript( String, Function ) - no callback", function( assert ) {
  2392. assert.expect( 1 );
  2393. Globals.register( "testBar" );
  2394. jQuery.getScript( url( "mock.php?action=testbar" ) ).done( assert.async() );
  2395. } );
  2396. QUnit.test( "#8082 - jQuery.getScript( String, Function ) - source as responseText", function( assert ) {
  2397. assert.expect( 2 );
  2398. var done = assert.async();
  2399. Globals.register( "testBar" );
  2400. jQuery.getScript( url( "mock.php?action=testbar" ), function( data, _, jqXHR ) {
  2401. assert.strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script" );
  2402. done();
  2403. } );
  2404. } );
  2405. QUnit.test( "jQuery.getScript( Object ) - with callback", function( assert ) {
  2406. assert.expect( 2 );
  2407. var done = assert.async();
  2408. Globals.register( "testBar" );
  2409. jQuery.getScript( {
  2410. url: url( "mock.php?action=testbar" ),
  2411. success: function() {
  2412. assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
  2413. done();
  2414. }
  2415. } );
  2416. } );
  2417. QUnit.test( "jQuery.getScript( Object ) - no callback", function( assert ) {
  2418. assert.expect( 1 );
  2419. Globals.register( "testBar" );
  2420. jQuery.getScript( { url: url( "mock.php?action=testbar" ) } ).done( assert.async() );
  2421. } );
  2422. // //----------- jQuery.fn.load()
  2423. // check if load can be called with only url
  2424. QUnit.test( "jQuery.fn.load( String )", function( assert ) {
  2425. assert.expect( 2 );
  2426. jQuery.ajaxSetup( {
  2427. beforeSend: function() {
  2428. assert.strictEqual( this.type, "GET", "no data means GET request" );
  2429. }
  2430. } );
  2431. jQuery( "#first" ).load( baseURL + "name.html", assert.async() );
  2432. } );
  2433. QUnit.test( "jQuery.fn.load() - 404 error callbacks", function( assert ) {
  2434. assert.expect( 6 );
  2435. var done = assert.async();
  2436. addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError", assert )();
  2437. jQuery( document ).on( "ajaxStop", done );
  2438. jQuery( "<div></div>" ).load( baseURL + "404.txt", function() {
  2439. assert.ok( true, "complete" );
  2440. } );
  2441. } );
  2442. // check if load can be called with url and null data
  2443. QUnit.test( "jQuery.fn.load( String, null )", function( assert ) {
  2444. assert.expect( 2 );
  2445. jQuery.ajaxSetup( {
  2446. beforeSend: function() {
  2447. assert.strictEqual( this.type, "GET", "no data means GET request" );
  2448. }
  2449. } );
  2450. jQuery( "#first" ).load( baseURL + "name.html", null, assert.async() );
  2451. } );
  2452. // check if load can be called with url and undefined data
  2453. QUnit.test( "jQuery.fn.load( String, undefined )", function( assert ) {
  2454. assert.expect( 2 );
  2455. jQuery.ajaxSetup( {
  2456. beforeSend: function() {
  2457. assert.strictEqual( this.type, "GET", "no data means GET request" );
  2458. }
  2459. } );
  2460. jQuery( "#first" ).load( baseURL + "name.html", undefined, assert.async() );
  2461. } );
  2462. // check if load can be called with only url
  2463. QUnit.test( "jQuery.fn.load( URL_SELECTOR )", function( assert ) {
  2464. assert.expect( 1 );
  2465. var done = assert.async();
  2466. jQuery( "#first" ).load( baseURL + "test3.html div.user", function() {
  2467. assert.strictEqual( jQuery( this ).children( "div" ).length, 2, "Verify that specific elements were injected" );
  2468. done();
  2469. } );
  2470. } );
  2471. // Selector should be trimmed to avoid leading spaces (#14773)
  2472. QUnit.test( "jQuery.fn.load( URL_SELECTOR with spaces )", function( assert ) {
  2473. assert.expect( 1 );
  2474. var done = assert.async();
  2475. jQuery( "#first" ).load( baseURL + "test3.html #superuser ", function() {
  2476. assert.strictEqual( jQuery( this ).children( "div" ).length, 1, "Verify that specific elements were injected" );
  2477. done();
  2478. } );
  2479. } );
  2480. // Selector should be trimmed to avoid leading spaces (#14773)
  2481. // Selector should include any valid non-HTML whitespace (#3003)
  2482. QUnit.test( "jQuery.fn.load( URL_SELECTOR with non-HTML whitespace(#3003) )", function( assert ) {
  2483. assert.expect( 1 );
  2484. var done = assert.async();
  2485. jQuery( "#first" ).load( baseURL + "test3.html #whitespace\\\\xA0 ", function() {
  2486. assert.strictEqual( jQuery( this ).children( "div" ).length, 1, "Verify that specific elements were injected" );
  2487. done();
  2488. } );
  2489. } );
  2490. QUnit.test( "jQuery.fn.load( String, Function ) - simple: inject text into DOM", function( assert ) {
  2491. assert.expect( 2 );
  2492. var done = assert.async();
  2493. jQuery( "#first" ).load( url( "name.html" ), function() {
  2494. assert.ok( /^ERROR/.test( jQuery( "#first" ).text() ), "Check if content was injected into the DOM" );
  2495. done();
  2496. } );
  2497. } );
  2498. QUnit.test( "jQuery.fn.load( String, Function ) - check scripts", function( assert ) {
  2499. assert.expect( 7 );
  2500. var done = assert.async();
  2501. var verifyEvaluation = function() {
  2502. assert.strictEqual( window[ "testBar" ], "bar", "Check if script src was evaluated after load" );
  2503. assert.strictEqual( jQuery( "#ap" ).html(), "bar", "Check if script evaluation has modified DOM" );
  2504. done();
  2505. };
  2506. Globals.register( "testFoo" );
  2507. Globals.register( "testBar" );
  2508. jQuery( "#first" ).load( url( "mock.php?action=testHTML&baseURL=" + baseURL ), function() {
  2509. assert.ok( jQuery( "#first" ).html().match( /^html text/ ), "Check content after loading html" );
  2510. assert.strictEqual( jQuery( "#foo" ).html(), "foo", "Check if script evaluation has modified DOM" );
  2511. assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated after load" );
  2512. setTimeout( verifyEvaluation, 600 );
  2513. } );
  2514. } );
  2515. QUnit.test( "jQuery.fn.load( String, Function ) - check file with only a script tag", function( assert ) {
  2516. assert.expect( 3 );
  2517. var done = assert.async();
  2518. Globals.register( "testFoo" );
  2519. jQuery( "#first" ).load( url( "test2.html" ), function() {
  2520. assert.strictEqual( jQuery( "#foo" ).html(), "foo", "Check if script evaluation has modified DOM" );
  2521. assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated after load" );
  2522. done();
  2523. } );
  2524. } );
  2525. QUnit.test( "jQuery.fn.load( String, Function ) - dataFilter in ajaxSettings", function( assert ) {
  2526. assert.expect( 2 );
  2527. var done = assert.async();
  2528. jQuery.ajaxSetup( {
  2529. dataFilter: function() {
  2530. return "Hello World";
  2531. }
  2532. } );
  2533. jQuery( "<div></div>" ).load( url( "name.html" ), function( responseText ) {
  2534. assert.strictEqual( jQuery( this ).html(), "Hello World", "Test div was filled with filtered data" );
  2535. assert.strictEqual( responseText, "Hello World", "Test callback receives filtered data" );
  2536. done();
  2537. } );
  2538. } );
  2539. QUnit.test( "jQuery.fn.load( String, Object, Function )", function( assert ) {
  2540. assert.expect( 2 );
  2541. var done = assert.async();
  2542. jQuery( "<div></div>" ).load( url( "mock.php?action=echoHtml" ), {
  2543. "bar": "ok"
  2544. }, function() {
  2545. var $node = jQuery( this );
  2546. assert.strictEqual( $node.find( "#method" ).text(), "POST", "Check method" );
  2547. assert.strictEqual( $node.find( "#data" ).text(), "bar=ok", "Check if data is passed correctly" );
  2548. done();
  2549. } );
  2550. } );
  2551. QUnit.test( "jQuery.fn.load( String, String, Function )", function( assert ) {
  2552. assert.expect( 2 );
  2553. var done = assert.async();
  2554. jQuery( "<div></div>" ).load( url( "mock.php?action=echoHtml" ), "foo=3&bar=ok", function() {
  2555. var $node = jQuery( this );
  2556. assert.strictEqual( $node.find( "#method" ).text(), "GET", "Check method" );
  2557. assert.ok( $node.find( "#query" ).text().match( /foo=3&bar=ok/ ), "Check if a string of data is passed correctly" );
  2558. done();
  2559. } );
  2560. } );
  2561. QUnit.test( "jQuery.fn.load() - callbacks get the correct parameters", function( assert ) {
  2562. assert.expect( 8 );
  2563. var completeArgs = {},
  2564. done = assert.async();
  2565. jQuery.ajaxSetup( {
  2566. success: function( _, status, jqXHR ) {
  2567. completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
  2568. },
  2569. error: function( jqXHR, status ) {
  2570. completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
  2571. }
  2572. } );
  2573. jQuery.when.apply(
  2574. jQuery,
  2575. jQuery.map( [
  2576. {
  2577. type: "success",
  2578. url: baseURL + "mock.php?action=echoQuery&arg=pop"
  2579. },
  2580. {
  2581. type: "error",
  2582. url: baseURL + "404.txt"
  2583. }
  2584. ],
  2585. function( options ) {
  2586. return jQuery.Deferred( function( defer ) {
  2587. jQuery( "#foo" ).load( options.url, function() {
  2588. var args = arguments;
  2589. assert.strictEqual( completeArgs[ options.url ].length, args.length, "same number of arguments (" + options.type + ")" );
  2590. jQuery.each( completeArgs[ options.url ], function( i, value ) {
  2591. assert.strictEqual( args[ i ], value, "argument #" + i + " is the same (" + options.type + ")" );
  2592. } );
  2593. defer.resolve();
  2594. } );
  2595. } );
  2596. } )
  2597. ).always( done );
  2598. } );
  2599. QUnit.test( "#2046 - jQuery.fn.load( String, Function ) with ajaxSetup on dataType json", function( assert ) {
  2600. assert.expect( 1 );
  2601. var done = assert.async();
  2602. jQuery.ajaxSetup( {
  2603. dataType: "json"
  2604. } );
  2605. jQuery( document ).on( "ajaxComplete", function( e, xml, s ) {
  2606. assert.strictEqual( s.dataType, "html", "Verify the load() dataType was html" );
  2607. jQuery( document ).off( "ajaxComplete" );
  2608. done();
  2609. } );
  2610. jQuery( "#first" ).load( baseURL + "test3.html" );
  2611. } );
  2612. QUnit.test( "#10524 - jQuery.fn.load() - data specified in ajaxSettings is merged in", function( assert ) {
  2613. assert.expect( 1 );
  2614. var done = assert.async();
  2615. var data = {
  2616. "baz": 1
  2617. };
  2618. jQuery.ajaxSetup( {
  2619. data: {
  2620. "foo": "bar"
  2621. }
  2622. } );
  2623. jQuery( "#foo" ).load( baseURL + "mock.php?action=echoQuery", data );
  2624. jQuery( document ).on( "ajaxComplete", function( event, jqXHR, options ) {
  2625. assert.ok( ~options.data.indexOf( "foo=bar" ), "Data from ajaxSettings was used" );
  2626. done();
  2627. } );
  2628. } );
  2629. // //----------- jQuery.post()
  2630. QUnit.test( "jQuery.post() - data", function( assert ) {
  2631. assert.expect( 3 );
  2632. var done = assert.async();
  2633. jQuery.when(
  2634. jQuery.post(
  2635. url( "mock.php?action=xml" ),
  2636. {
  2637. cal: "5-2"
  2638. },
  2639. function( xml ) {
  2640. jQuery( "math", xml ).each( function() {
  2641. assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
  2642. assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
  2643. } );
  2644. }
  2645. ),
  2646. jQuery.ajax( {
  2647. url: url( "mock.php?action=echoData" ),
  2648. type: "POST",
  2649. data: {
  2650. "test": {
  2651. "length": 7,
  2652. "foo": "bar"
  2653. }
  2654. },
  2655. success: function( data ) {
  2656. assert.strictEqual( data, "test%5Blength%5D=7&test%5Bfoo%5D=bar", "Check if a sub-object with a length param is serialized correctly" );
  2657. }
  2658. } )
  2659. ).always( done );
  2660. } );
  2661. QUnit.test( "jQuery.post( String, Hash, Function ) - simple with xml", function( assert ) {
  2662. assert.expect( 4 );
  2663. var done = assert.async();
  2664. jQuery.when(
  2665. jQuery.post(
  2666. url( "mock.php?action=xml" ),
  2667. {
  2668. cal: "5-2"
  2669. },
  2670. function( xml ) {
  2671. jQuery( "math", xml ).each( function() {
  2672. assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
  2673. assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
  2674. } );
  2675. }
  2676. ),
  2677. jQuery.post( url( "mock.php?action=xml&cal=5-2" ), {}, function( xml ) {
  2678. jQuery( "math", xml ).each( function() {
  2679. assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
  2680. assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
  2681. } );
  2682. } )
  2683. ).always( function() {
  2684. done();
  2685. } );
  2686. } );
  2687. QUnit.test( "jQuery[get|post]( options ) - simple with xml", function( assert ) {
  2688. assert.expect( 2 );
  2689. var done = assert.async();
  2690. jQuery.when.apply( jQuery,
  2691. jQuery.map( [ "get", "post" ], function( method ) {
  2692. return jQuery[ method ]( {
  2693. url: url( "mock.php?action=xml" ),
  2694. data: {
  2695. cal: "5-2"
  2696. },
  2697. success: function( xml ) {
  2698. jQuery( "math", xml ).each( function() {
  2699. assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
  2700. } );
  2701. }
  2702. } );
  2703. } )
  2704. ).always( function() {
  2705. done();
  2706. } );
  2707. } );
  2708. //----------- jQuery.active
  2709. QUnit.test( "jQuery.active", function( assert ) {
  2710. assert.expect( 1 );
  2711. assert.ok( jQuery.active === 0, "ajax active counter should be zero: " + jQuery.active );
  2712. } );
  2713. } )();