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