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