335c2ac4d310348eda37d17a602b9a4d63940c2a
[jquery.git] / test / unit / ajax.js
1 module("ajax", { teardown: moduleTeardown });
2
3 // Safari 3 randomly crashes when running these tests,
4 // but only in the full suite - you can run just the Ajax
5 // tests and they'll pass
6 //if ( !jQuery.browser.safari ) {
7
8 if ( !isLocal ) {
9
10 test("jQuery.ajax() - success callbacks", function() {
11         expect( 8 );
12
13         jQuery.ajaxSetup({ timeout: 0 });
14
15         stop();
16
17         jQuery('#foo').ajaxStart(function(){
18                 ok( true, "ajaxStart" );
19         }).ajaxStop(function(){
20                 ok( true, "ajaxStop" );
21                 start();
22         }).ajaxSend(function(){
23                 ok( true, "ajaxSend" );
24         }).ajaxComplete(function(){
25                 ok( true, "ajaxComplete" );
26         }).ajaxError(function(){
27                 ok( false, "ajaxError" );
28         }).ajaxSuccess(function(){
29                 ok( true, "ajaxSuccess" );
30         });
31
32         jQuery.ajax({
33                 url: url("data/name.html"),
34                 beforeSend: function(){ ok(true, "beforeSend"); },
35                 success: function(){ ok(true, "success"); },
36                 error: function(){ ok(false, "error"); },
37                 complete: function(){ ok(true, "complete"); }
38         });
39 });
40
41 test("jQuery.ajax() - success callbacks - (url, options) syntax", function() {
42         expect( 8 );
43
44         jQuery.ajaxSetup({ timeout: 0 });
45
46         stop();
47
48         setTimeout(function(){
49                 jQuery('#foo').ajaxStart(function(){
50                         ok( true, "ajaxStart" );
51                 }).ajaxStop(function(){
52                         ok( true, "ajaxStop" );
53                         start();
54                 }).ajaxSend(function(){
55                         ok( true, "ajaxSend" );
56                 }).ajaxComplete(function(){
57                         ok( true, "ajaxComplete" );
58                 }).ajaxError(function(){
59                         ok( false, "ajaxError" );
60                 }).ajaxSuccess(function(){
61                         ok( true, "ajaxSuccess" );
62                 });
63
64                 jQuery.ajax( url("data/name.html") , {
65                         beforeSend: function(){ ok(true, "beforeSend"); },
66                         success: function(){ ok(true, "success"); },
67                         error: function(){ ok(false, "error"); },
68                         complete: function(){ ok(true, "complete"); }
69                 });
70         }, 13);
71 });
72
73 test("jQuery.ajax() - success callbacks (late binding)", function() {
74         expect( 8 );
75
76         jQuery.ajaxSetup({ timeout: 0 });
77
78         stop();
79
80         setTimeout(function(){
81                 jQuery('#foo').ajaxStart(function(){
82                         ok( true, "ajaxStart" );
83                 }).ajaxStop(function(){
84                         ok( true, "ajaxStop" );
85                         start();
86                 }).ajaxSend(function(){
87                         ok( true, "ajaxSend" );
88                 }).ajaxComplete(function(){
89                         ok( true, "ajaxComplete" );
90                 }).ajaxError(function(){
91                         ok( false, "ajaxError" );
92                 }).ajaxSuccess(function(){
93                         ok( true, "ajaxSuccess" );
94                 });
95
96                 jQuery.ajax({
97                         url: url("data/name.html"),
98                         beforeSend: function(){ ok(true, "beforeSend"); }
99                 })
100                         .complete(function(){ ok(true, "complete"); })
101                         .success(function(){ ok(true, "success"); })
102                         .error(function(){ ok(false, "error"); });
103         }, 13);
104 });
105
106 test("jQuery.ajax() - success callbacks (oncomplete binding)", function() {
107         expect( 8 );
108
109         jQuery.ajaxSetup({ timeout: 0 });
110
111         stop();
112
113         setTimeout(function(){
114                 jQuery('#foo').ajaxStart(function(){
115                         ok( true, "ajaxStart" );
116                 }).ajaxStop(function(){
117                         ok( true, "ajaxStop" );
118                 }).ajaxSend(function(){
119                         ok( true, "ajaxSend" );
120                 }).ajaxComplete(function(){
121                         ok( true, "ajaxComplete" );
122                 }).ajaxError(function(){
123                         ok( false, "ajaxError" );
124                 }).ajaxSuccess(function(){
125                         ok( true, "ajaxSuccess" );
126                 });
127
128                 jQuery.ajax({
129                         url: url("data/name.html"),
130                         beforeSend: function(){ ok(true, "beforeSend"); },
131                         complete: function(xhr) {
132                                 xhr
133                                 .complete(function(){ ok(true, "complete"); })
134                                 .success(function(){ ok(true, "success"); })
135                                 .error(function(){ ok(false, "error"); })
136                                 .complete(function(){ start(); });
137                         }
138                 });
139         }, 13);
140 });
141
142 test("jQuery.ajax() - success callbacks (very late binding)", function() {
143         expect( 8 );
144
145         jQuery.ajaxSetup({ timeout: 0 });
146
147         stop();
148
149         setTimeout(function(){
150                 jQuery('#foo').ajaxStart(function(){
151                         ok( true, "ajaxStart" );
152                 }).ajaxStop(function(){
153                         ok( true, "ajaxStop" );
154                 }).ajaxSend(function(){
155                         ok( true, "ajaxSend" );
156                 }).ajaxComplete(function(){
157                         ok( true, "ajaxComplete" );
158                 }).ajaxError(function(){
159                         ok( false, "ajaxError" );
160                 }).ajaxSuccess(function(){
161                         ok( true, "ajaxSuccess" );
162                 });
163
164                 jQuery.ajax({
165                         url: url("data/name.html"),
166                         beforeSend: function(){ ok(true, "beforeSend"); },
167                         complete: function(xhr) {
168                                 setTimeout (function() {
169                                         xhr
170                                         .complete(function(){ ok(true, "complete"); })
171                                         .success(function(){ ok(true, "success"); })
172                                         .error(function(){ ok(false, "error"); })
173                                         .complete(function(){ start(); });
174                                 },100);
175                         }
176                 });
177         }, 13);
178 });
179
180 test("jQuery.ajax() - success callbacks (order)", function() {
181         expect( 1 );
182
183         jQuery.ajaxSetup({ timeout: 0 });
184
185         stop();
186
187         var testString = "";
188
189         setTimeout(function(){
190                 jQuery.ajax({
191                         url: url("data/name.html"),
192                         success: function( _1 , _2 , xhr ) {
193                                 xhr.success(function() {
194                                         xhr.success(function() {
195                                                 testString += "E";
196                                         });
197                                         testString += "D";
198                                 });
199                                 testString += "A";
200                         },
201                         complete: function() {
202                                 strictEqual(testString, "ABCDE", "Proper order");
203                                 start();
204                         }
205                 }).success(function() {
206                         testString += "B";
207                 }).success(function() {
208                         testString += "C";
209                 });
210         }, 13);
211 });
212
213 test("jQuery.ajax() - error callbacks", function() {
214         expect( 8 );
215         stop();
216
217         jQuery('#foo').ajaxStart(function(){
218                 ok( true, "ajaxStart" );
219         }).ajaxStop(function(){
220                 ok( true, "ajaxStop" );
221                 start();
222         }).ajaxSend(function(){
223                 ok( true, "ajaxSend" );
224         }).ajaxComplete(function(){
225                 ok( true, "ajaxComplete" );
226         }).ajaxError(function(){
227                 ok( true, "ajaxError" );
228         }).ajaxSuccess(function(){
229                 ok( false, "ajaxSuccess" );
230         });
231
232         jQuery.ajaxSetup({ timeout: 500 });
233
234         jQuery.ajax({
235                 url: url("data/name.php?wait=5"),
236                 beforeSend: function(){ ok(true, "beforeSend"); },
237                 success: function(){ ok(false, "success"); },
238                 error: function(){ ok(true, "error"); },
239                 complete: function(){ ok(true, "complete"); }
240         });
241 });
242
243 test( "jQuery.ajax - multiple method signatures introduced in 1.5 ( #8107)", function() {
244
245         expect( 4 );
246
247         stop();
248
249         jQuery.when(
250                 jQuery.ajax().success(function() { ok( true, 'With no arguments' ); }),
251                 jQuery.ajax('data/name.html').success(function() { ok( true, 'With only string URL argument' ); }),
252                 jQuery.ajax('data/name.html', {} ).success(function() { ok( true, 'With string URL param and map' ); }),
253                 jQuery.ajax({ url: 'data/name.html'} ).success(function() { ok( true, 'With only map' ); })
254         ).then( start, start );
255
256 });
257
258 test("jQuery.ajax() - textStatus and errorThrown values", function() {
259
260         var nb = 2;
261
262         expect( 2 * nb );
263         stop();
264
265         function startN() {
266                 if ( !( --nb ) ) {
267                         start();
268                 }
269         }
270
271         /*
272         Safari 3.x returns "OK" instead of "Not Found"
273         Safari 4.x doesn't have this issue so the test should be re-instated once
274         we drop support for 3.x
275
276         jQuery.ajax({
277                 url: url("data/nonExistingURL"),
278                 error: function( _ , textStatus , errorThrown ){
279                         strictEqual( textStatus, "error", "textStatus is 'error' for 404" );
280                         strictEqual( errorThrown, "Not Found", "errorThrown is 'Not Found' for 404");
281                         startN();
282                 }
283         });
284         */
285
286         jQuery.ajax({
287                 url: url("data/name.php?wait=5"),
288                 error: function( _ , textStatus , errorThrown ){
289                         strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
290                         strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort");
291                         startN();
292                 }
293         }).abort();
294
295         jQuery.ajax({
296                 url: url("data/name.php?wait=5"),
297                 error: function( _ , textStatus , errorThrown ){
298                         strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
299                         strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')");
300                         startN();
301                 }
302         }).abort( "mystatus" );
303 });
304
305 test("jQuery.ajax() - responseText on error", function() {
306
307         expect( 1 );
308
309         stop();
310
311         jQuery.ajax({
312                 url: url("data/errorWithText.php"),
313                 error: function(xhr) {
314                         strictEqual( xhr.responseText , "plain text message" , "Test jqXHR.responseText is filled for HTTP errors" );
315                 },
316                 complete: function() {
317                         start();
318                 }
319         });
320 });
321
322 test(".ajax() - retry with jQuery.ajax( this )", function() {
323
324         expect( 1 );
325
326         stop();
327
328         var firstTime = 1;
329
330         jQuery.ajax({
331                 url: url("data/errorWithText.php"),
332                 error: function() {
333                         if ( firstTime ) {
334                                 firstTime = 0;
335                                 jQuery.ajax( this );
336                         } else {
337                                 ok( true , "Test retrying with jQuery.ajax(this) works" );
338                                 start();
339                         }
340                 }
341         });
342
343 });
344
345 test(".ajax() - headers" , function() {
346
347         expect( 4 );
348
349         stop();
350
351         jQuery('#foo').ajaxSend(function( evt, xhr ) {
352                 xhr.setRequestHeader( "ajax-send", "test" );
353         });
354
355         var requestHeaders = {
356                         siMPle: "value",
357                         "SometHing-elsE": "other value",
358                         OthEr: "something else"
359                 },
360                 list = [],
361                 i;
362
363         for( i in requestHeaders ) {
364                 list.push( i );
365         }
366         list.push( "ajax-send" );
367
368         jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
369
370                 headers: requestHeaders,
371                 success: function( data , _ , xhr ) {
372                         var tmp = [];
373                         for ( i in requestHeaders ) {
374                                 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
375                         }
376                         tmp.push(  "ajax-send: test\n" );
377                         tmp = tmp.join( "" );
378
379                         strictEqual( data , tmp , "Headers were sent" );
380                         strictEqual( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
381                         if ( jQuery.browser.mozilla ) {
382                                 ok( true, "Firefox doesn't support empty headers" );
383                         } else {
384                                 strictEqual( xhr.getResponseHeader( "Empty-Header" ) , "" , "Empty header received" );
385                         }
386                         strictEqual( xhr.getResponseHeader( "Sample-Header2" ) , "Hello World 2" , "Second sample header received" );
387                 },
388                 error: function(){ ok(false, "error"); }
389
390         }).then( start, start );
391
392 });
393
394 test(".ajax() - Accept header" , function() {
395
396         expect( 1 );
397
398         stop();
399
400         jQuery.ajax(url("data/headers.php?keys=accept"), {
401                 headers: {
402                         Accept: "very wrong accept value"
403                 },
404                 beforeSend: function( xhr ) {
405                         xhr.setRequestHeader( "Accept", "*/*" );
406                 },
407                 success: function( data ) {
408                         strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
409                         start();
410                 },
411                 error: function(){ ok(false, "error"); }
412         });
413
414 });
415
416 test(".ajax() - contentType" , function() {
417
418         expect( 2 );
419
420         stop();
421
422         var count = 2;
423
424         function restart() {
425                 if ( ! --count ) {
426                         start();
427                 }
428         }
429
430         jQuery.ajax(url("data/headers.php?keys=content-type" ), {
431                 contentType: "test",
432                 success: function( data ) {
433                         strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
434                 },
435                 complete: function() {
436                         restart();
437                 }
438         });
439
440         jQuery.ajax(url("data/headers.php?keys=content-type" ), {
441                 contentType: false,
442                 success: function( data ) {
443                         strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
444                 },
445                 complete: function() {
446                         restart();
447                 }
448         });
449
450 });
451
452 test(".ajax() - protocol-less urls", function() {
453         expect(1);
454
455         jQuery.ajax({
456                 url: "//somedomain.com",
457                 beforeSend: function( xhr, settings ) {
458                         equals(settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added.");
459                         return false;
460                 }
461         });
462 });
463
464 test(".ajax() - hash", function() {
465         expect(3);
466
467         jQuery.ajax({
468                 url: "data/name.html#foo",
469                 beforeSend: function( xhr, settings ) {
470                         equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
471                         return false;
472                 }
473         });
474
475         jQuery.ajax({
476                 url: "data/name.html?abc#foo",
477                 beforeSend: function( xhr, settings ) {
478                 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
479                         return false;
480                 }
481         });
482
483         jQuery.ajax({
484                 url: "data/name.html?abc#foo",
485                 data: { "test": 123 },
486                 beforeSend: function( xhr, settings ) {
487                         equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
488                         return false;
489                 }
490         });
491 });
492
493 test("jQuery ajax - cross-domain detection", function() {
494
495         expect( 4 );
496
497         var loc = document.location,
498                 otherPort = loc.port === 666 ? 667 : 666,
499                 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
500
501         jQuery.ajax({
502                 dataType: "jsonp",
503                 url: otherProtocol + "//" + loc.host,
504                 beforeSend: function( _ , s ) {
505                         ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
506                         return false;
507                 }
508         });
509
510         jQuery.ajax({
511                 dataType: "jsonp",
512                 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
513                 beforeSend: function( _ , s ) {
514                         ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
515                         return false;
516                 }
517         });
518
519         jQuery.ajax({
520                 dataType: "jsonp",
521                 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
522                 beforeSend: function( _ , s ) {
523                         ok( s.crossDomain , "Test different ports are detected as cross-domain" );
524                         return false;
525                 }
526         });
527
528         jQuery.ajax({
529                 dataType: "jsonp",
530                 url: loc.protocol + "//" + loc.host,
531                 crossDomain: true,
532                 beforeSend: function( _ , s ) {
533                         ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
534                         return false;
535                 }
536         });
537
538 });
539
540 test(".load() - 404 error callbacks", function() {
541         expect( 6 );
542         stop();
543
544         jQuery('#foo').ajaxStart(function(){
545                 ok( true, "ajaxStart" );
546         }).ajaxStop(function(){
547                 ok( true, "ajaxStop" );
548                 start();
549         }).ajaxSend(function(){
550                 ok( true, "ajaxSend" );
551         }).ajaxComplete(function(){
552                 ok( true, "ajaxComplete" );
553         }).ajaxError(function(){
554                 ok( true, "ajaxError" );
555         }).ajaxSuccess(function(){
556                 ok( false, "ajaxSuccess" );
557         });
558
559         jQuery("<div/>").load("data/404.html", function(){
560                 ok(true, "complete");
561         });
562 });
563
564 test("jQuery.ajax() - abort", function() {
565         expect( 8 );
566         stop();
567
568         jQuery('#foo').ajaxStart(function(){
569                 ok( true, "ajaxStart" );
570         }).ajaxStop(function(){
571                 ok( true, "ajaxStop" );
572                 start();
573         }).ajaxSend(function(){
574                 ok( true, "ajaxSend" );
575         }).ajaxComplete(function(){
576                 ok( true, "ajaxComplete" );
577         });
578
579         var xhr = jQuery.ajax({
580                 url: url("data/name.php?wait=5"),
581                 beforeSend: function(){ ok(true, "beforeSend"); },
582                 complete: function(){ ok(true, "complete"); }
583         });
584
585         equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
586
587         xhr.abort();
588         equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
589 });
590
591 test("Ajax events with context", function() {
592         expect(14);
593
594         stop();
595         var context = document.createElement("div");
596
597         function event(e){
598                 equals( this, context, e.type );
599         }
600
601         function callback(msg){
602                 return function(){
603                         equals( this, context, "context is preserved on callback " + msg );
604                 };
605         }
606
607         function nocallback(msg){
608                 return function(){
609                         equals( typeof this.url, "string", "context is settings on callback " + msg );
610                 };
611         }
612
613         jQuery('#foo').add(context)
614                         .ajaxSend(event)
615                         .ajaxComplete(event)
616                         .ajaxError(event)
617                         .ajaxSuccess(event);
618
619         jQuery.ajax({
620                 url: url("data/name.html"),
621                 beforeSend: callback("beforeSend"),
622                 success: callback("success"),
623                 error: callback("error"),
624                 complete:function(){
625                         callback("complete").call(this);
626
627                         jQuery.ajax({
628                                 url: url("data/404.html"),
629                                 context: context,
630                                 beforeSend: callback("beforeSend"),
631                                 error: callback("error"),
632                                 complete: function(){
633                                         callback("complete").call(this);
634
635                                         jQuery('#foo').add(context).unbind();
636
637                                         jQuery.ajax({
638                                                 url: url("data/404.html"),
639                                                 beforeSend: nocallback("beforeSend"),
640                                                 error: nocallback("error"),
641                                                 complete: function(){
642                                                         nocallback("complete").call(this);
643                                                         start();
644                                                 }
645                                         });
646                                 }
647                         });
648                 },
649                 context:context
650         });
651 });
652
653 test("jQuery.ajax context modification", function() {
654         expect(1);
655
656         stop();
657
658         var obj = {};
659
660         jQuery.ajax({
661                 url: url("data/name.html"),
662                 context: obj,
663                 beforeSend: function(){
664                         this.test = "foo";
665                 },
666                 complete: function() {
667                         start();
668                 }
669         });
670
671         equals( obj.test, "foo", "Make sure the original object is maintained." );
672 });
673
674 test("jQuery.ajax context modification through ajaxSetup", function() {
675         expect(4);
676
677         stop();
678
679         var obj = {};
680
681         jQuery.ajaxSetup({
682                 context: obj
683         });
684
685         strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
686
687         jQuery.ajax({
688                 url: url("data/name.html"),
689                 complete: function() {
690                         strictEqual( this, obj, "Make sure the original object is maintained." );
691                         jQuery.ajax({
692                                 url: url("data/name.html"),
693                                 context: {},
694                                 complete: function() {
695                                         ok( this !== obj, "Make sure overidding context is possible." );
696                                         jQuery.ajaxSetup({
697                                                 context: false
698                                         });
699                                         jQuery.ajax({
700                                                 url: url("data/name.html"),
701                                                 beforeSend: function(){
702                                                         this.test = "foo2";
703                                                 },
704                                                 complete: function() {
705                                                         ok( this !== obj, "Make sure unsetting context is possible." );
706                                                         start();
707                                                 }
708                                         });
709                                 }
710                         });
711                 }
712         });
713 });
714
715 test("jQuery.ajax() - disabled globals", function() {
716         expect( 3 );
717         stop();
718
719         jQuery('#foo').ajaxStart(function(){
720                 ok( false, "ajaxStart" );
721         }).ajaxStop(function(){
722                 ok( false, "ajaxStop" );
723         }).ajaxSend(function(){
724                 ok( false, "ajaxSend" );
725         }).ajaxComplete(function(){
726                 ok( false, "ajaxComplete" );
727         }).ajaxError(function(){
728                 ok( false, "ajaxError" );
729         }).ajaxSuccess(function(){
730                 ok( false, "ajaxSuccess" );
731         });
732
733         jQuery.ajax({
734                 global: false,
735                 url: url("data/name.html"),
736                 beforeSend: function(){ ok(true, "beforeSend"); },
737                 success: function(){ ok(true, "success"); },
738                 error: function(){ ok(false, "error"); },
739                 complete: function(){
740                   ok(true, "complete");
741                   setTimeout(function(){ start(); }, 13);
742                 }
743         });
744 });
745
746 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
747         expect(3);
748         stop();
749         jQuery.ajax({
750           url: url("data/with_fries.xml"),
751           dataType: "xml",
752           success: function(resp) {
753                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
754                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
755                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
756                 start();
757           }
758         });
759 });
760
761 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
762         expect(3);
763         stop();
764         jQuery.ajax({
765           url: url("data/with_fries_over_jsonp.php"),
766           dataType: "jsonp xml",
767           success: function(resp) {
768                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
769                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
770                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
771                 start();
772           },
773           error: function(_1,_2,error) {
774                 ok( false, error );
775                 start();
776           }
777         });
778 });
779
780 test("jQuery.ajax - HEAD requests", function() {
781         expect(2);
782
783         stop();
784         jQuery.ajax({
785                 url: url("data/name.html"),
786                 type: "HEAD",
787                 success: function(data, status, xhr){
788                         var h = xhr.getAllResponseHeaders();
789                         ok( /Date/i.test(h), 'No Date in HEAD response' );
790
791                         jQuery.ajax({
792                                 url: url("data/name.html"),
793                                 data: { whip_it: "good" },
794                                 type: "HEAD",
795                                 success: function(data, status, xhr){
796                                         var h = xhr.getAllResponseHeaders();
797                                         ok( /Date/i.test(h), 'No Date in HEAD response with data' );
798                                         start();
799                                 }
800                         });
801                 }
802         });
803
804 });
805
806 test("jQuery.ajax - beforeSend", function() {
807         expect(1);
808         stop();
809
810         var check = false;
811
812         jQuery.ajaxSetup({ timeout: 0 });
813
814         jQuery.ajax({
815                 url: url("data/name.html"),
816                 beforeSend: function(xml) {
817                         check = true;
818                 },
819                 success: function(data) {
820                         ok( check, "check beforeSend was executed" );
821                         start();
822                 }
823         });
824 });
825
826 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
827         expect(2);
828         var request = jQuery.ajax({
829                 url: url("data/name.html"),
830                 beforeSend: function() {
831                         ok( true, "beforeSend got called, canceling" );
832                         return false;
833                 },
834                 success: function() {
835                         ok( false, "request didn't get canceled" );
836                 },
837                 complete: function() {
838                         ok( false, "request didn't get canceled" );
839                 },
840                 error: function() {
841                         ok( false, "request didn't get canceled" );
842                 }
843         });
844         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
845 });
846
847 test("jQuery.ajax - beforeSend, cancel request manually", function() {
848         expect(2);
849         var request = jQuery.ajax({
850                 url: url("data/name.html"),
851                 beforeSend: function(xhr) {
852                         ok( true, "beforeSend got called, canceling" );
853                         xhr.abort();
854                 },
855                 success: function() {
856                         ok( false, "request didn't get canceled" );
857                 },
858                 complete: function() {
859                         ok( false, "request didn't get canceled" );
860                 },
861                 error: function() {
862                         ok( false, "request didn't get canceled" );
863                 }
864         });
865         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
866 });
867
868 window.foobar = null;
869 window.testFoo = undefined;
870
871 test("jQuery.ajax - dataType html", function() {
872         expect(5);
873         stop();
874
875         var verifyEvaluation = function() {
876                 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
877                 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
878
879                 start();
880         };
881
882         jQuery.ajax({
883           dataType: "html",
884           url: url("data/test.html"),
885           success: function(data) {
886                 jQuery("#ap").html(data);
887                 ok( data.match(/^html text/), 'Check content for datatype html' );
888                 setTimeout(verifyEvaluation, 600);
889           }
890         });
891 });
892
893 test("serialize()", function() {
894         expect(5);
895
896         // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
897         jQuery("#search").after(
898                 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
899                 '<input type="number" id="html5number" name="number" value="43" />'
900         );
901
902         equals( jQuery('#form').serialize(),
903                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
904                 'Check form serialization as query string');
905
906         equals( jQuery('#form :input').serialize(),
907                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
908                 'Check input serialization as query string');
909
910         equals( jQuery('#testForm').serialize(),
911                 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
912                 'Check form serialization as query string');
913
914         equals( jQuery('#testForm :input').serialize(),
915                 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
916                 'Check input serialization as query string');
917
918         equals( jQuery('#form, #testForm').serialize(),
919                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
920                 'Multiple form serialization as query string');
921
922   /* Temporarily disabled. Opera 10 has problems with form serialization.
923         equals( jQuery('#form, #testForm :input').serialize(),
924                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
925                 'Mixed form/input serialization as query string');
926         */
927         jQuery("#html5email, #html5number").remove();
928 });
929
930 test("jQuery.param()", function() {
931         expect(24);
932
933         equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
934
935         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
936         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
937
938         params = {someName: [1, 2, 3], regularThing: "blah" };
939         equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
940
941         params = {foo: ['a', 'b', 'c']};
942         equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
943
944         params = {foo: ["baz", 42, "All your base are belong to us"] };
945         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
946
947         params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
948         equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
949
950         params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
951         equals( decodeURIComponent( jQuery.param(params) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure" );
952
953         params = { a: [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { b: [ 7, [ 8, 9 ], [ { c: 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { e: { f: { g: [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
954         equals( decodeURIComponent( jQuery.param(params) ), "a[]=0&a[1][]=1&a[1][]=2&a[2][]=3&a[2][1][]=4&a[2][1][]=5&a[2][2][]=6&a[3][b][]=7&a[3][b][1][]=8&a[3][b][1][]=9&a[3][b][2][0][c]=10&a[3][b][2][0][d]=11&a[3][b][3][0][]=12&a[3][b][4][0][0][]=13&a[3][b][5][e][f][g][]=14&a[3][b][5][e][f][g][1][]=15&a[3][b][]=16&a[]=17", "nested arrays" );
955
956         params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
957         equals( jQuery.param(params,true), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure, forced traditional" );
958
959         equals( decodeURIComponent( jQuery.param({ a: [1,2,3], 'b[]': [4,5,6], 'c[d]': [7,8,9], e: { f: [10], g: [11,12], h: 13 } }) ), "a[]=1&a[]=2&a[]=3&b[]=4&b[]=5&b[]=6&c[d][]=7&c[d][]=8&c[d][]=9&e[f][]=10&e[g][]=11&e[g][]=12&e[h]=13", "Make sure params are not double-encoded." );
960
961         // Make sure empty arrays and objects are handled #6481
962         equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
963         equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
964         equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
965
966         // #7945
967         equals( jQuery.param({"jquery": "1.4.2"}), "jquery=1.4.2", "Check that object with a jQuery property get serialized correctly" );
968
969         jQuery.ajaxSetup({ traditional: true });
970
971         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
972         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
973
974         params = {someName: [1, 2, 3], regularThing: "blah" };
975         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
976
977         params = {foo: ['a', 'b', 'c']};
978         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
979
980         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
981         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
982
983         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
984         equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
985
986         params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
987         equals( jQuery.param(params), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure" );
988
989         params = { a: [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { b: [ 7, [ 8, 9 ], [ { c: 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { e: { f: { g: [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
990         equals( jQuery.param(params), "a=0&a=1%2C2&a=3%2C4%2C5%2C6&a=%5Bobject+Object%5D&a=17", "nested arrays (not possible when jQuery.param.traditional == true)" );
991
992         params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
993         equals( decodeURIComponent( jQuery.param(params,false) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure, forced not traditional" );
994
995         params = { param1: null };
996         equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
997
998         params = {'test': {'length': 3, 'foo': 'bar'} };
999         equals( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
1000 });
1001
1002 test("synchronous request", function() {
1003         expect(1);
1004         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
1005 });
1006
1007 test("synchronous request with callbacks", function() {
1008         expect(2);
1009         var result;
1010         jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
1011         ok( /^{ "data"/.test( result ), "check returned text" );
1012 });
1013
1014 test("pass-through request object", function() {
1015         expect(8);
1016         stop();
1017
1018         var target = "data/name.html";
1019         var successCount = 0;
1020         var errorCount = 0;
1021         var errorEx = "";
1022         var success = function() {
1023                 successCount++;
1024         };
1025         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
1026                 errorCount++;
1027                 errorEx += ": " + xml.status;
1028         });
1029         jQuery("#foo").one('ajaxStop', function () {
1030                 equals(successCount, 5, "Check all ajax calls successful");
1031                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
1032                 jQuery("#foo").unbind('ajaxError');
1033
1034                 start();
1035         });
1036
1037         ok( jQuery.get(url(target), success), "get" );
1038         ok( jQuery.post(url(target), success), "post" );
1039         ok( jQuery.getScript(url("data/test.js"), success), "script" );
1040         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1041         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1042 });
1043
1044 test("ajax cache", function () {
1045         expect(18);
1046
1047         stop();
1048
1049         var count = 0;
1050
1051         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1052                 var re = /_=(.*?)(&|$)/g;
1053                 var oldOne = null;
1054                 for (var i = 0; i < 6; i++) {
1055                         var ret = re.exec(s.url);
1056                         if (!ret) {
1057                                 break;
1058                         }
1059                         oldOne = ret[1];
1060                 }
1061                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1062                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1063                 if(++count == 6)
1064                         start();
1065         });
1066
1067         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1068         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1069         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1070         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1071         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1072         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1073 });
1074
1075 /*
1076  * Test disabled.
1077  * The assertions expect that the passed-in object will be modified,
1078  * which shouldn't be the case. Fixes #5439.
1079 test("global ajaxSettings", function() {
1080         expect(2);
1081
1082         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1083         var orig = { url: "data/with_fries.xml" };
1084         var t;
1085
1086         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1087
1088         t = jQuery.extend({}, orig);
1089         t.data = {};
1090         jQuery.ajax(t);
1091         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1092
1093         t = jQuery.extend({}, orig);
1094         t.data = { zoo: 'a', ping: 'b' };
1095         jQuery.ajax(t);
1096         ok( t.url.indexOf('ping') > -1 && t.url.indexOf('zoo') > -1 && t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending { zoo: 'a', ping: 'b' }" );
1097
1098         jQuery.ajaxSettings = tmp;
1099 });
1100 */
1101
1102 test("load(String)", function() {
1103         expect(1);
1104         stop(); // check if load can be called with only url
1105         jQuery('#first').load("data/name.html", start);
1106 });
1107
1108 test("load('url selector')", function() {
1109         expect(1);
1110         stop(); // check if load can be called with only url
1111         jQuery('#first').load("data/test3.html div.user", function(){
1112                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1113                 start();
1114         });
1115 });
1116
1117 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1118         expect(1);
1119         stop();
1120         jQuery.ajaxSetup({ dataType: "json" });
1121         jQuery("#first").ajaxComplete(function (e, xml, s) {
1122                 equals( s.dataType, "html", "Verify the load() dataType was html" );
1123                 jQuery("#first").unbind("ajaxComplete");
1124                 jQuery.ajaxSetup({ dataType: "" });
1125                 start();
1126         });
1127         jQuery('#first').load("data/test3.html");
1128 });
1129
1130 test("load(String, Function) - simple: inject text into DOM", function() {
1131         expect(2);
1132         stop();
1133         jQuery('#first').load(url("data/name.html"), function() {
1134                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1135                 start();
1136         });
1137 });
1138
1139 test("load(String, Function) - check scripts", function() {
1140         expect(7);
1141         stop();
1142
1143         var verifyEvaluation = function() {
1144                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1145                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1146
1147                 start();
1148         };
1149         jQuery('#first').load(url('data/test.html'), function() {
1150                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1151                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1152                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1153                 setTimeout(verifyEvaluation, 600);
1154         });
1155 });
1156
1157 test("load(String, Function) - check file with only a script tag", function() {
1158         expect(3);
1159         stop();
1160
1161         jQuery('#first').load(url('data/test2.html'), function() {
1162                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1163                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1164
1165                 start();
1166         });
1167 });
1168
1169 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1170         expect(2);
1171         stop();
1172         jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1173         var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1174                 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1175                 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1176                 jQuery.ajaxSetup({ dataFilter: 0 });
1177                 start();
1178         });
1179 });
1180
1181 test("load(String, Object, Function)", function() {
1182         expect(2);
1183         stop();
1184
1185         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1186                 var $post = jQuery(this).find('#post');
1187                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1188                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1189                 start();
1190         });
1191 });
1192
1193 test("load(String, String, Function)", function() {
1194         expect(2);
1195         stop();
1196
1197         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1198                 var $get = jQuery(this).find('#get');
1199                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1200                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
1201                 start();
1202         });
1203 });
1204
1205 test("jQuery.get(String, Function) - data in ajaxSettings (#8277)", function() {
1206         expect(1);
1207         stop();
1208         jQuery.ajaxSetup({
1209                 data: "helloworld"
1210         });
1211         jQuery.get(url('data/echoQuery.php'), function(data) {
1212                 ok( /helloworld$/.test( data ), 'Data from ajaxSettings was used');
1213                 jQuery.ajaxSetup({
1214                         data: null
1215                 });
1216                 start();
1217         });
1218 });
1219
1220 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1221         expect(2);
1222         stop();
1223         jQuery.get(url('data/dashboard.xml'), function(xml) {
1224                 var content = [];
1225                 jQuery('tab', xml).each(function() {
1226                         content.push(jQuery(this).text());
1227                 });
1228                 equals( content[0], 'blabla', 'Check first tab');
1229                 equals( content[1], 'blublu', 'Check second tab');
1230                 start();
1231         });
1232 });
1233
1234 test("jQuery.getScript(String, Function) - with callback", function() {
1235         expect(3);
1236         stop();
1237         jQuery.getScript(url("data/test.js"), function( data, _, jqXHR ) {
1238                 equals( foobar, "bar", 'Check if script was evaluated' );
1239                 strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script (#8082)" );
1240                 setTimeout(start, 100);
1241         });
1242 });
1243
1244 test("jQuery.getScript(String, Function) - no callback", function() {
1245         expect(1);
1246         stop();
1247         jQuery.getScript(url("data/test.js"), function(){
1248                 start();
1249         });
1250 });
1251
1252 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1253
1254         test("jQuery.ajax() - JSONP, " + label, function() {
1255                 expect(20);
1256
1257                 var count = 0;
1258                 function plus(){ if ( ++count == 18 ) start(); }
1259
1260                 stop();
1261
1262                 jQuery.ajax({
1263                         url: "data/jsonp.php",
1264                         dataType: "jsonp",
1265                         crossDomain: crossDomain,
1266                         success: function(data){
1267                                 ok( data.data, "JSON results returned (GET, no callback)" );
1268                                 plus();
1269                         },
1270                         error: function(data){
1271                                 ok( false, "Ajax error JSON (GET, no callback)" );
1272                                 plus();
1273                         }
1274                 });
1275
1276                 jQuery.ajax({
1277                         url: "data/jsonp.php?callback=?",
1278                         dataType: "jsonp",
1279                         crossDomain: crossDomain,
1280                         success: function(data){
1281                                 ok( data.data, "JSON results returned (GET, url callback)" );
1282                                 plus();
1283                         },
1284                         error: function(data){
1285                                 ok( false, "Ajax error JSON (GET, url callback)" );
1286                                 plus();
1287                         }
1288                 });
1289
1290                 jQuery.ajax({
1291                         url: "data/jsonp.php",
1292                         dataType: "jsonp",
1293                         crossDomain: crossDomain,
1294                         data: "callback=?",
1295                         success: function(data){
1296                                 ok( data.data, "JSON results returned (GET, data callback)" );
1297                                 plus();
1298                         },
1299                         error: function(data){
1300                                 ok( false, "Ajax error JSON (GET, data callback)" );
1301                                 plus();
1302                         }
1303                 });
1304
1305                 jQuery.ajax({
1306                         url: "data/jsonp.php?callback=??",
1307                         dataType: "jsonp",
1308                         crossDomain: crossDomain,
1309                         success: function(data){
1310                                 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1311                                 plus();
1312                         },
1313                         error: function(data){
1314                                 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1315                                 plus();
1316                         }
1317                 });
1318
1319                 jQuery.ajax({
1320                         url: "data/jsonp.php",
1321                         dataType: "jsonp",
1322                         crossDomain: crossDomain,
1323                         data: "callback=??",
1324                         success: function(data){
1325                                 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1326                                 plus();
1327                         },
1328                         error: function(data){
1329                                 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1330                                 plus();
1331                         }
1332                 });
1333
1334                 jQuery.ajax({
1335                         url: "data/jsonp.php/??",
1336                         dataType: "jsonp",
1337                         crossDomain: crossDomain,
1338                         success: function(data){
1339                                 ok( data.data, "JSON results returned (GET, REST-like)" );
1340                                 plus();
1341                         },
1342                         error: function(data){
1343                                 ok( false, "Ajax error JSON (GET, REST-like)" );
1344                                 plus();
1345                         }
1346                 });
1347
1348                 jQuery.ajax({
1349                         url: "data/jsonp.php/???json=1",
1350                         dataType: "jsonp",
1351                         crossDomain: crossDomain,
1352                         success: function(data){
1353                                 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1354                                 plus();
1355                         },
1356                         error: function(data){
1357                                 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1358                                 plus();
1359                         }
1360                 });
1361
1362                 jQuery.ajax({
1363                         url: "data/jsonp.php",
1364                         dataType: "jsonp",
1365                         crossDomain: crossDomain,
1366                         jsonp: "callback",
1367                         success: function(data){
1368                                 ok( data.data, "JSON results returned (GET, data obj callback)" );
1369                                 plus();
1370                         },
1371                         error: function(data){
1372                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1373                                 plus();
1374                         }
1375                 });
1376
1377                 window.jsonpResults = function(data) {
1378                         ok( data.data, "JSON results returned (GET, custom callback function)" );
1379                         window.jsonpResults = undefined;
1380                         plus();
1381                 };
1382
1383                 jQuery.ajax({
1384                         url: "data/jsonp.php",
1385                         dataType: "jsonp",
1386                         crossDomain: crossDomain,
1387                         jsonpCallback: "jsonpResults",
1388                         success: function(data){
1389                                 ok( data.data, "JSON results returned (GET, custom callback name)" );
1390                                 plus();
1391                         },
1392                         error: function(data){
1393                                 ok( false, "Ajax error JSON (GET, custom callback name)" );
1394                                 plus();
1395                         }
1396                 });
1397
1398                 jQuery.ajax({
1399                         url: "data/jsonp.php",
1400                         dataType: "jsonp",
1401                         crossDomain: crossDomain,
1402                         jsonpCallback: "functionToCleanUp",
1403                         success: function(data){
1404                                 ok( data.data, "JSON results returned (GET, custom callback name to be cleaned up)" );
1405                                 strictEqual( window.functionToCleanUp, undefined, "Callback was removed (GET, custom callback name to be cleaned up)" );
1406                                 plus();
1407                                 var xhr;
1408                                 jQuery.ajax({
1409                                         url: "data/jsonp.php",
1410                                         dataType: "jsonp",
1411                                         crossDomain: crossDomain,
1412                                         jsonpCallback: "functionToCleanUp",
1413                                         beforeSend: function( jqXHR ) {
1414                                                 xhr = jqXHR;
1415                                                 return false;
1416                                         }
1417                                 });
1418                                 xhr.error(function() {
1419                                         ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1420                                         strictEqual( window.functionToCleanUp, undefined, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1421                                         plus();
1422                                 });
1423                         },
1424                         error: function(data){
1425                                 ok( false, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1426                                 plus();
1427                         }
1428                 });
1429
1430                 jQuery.ajax({
1431                         type: "POST",
1432                         url: "data/jsonp.php",
1433                         dataType: "jsonp",
1434                         crossDomain: crossDomain,
1435                         success: function(data){
1436                                 ok( data.data, "JSON results returned (POST, no callback)" );
1437                                 plus();
1438                         },
1439                         error: function(data){
1440                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1441                                 plus();
1442                         }
1443                 });
1444
1445                 jQuery.ajax({
1446                         type: "POST",
1447                         url: "data/jsonp.php",
1448                         data: "callback=?",
1449                         dataType: "jsonp",
1450                         crossDomain: crossDomain,
1451                         success: function(data){
1452                                 ok( data.data, "JSON results returned (POST, data callback)" );
1453                                 plus();
1454                         },
1455                         error: function(data){
1456                                 ok( false, "Ajax error JSON (POST, data callback)" );
1457                                 plus();
1458                         }
1459                 });
1460
1461                 jQuery.ajax({
1462                         type: "POST",
1463                         url: "data/jsonp.php",
1464                         jsonp: "callback",
1465                         dataType: "jsonp",
1466                         crossDomain: crossDomain,
1467                         success: function(data){
1468                                 ok( data.data, "JSON results returned (POST, data obj callback)" );
1469                                 plus();
1470                         },
1471                         error: function(data){
1472                                 ok( false, "Ajax error JSON (POST, data obj callback)" );
1473                                 plus();
1474                         }
1475                 });
1476
1477                 //#7578
1478                 jQuery.ajax({
1479                         url: "data/jsonp.php",
1480                         dataType: "jsonp",
1481                         crossDomain: crossDomain,
1482                         beforeSend: function(){
1483                                 strictEqual( this.cache, false, "cache must be false on JSON request" );
1484                                 plus();
1485                                 return false;
1486                         }
1487                 });
1488
1489                 jQuery.ajax({
1490                         url: "data/jsonp.php?callback=XXX",
1491                         dataType: "jsonp",
1492                         jsonp: false,
1493                         jsonpCallback: "XXX",
1494                         crossDomain: crossDomain,
1495                         beforeSend: function() {
1496                                 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1497                                         "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1498                                 plus();
1499                         },
1500                         success: function(data){
1501                                 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1502                                 plus();
1503                         },
1504                         error: function(data){
1505                                 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1506                                 plus();
1507                         }
1508                 });
1509
1510         });
1511 });
1512
1513 test("jQuery.ajax() - script, Remote", function() {
1514         expect(2);
1515
1516         var base = window.location.href.replace(/[^\/]*$/, "");
1517
1518         stop();
1519
1520         jQuery.ajax({
1521                 url: base + "data/test.js",
1522                 dataType: "script",
1523                 success: function(data){
1524                         ok( foobar, "Script results returned (GET, no callback)" );
1525                         start();
1526                 }
1527         });
1528 });
1529
1530 test("jQuery.ajax() - script, Remote with POST", function() {
1531         expect(3);
1532
1533         var base = window.location.href.replace(/[^\/]*$/, "");
1534
1535         stop();
1536
1537         jQuery.ajax({
1538                 url: base + "data/test.js",
1539                 type: "POST",
1540                 dataType: "script",
1541                 success: function(data, status){
1542                         ok( foobar, "Script results returned (POST, no callback)" );
1543                         equals( status, "success", "Script results returned (POST, no callback)" );
1544                         start();
1545                 },
1546                 error: function(xhr) {
1547                         ok( false, "ajax error, status code: " + xhr.status );
1548                         start();
1549                 }
1550         });
1551 });
1552
1553 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1554         expect(2);
1555
1556         var base = window.location.href.replace(/[^\/]*$/, "");
1557         base = base.replace(/^.*?\/\//, "//");
1558
1559         stop();
1560
1561         jQuery.ajax({
1562                 url: base + "data/test.js",
1563                 dataType: "script",
1564                 success: function(data){
1565                         ok( foobar, "Script results returned (GET, no callback)" );
1566                         start();
1567                 }
1568         });
1569 });
1570
1571 test("jQuery.ajax() - malformed JSON", function() {
1572         expect(2);
1573
1574         stop();
1575
1576         jQuery.ajax({
1577                 url: "data/badjson.js",
1578                 dataType: "json",
1579                 success: function(){
1580                         ok( false, "Success." );
1581                         start();
1582                 },
1583                 error: function(xhr, msg, detailedMsg) {
1584                         equals( "parsererror", msg, "A parse error occurred." );
1585                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1586                         start();
1587                 }
1588         });
1589 });
1590
1591 test("jQuery.ajax() - script by content-type", function() {
1592         expect(2);
1593
1594         stop();
1595
1596         jQuery.when(
1597
1598                 jQuery.ajax({
1599                         url: "data/script.php",
1600                         data: { header: "script" }
1601                 }),
1602
1603                 jQuery.ajax({
1604                         url: "data/script.php",
1605                         data: { header: "ecma" }
1606                 })
1607
1608         ).then( start, start );
1609 });
1610
1611 test("jQuery.ajax() - json by content-type", function() {
1612         expect(5);
1613
1614         stop();
1615
1616         jQuery.ajax({
1617                 url: "data/json.php",
1618                 data: { header: "json", json: "array" },
1619                 success: function( json ) {
1620                         ok( json.length >= 2, "Check length");
1621                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1622                         equals( json[0].age, 21, 'Check JSON: first, age' );
1623                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1624                         equals( json[1].age, 25, 'Check JSON: second, age' );
1625                         start();
1626                 }
1627         });
1628 });
1629
1630 test("jQuery.ajax() - json by content-type disabled with options", function() {
1631         expect(6);
1632
1633         stop();
1634
1635         jQuery.ajax({
1636                 url: url("data/json.php"),
1637                 data: { header: "json", json: "array" },
1638                 contents: {
1639                         json: false
1640                 },
1641                 success: function( text ) {
1642                         equals( typeof text , "string" , "json wasn't auto-determined" );
1643                         var json = jQuery.parseJSON( text );
1644                         ok( json.length >= 2, "Check length");
1645                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1646                         equals( json[0].age, 21, 'Check JSON: first, age' );
1647                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1648                         equals( json[1].age, 25, 'Check JSON: second, age' );
1649                         start();
1650                 }
1651         });
1652 });
1653
1654 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1655         expect(5);
1656         stop();
1657         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1658           ok( json.length >= 2, "Check length");
1659           equals( json[0].name, 'John', 'Check JSON: first, name' );
1660           equals( json[0].age, 21, 'Check JSON: first, age' );
1661           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1662           equals( json[1].age, 25, 'Check JSON: second, age' );
1663           start();
1664         });
1665 });
1666
1667 test("jQuery.getJSON(String, Function) - JSON object", function() {
1668         expect(2);
1669         stop();
1670         jQuery.getJSON(url("data/json.php"), function(json) {
1671           if (json && json.data) {
1672                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1673                   equals( json.data.length, 25, 'Check JSON: length' );
1674           }
1675           start();
1676         });
1677 });
1678
1679 test("jQuery.getJSON - Using Native JSON", function() {
1680         expect(2);
1681
1682         var old = window.JSON;
1683         JSON = {
1684                 parse: function(str){
1685                         ok( true, "Verifying that parse method was run" );
1686                         return true;
1687                 }
1688         };
1689
1690         stop();
1691         jQuery.getJSON(url("data/json.php"), function(json) {
1692                 window.JSON = old;
1693                 equals( json, true, "Verifying return value" );
1694                 start();
1695         });
1696 });
1697
1698 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1699         expect(2);
1700
1701         var base = window.location.href.replace(/[^\/]*$/, "");
1702
1703         stop();
1704         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1705           equals( json.data.lang, 'en', 'Check JSON: lang' );
1706           equals( json.data.length, 25, 'Check JSON: length' );
1707           start();
1708         });
1709 });
1710
1711 test("jQuery.post - data", 3, function() {
1712         stop();
1713
1714         jQuery.when(
1715                 jQuery.post( url( "data/name.php" ), { xml: "5-2", length: 3 }, function( xml ) {
1716                         jQuery( 'math', xml ).each( function() {
1717                                 equals( jQuery( 'calculation', this ).text(), '5-2', 'Check for XML' );
1718                                 equals( jQuery( 'result', this ).text(), '3', 'Check for XML' );
1719                         });
1720                 }),
1721
1722                 jQuery.ajax({
1723                         url: url('data/echoData.php'),
1724                         type: "POST",
1725                         data: {
1726                                 'test': {
1727                                         'length': 7,
1728                                                 'foo': 'bar'
1729                                 }
1730                         },
1731                         success: function( data ) {
1732                                 strictEqual( data, 'test%5Blength%5D=7&test%5Bfoo%5D=bar', 'Check if a sub-object with a length param is serialized correctly');
1733                         }
1734                 })
1735         ).then( start, start );
1736
1737 });
1738
1739 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1740         expect(4);
1741         stop();
1742         var done = 0;
1743
1744         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1745           jQuery('math', xml).each(function() {
1746                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1747                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1748                  });
1749           if ( ++done === 2 ) start();
1750         });
1751
1752         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1753           jQuery('math', xml).each(function() {
1754                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1755                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1756                  });
1757           if ( ++done === 2 ) start();
1758         });
1759 });
1760
1761 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1762         stop();
1763
1764         var passed = 0;
1765
1766         jQuery.ajaxSetup({timeout: 1000});
1767
1768         var pass = function() {
1769                 passed++;
1770                 if ( passed == 2 ) {
1771                         ok( true, 'Check local and global callbacks after timeout' );
1772                         jQuery('#main').unbind("ajaxError");
1773                         start();
1774                 }
1775         };
1776
1777         var fail = function(a,b,c) {
1778                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1779                 start();
1780         };
1781
1782         jQuery('#main').ajaxError(pass);
1783
1784         jQuery.ajax({
1785           type: "GET",
1786           url: url("data/name.php?wait=5"),
1787           error: pass,
1788           success: fail
1789         });
1790
1791         // reset timeout
1792         jQuery.ajaxSetup({timeout: 0});
1793 });
1794
1795 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1796         stop();
1797         jQuery.ajaxSetup({timeout: 50});
1798
1799         jQuery.ajax({
1800           type: "GET",
1801           timeout: 15000,
1802           url: url("data/name.php?wait=1"),
1803           error: function() {
1804                    ok( false, 'Check for local timeout failed' );
1805                    start();
1806           },
1807           success: function() {
1808                 ok( true, 'Check for local timeout' );
1809                 start();
1810           }
1811         });
1812
1813         // reset timeout
1814         jQuery.ajaxSetup({timeout: 0});
1815 });
1816
1817 test("jQuery.ajax - simple get", function() {
1818         expect(1);
1819         stop();
1820         jQuery.ajax({
1821           type: "GET",
1822           url: url("data/name.php?name=foo"),
1823           success: function(msg){
1824                 equals( msg, 'bar', 'Check for GET' );
1825                 start();
1826           }
1827         });
1828 });
1829
1830 test("jQuery.ajax - simple post", function() {
1831         expect(1);
1832         stop();
1833         jQuery.ajax({
1834           type: "POST",
1835           url: url("data/name.php"),
1836           data: "name=peter",
1837           success: function(msg){
1838                 equals( msg, 'pan', 'Check for POST' );
1839                 start();
1840           }
1841         });
1842 });
1843
1844 test("ajaxSetup()", function() {
1845         expect(1);
1846         stop();
1847         jQuery.ajaxSetup({
1848                 url: url("data/name.php?name=foo"),
1849                 success: function(msg){
1850                         equals( msg, 'bar', 'Check for GET' );
1851                         start();
1852                 }
1853         });
1854         jQuery.ajax();
1855 });
1856
1857 /*
1858 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1859         stop();
1860         jQuery.ajax({
1861                 url: "data/name.php?wait=1",
1862                 timeout: 500,
1863                 error: function(request, status) {
1864                         ok( status != null, "status shouldn't be null in error handler" );
1865                         equals( "timeout", status );
1866                         start();
1867                 }
1868         });
1869 });
1870 */
1871
1872 test("data option: evaluate function values (#2806)", function() {
1873         stop();
1874         jQuery.ajax({
1875                 url: "data/echoQuery.php",
1876                 data: {
1877                         key: function() {
1878                                 return "value";
1879                         }
1880                 },
1881                 success: function(result) {
1882                         equals( result, "key=value" );
1883                         start();
1884                 }
1885         });
1886 });
1887
1888 test("data option: empty bodies for non-GET requests", function() {
1889         stop();
1890         jQuery.ajax({
1891                 url: "data/echoData.php",
1892                 data: undefined,
1893                 type: "post",
1894                 success: function(result) {
1895                         equals( result, "" );
1896                         start();
1897                 }
1898         });
1899 });
1900
1901 var ifModifiedNow = new Date();
1902
1903 jQuery.each( { " (cache)": true, " (no cache)": false }, function( label, cache ) {
1904
1905         test("jQuery.ajax - If-Modified-Since support" + label, function() {
1906                 expect( 3 );
1907
1908                 stop();
1909
1910                 var url = "data/if_modified_since.php?ts=" + ifModifiedNow++;
1911
1912                 jQuery.ajax({
1913                         url: url,
1914                         ifModified: true,
1915                         cache: cache,
1916                         success: function(data, status) {
1917                                 equals(status, "success" );
1918
1919                                 jQuery.ajax({
1920                                         url: url,
1921                                         ifModified: true,
1922                                         cache: cache,
1923                                         success: function(data, status) {
1924                                                 if ( data === "FAIL" ) {
1925                                                         ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1926                                                         ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1927                                                 } else {
1928                                                         equals(status, "notmodified");
1929                                                         ok(data == null, "response body should be empty");
1930                                                 }
1931                                                 start();
1932                                 },
1933                                         error: function() {
1934                                                 // Do this because opera simply refuses to implement 304 handling :(
1935                                                 // A feature-driven way of detecting this would be appreciated
1936                                                 // See: http://gist.github.com/599419
1937                                                 ok(jQuery.browser.opera, "error");
1938                                                 ok(jQuery.browser.opera, "error");
1939                                                 start();
1940                                         }
1941                                 });
1942                         },
1943                         error: function() {
1944                                 equals(false, "error");
1945                                 // Do this because opera simply refuses to implement 304 handling :(
1946                                 // A feature-driven way of detecting this would be appreciated
1947                                 // See: http://gist.github.com/599419
1948                                 ok(jQuery.browser.opera, "error");
1949                                 start();
1950                         }
1951                 });
1952         });
1953
1954         test("jQuery.ajax - Etag support" + label, function() {
1955                 expect( 3 );
1956
1957                 stop();
1958
1959                 var url = "data/etag.php?ts=" + ifModifiedNow++;
1960
1961                 jQuery.ajax({
1962                         url: url,
1963                         ifModified: true,
1964                         cache: cache,
1965                         success: function(data, status) {
1966                                 equals(status, "success" );
1967
1968                                 jQuery.ajax({
1969                                         url: url,
1970                                         ifModified: true,
1971                                         cache: cache,
1972                                         success: function(data, status) {
1973                                                 if ( data === "FAIL" ) {
1974                                                         ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1975                                                         ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1976                                                 } else {
1977                                                         equals(status, "notmodified");
1978                                                         ok(data == null, "response body should be empty");
1979                                                 }
1980                                                 start();
1981                                 },
1982                                 error: function() {
1983                                                 // Do this because opera simply refuses to implement 304 handling :(
1984                                                 // A feature-driven way of detecting this would be appreciated
1985                                                 // See: http://gist.github.com/599419
1986                                                 ok(jQuery.browser.opera, "error");
1987                                                 ok(jQuery.browser.opera, "error");
1988                                                 start();
1989                                         }
1990                                 });
1991                         },
1992                         error: function() {
1993                                 // Do this because opera simply refuses to implement 304 handling :(
1994                                 // A feature-driven way of detecting this would be appreciated
1995                                 // See: http://gist.github.com/599419
1996                                 ok(jQuery.browser.opera, "error");
1997                                 start();
1998                         }
1999                 });
2000         });
2001 });
2002
2003 test("jQuery ajax - failing cross-domain", function() {
2004
2005         expect( 2 );
2006
2007         stop();
2008
2009         var i = 2;
2010
2011         jQuery.ajax({
2012                 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
2013                 success: function(){ ok( false , "success" ); },
2014                 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
2015                 complete: function() { if ( ! --i ) start(); }
2016         });
2017
2018         jQuery.ajax({
2019                 url: 'http://www.google.com',
2020                 success: function(){ ok( false , "success" ); },
2021                 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
2022                 complete: function() { if ( ! --i ) start(); }
2023         });
2024
2025 });
2026
2027 test("jQuery ajax - atom+xml", function() {
2028
2029         stop();
2030
2031         jQuery.ajax({
2032                 url: url( 'data/atom+xml.php' ),
2033                 success: function(){ ok( true , "success" ); },
2034                 error: function(){ ok( false , "error" ); },
2035                 complete: function() { start(); }
2036         });
2037
2038 });
2039
2040 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
2041         var success = false;
2042         try {
2043                 var xhr = jQuery.ajax({ url: window.location });
2044                 success = true;
2045                 xhr.abort();
2046         } catch (e) {}
2047
2048         ok( success, "document.location did not generate exception" );
2049 });
2050
2051 test( "jQuery.ajax - statusCode" , function() {
2052
2053         var count = 12;
2054
2055         expect( 20 );
2056         stop();
2057
2058         function countComplete() {
2059                 if ( ! --count ) {
2060                         start();
2061                 }
2062         }
2063
2064         function createStatusCodes( name , isSuccess ) {
2065                 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
2066                 return {
2067                         200: function() {
2068                                 ok( isSuccess , name );
2069                         },
2070                         404: function() {
2071                                 ok( ! isSuccess , name );
2072                         }
2073                 };
2074         }
2075
2076         jQuery.each( {
2077                 "data/name.html": true,
2078                 "data/someFileThatDoesNotExist.html": false
2079         } , function( uri , isSuccess ) {
2080
2081                 jQuery.ajax( url( uri ) , {
2082                         statusCode: createStatusCodes( "in options" , isSuccess ),
2083                         complete: countComplete
2084                 });
2085
2086                 jQuery.ajax( url( uri ) , {
2087                         complete: countComplete
2088                 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
2089
2090                 jQuery.ajax( url( uri ) , {
2091                         complete: function(jqXHR) {
2092                                 jqXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
2093                                 countComplete();
2094                         }
2095                 });
2096
2097                 jQuery.ajax( url( uri ) , {
2098                         complete: function(jqXHR) {
2099                                 setTimeout( function() {
2100                                         jqXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
2101                                         countComplete();
2102                                 } , 100 );
2103                         }
2104                 });
2105
2106                 jQuery.ajax( url( uri ) , {
2107                         statusCode: createStatusCodes( "all (options)" , isSuccess ),
2108                         complete: function(jqXHR) {
2109                                 jqXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2110                                 setTimeout( function() {
2111                                         jqXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2112                                         countComplete();
2113                                 } , 100 );
2114                         }
2115                 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2116
2117                 var testString = "";
2118
2119                 jQuery.ajax( url( uri ), {
2120                         success: function( a , b , jqXHR ) {
2121                                 ok( isSuccess , "success" );
2122                                 var statusCode = {};
2123                                 statusCode[ jqXHR.status ] = function() {
2124                                         testString += "B";
2125                                 };
2126                                 jqXHR.statusCode( statusCode );
2127                                 testString += "A";
2128                         },
2129                         error: function( jqXHR ) {
2130                                 ok( ! isSuccess , "error" );
2131                                 var statusCode = {};
2132                                 statusCode[ jqXHR.status ] = function() {
2133                                         testString += "B";
2134                                 };
2135                                 jqXHR.statusCode( statusCode );
2136                                 testString += "A";
2137                         },
2138                         complete: function() {
2139                                 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2140                                                 ( isSuccess ? "success" :  "error" ) + " callbacks" );
2141                                 countComplete();
2142                         }
2143                 } );
2144
2145         });
2146 });
2147
2148 test("jQuery.ajax - transitive conversions", function() {
2149
2150         expect( 8 );
2151
2152         stop();
2153
2154         jQuery.when(
2155
2156                 jQuery.ajax( url("data/json.php") , {
2157                         converters: {
2158                                 "json myJson": function( data ) {
2159                                         ok( true , "converter called" );
2160                                         return data;
2161                                 }
2162                         },
2163                         dataType: "myJson",
2164                         success: function() {
2165                                 ok( true , "Transitive conversion worked" );
2166                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2167                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2168                         }
2169                 }),
2170
2171                 jQuery.ajax( url("data/json.php") , {
2172                         converters: {
2173                                 "json myJson": function( data ) {
2174                                         ok( true , "converter called (*)" );
2175                                         return data;
2176                                 }
2177                         },
2178                         contents: false, /* headers are wrong so we ignore them */
2179                         dataType: "* myJson",
2180                         success: function() {
2181                                 ok( true , "Transitive conversion worked (*)" );
2182                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2183                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2184                         }
2185                 })
2186
2187         ).then( start , start );
2188
2189 });
2190
2191 test("jQuery.ajax - overrideMimeType", function() {
2192
2193         expect( 2 );
2194
2195         stop();
2196
2197         jQuery.when(
2198
2199                 jQuery.ajax( url("data/json.php") , {
2200                         beforeSend: function( xhr ) {
2201                                 xhr.overrideMimeType( "application/json" );
2202                         },
2203                         success: function( json ) {
2204                                 ok( json.data , "Mimetype overriden using beforeSend" );
2205                         }
2206                 }),
2207
2208                 jQuery.ajax( url("data/json.php") , {
2209                         mimeType: "application/json",
2210                         success: function( json ) {
2211                                 ok( json.data , "Mimetype overriden using mimeType option" );
2212                         }
2213                 })
2214
2215         ).then( start , start );
2216
2217 });
2218
2219 test("jQuery.ajax - abort in prefilter", function() {
2220
2221         expect( 1 );
2222
2223         jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
2224                 if ( options.abortInPrefilter ) {
2225                         jqXHR.abort();
2226                 }
2227         });
2228
2229         strictEqual( jQuery.ajax({
2230                 abortInPrefilter: true,
2231                 error: function() {
2232                         ok( false, "error callback called" );
2233                 }
2234         }), false, "Request was properly aborted early by the prefilter" );
2235
2236 });
2237
2238 test("jQuery.ajax - active counter", function() {
2239     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
2240 });
2241
2242 }
2243
2244 //}