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