3041834751d375679170959975c99778cde9a610
[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(23);
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         jQuery.ajaxSetup({ traditional: true });
969
970         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
971         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
972
973         params = {someName: [1, 2, 3], regularThing: "blah" };
974         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
975
976         params = {foo: ['a', 'b', 'c']};
977         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
978
979         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
980         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
981
982         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
983         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" );
984
985         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?" };
986         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" );
987
988         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 ] };
989         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)" );
990
991         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?" };
992         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" );
993
994         params = { param1: null };
995         equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
996
997         params = {'test': {'length': 3, 'foo': 'bar'} };
998         equals( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
999 });
1000
1001 test("synchronous request", function() {
1002         expect(1);
1003         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
1004 });
1005
1006 test("synchronous request with callbacks", function() {
1007         expect(2);
1008         var result;
1009         jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
1010         ok( /^{ "data"/.test( result ), "check returned text" );
1011 });
1012
1013 test("pass-through request object", function() {
1014         expect(8);
1015         stop();
1016
1017         var target = "data/name.html";
1018         var successCount = 0;
1019         var errorCount = 0;
1020         var errorEx = "";
1021         var success = function() {
1022                 successCount++;
1023         };
1024         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
1025                 errorCount++;
1026                 errorEx += ": " + xml.status;
1027         });
1028         jQuery("#foo").one('ajaxStop', function () {
1029                 equals(successCount, 5, "Check all ajax calls successful");
1030                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
1031                 jQuery("#foo").unbind('ajaxError');
1032
1033                 start();
1034         });
1035
1036         ok( jQuery.get(url(target), success), "get" );
1037         ok( jQuery.post(url(target), success), "post" );
1038         ok( jQuery.getScript(url("data/test.js"), success), "script" );
1039         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1040         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1041 });
1042
1043 test("ajax cache", function () {
1044         expect(18);
1045
1046         stop();
1047
1048         var count = 0;
1049
1050         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1051                 var re = /_=(.*?)(&|$)/g;
1052                 var oldOne = null;
1053                 for (var i = 0; i < 6; i++) {
1054                         var ret = re.exec(s.url);
1055                         if (!ret) {
1056                                 break;
1057                         }
1058                         oldOne = ret[1];
1059                 }
1060                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1061                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1062                 if(++count == 6)
1063                         start();
1064         });
1065
1066         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1067         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1068         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1069         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1070         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1071         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1072 });
1073
1074 /*
1075  * Test disabled.
1076  * The assertions expect that the passed-in object will be modified,
1077  * which shouldn't be the case. Fixes #5439.
1078 test("global ajaxSettings", function() {
1079         expect(2);
1080
1081         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1082         var orig = { url: "data/with_fries.xml" };
1083         var t;
1084
1085         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1086
1087         t = jQuery.extend({}, orig);
1088         t.data = {};
1089         jQuery.ajax(t);
1090         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1091
1092         t = jQuery.extend({}, orig);
1093         t.data = { zoo: 'a', ping: 'b' };
1094         jQuery.ajax(t);
1095         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' }" );
1096
1097         jQuery.ajaxSettings = tmp;
1098 });
1099 */
1100
1101 test("load(String)", function() {
1102         expect(1);
1103         stop(); // check if load can be called with only url
1104         jQuery('#first').load("data/name.html", start);
1105 });
1106
1107 test("load('url selector')", function() {
1108         expect(1);
1109         stop(); // check if load can be called with only url
1110         jQuery('#first').load("data/test3.html div.user", function(){
1111                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1112                 start();
1113         });
1114 });
1115
1116 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1117         expect(1);
1118         stop();
1119         jQuery.ajaxSetup({ dataType: "json" });
1120         jQuery("#first").ajaxComplete(function (e, xml, s) {
1121                 equals( s.dataType, "html", "Verify the load() dataType was html" );
1122                 jQuery("#first").unbind("ajaxComplete");
1123                 jQuery.ajaxSetup({ dataType: "" });
1124                 start();
1125         });
1126         jQuery('#first').load("data/test3.html");
1127 });
1128
1129 test("load(String, Function) - simple: inject text into DOM", function() {
1130         expect(2);
1131         stop();
1132         jQuery('#first').load(url("data/name.html"), function() {
1133                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1134                 start();
1135         });
1136 });
1137
1138 test("load(String, Function) - check scripts", function() {
1139         expect(7);
1140         stop();
1141
1142         var verifyEvaluation = function() {
1143                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1144                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1145
1146                 start();
1147         };
1148         jQuery('#first').load(url('data/test.html'), function() {
1149                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1150                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1151                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1152                 setTimeout(verifyEvaluation, 600);
1153         });
1154 });
1155
1156 test("load(String, Function) - check file with only a script tag", function() {
1157         expect(3);
1158         stop();
1159
1160         jQuery('#first').load(url('data/test2.html'), function() {
1161                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1162                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1163
1164                 start();
1165         });
1166 });
1167
1168 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1169         expect(2);
1170         stop();
1171         jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1172         var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1173                 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1174                 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1175                 jQuery.ajaxSetup({ dataFilter: 0 });
1176                 start();
1177         });
1178 });
1179
1180 test("load(String, Object, Function)", function() {
1181         expect(2);
1182         stop();
1183
1184         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1185                 var $post = jQuery(this).find('#post');
1186                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1187                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1188                 start();
1189         });
1190 });
1191
1192 test("load(String, String, Function)", function() {
1193         expect(2);
1194         stop();
1195
1196         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1197                 var $get = jQuery(this).find('#get');
1198                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1199                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
1200                 start();
1201         });
1202 });
1203
1204 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1205         expect(2);
1206         stop();
1207         jQuery.get(url('data/dashboard.xml'), function(xml) {
1208                 var content = [];
1209                 jQuery('tab', xml).each(function() {
1210                         content.push(jQuery(this).text());
1211                 });
1212                 equals( content[0], 'blabla', 'Check first tab');
1213                 equals( content[1], 'blublu', 'Check second tab');
1214                 start();
1215         });
1216 });
1217
1218 test("jQuery.getScript(String, Function) - with callback", function() {
1219         expect(3);
1220         stop();
1221         jQuery.getScript(url("data/test.js"), function( data, _, jqXHR ) {
1222                 equals( foobar, "bar", 'Check if script was evaluated' );
1223                 strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script (#8082)" );
1224                 setTimeout(start, 100);
1225         });
1226 });
1227
1228 test("jQuery.getScript(String, Function) - no callback", function() {
1229         expect(1);
1230         stop();
1231         jQuery.getScript(url("data/test.js"), function(){
1232                 start();
1233         });
1234 });
1235
1236 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1237
1238         test("jQuery.ajax() - JSONP, " + label, function() {
1239                 expect(20);
1240
1241                 var count = 0;
1242                 function plus(){ if ( ++count == 18 ) start(); }
1243
1244                 stop();
1245
1246                 jQuery.ajax({
1247                         url: "data/jsonp.php",
1248                         dataType: "jsonp",
1249                         crossDomain: crossDomain,
1250                         success: function(data){
1251                                 ok( data.data, "JSON results returned (GET, no callback)" );
1252                                 plus();
1253                         },
1254                         error: function(data){
1255                                 ok( false, "Ajax error JSON (GET, no callback)" );
1256                                 plus();
1257                         }
1258                 });
1259
1260                 jQuery.ajax({
1261                         url: "data/jsonp.php?callback=?",
1262                         dataType: "jsonp",
1263                         crossDomain: crossDomain,
1264                         success: function(data){
1265                                 ok( data.data, "JSON results returned (GET, url callback)" );
1266                                 plus();
1267                         },
1268                         error: function(data){
1269                                 ok( false, "Ajax error JSON (GET, url callback)" );
1270                                 plus();
1271                         }
1272                 });
1273
1274                 jQuery.ajax({
1275                         url: "data/jsonp.php",
1276                         dataType: "jsonp",
1277                         crossDomain: crossDomain,
1278                         data: "callback=?",
1279                         success: function(data){
1280                                 ok( data.data, "JSON results returned (GET, data callback)" );
1281                                 plus();
1282                         },
1283                         error: function(data){
1284                                 ok( false, "Ajax error JSON (GET, data callback)" );
1285                                 plus();
1286                         }
1287                 });
1288
1289                 jQuery.ajax({
1290                         url: "data/jsonp.php?callback=??",
1291                         dataType: "jsonp",
1292                         crossDomain: crossDomain,
1293                         success: function(data){
1294                                 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1295                                 plus();
1296                         },
1297                         error: function(data){
1298                                 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1299                                 plus();
1300                         }
1301                 });
1302
1303                 jQuery.ajax({
1304                         url: "data/jsonp.php",
1305                         dataType: "jsonp",
1306                         crossDomain: crossDomain,
1307                         data: "callback=??",
1308                         success: function(data){
1309                                 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1310                                 plus();
1311                         },
1312                         error: function(data){
1313                                 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1314                                 plus();
1315                         }
1316                 });
1317
1318                 jQuery.ajax({
1319                         url: "data/jsonp.php/??",
1320                         dataType: "jsonp",
1321                         crossDomain: crossDomain,
1322                         success: function(data){
1323                                 ok( data.data, "JSON results returned (GET, REST-like)" );
1324                                 plus();
1325                         },
1326                         error: function(data){
1327                                 ok( false, "Ajax error JSON (GET, REST-like)" );
1328                                 plus();
1329                         }
1330                 });
1331
1332                 jQuery.ajax({
1333                         url: "data/jsonp.php/???json=1",
1334                         dataType: "jsonp",
1335                         crossDomain: crossDomain,
1336                         success: function(data){
1337                                 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1338                                 plus();
1339                         },
1340                         error: function(data){
1341                                 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1342                                 plus();
1343                         }
1344                 });
1345
1346                 jQuery.ajax({
1347                         url: "data/jsonp.php",
1348                         dataType: "jsonp",
1349                         crossDomain: crossDomain,
1350                         jsonp: "callback",
1351                         success: function(data){
1352                                 ok( data.data, "JSON results returned (GET, data obj callback)" );
1353                                 plus();
1354                         },
1355                         error: function(data){
1356                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1357                                 plus();
1358                         }
1359                 });
1360
1361                 window.jsonpResults = function(data) {
1362                         ok( data.data, "JSON results returned (GET, custom callback function)" );
1363                         window.jsonpResults = undefined;
1364                         plus();
1365                 };
1366
1367                 jQuery.ajax({
1368                         url: "data/jsonp.php",
1369                         dataType: "jsonp",
1370                         crossDomain: crossDomain,
1371                         jsonpCallback: "jsonpResults",
1372                         success: function(data){
1373                                 ok( data.data, "JSON results returned (GET, custom callback name)" );
1374                                 plus();
1375                         },
1376                         error: function(data){
1377                                 ok( false, "Ajax error JSON (GET, custom callback name)" );
1378                                 plus();
1379                         }
1380                 });
1381
1382                 jQuery.ajax({
1383                         url: "data/jsonp.php",
1384                         dataType: "jsonp",
1385                         crossDomain: crossDomain,
1386                         jsonpCallback: "functionToCleanUp",
1387                         success: function(data){
1388                                 ok( data.data, "JSON results returned (GET, custom callback name to be cleaned up)" );
1389                                 strictEqual( window.functionToCleanUp, undefined, "Callback was removed (GET, custom callback name to be cleaned up)" );
1390                                 plus();
1391                                 var xhr;
1392                                 jQuery.ajax({
1393                                         url: "data/jsonp.php",
1394                                         dataType: "jsonp",
1395                                         crossDomain: crossDomain,
1396                                         jsonpCallback: "functionToCleanUp",
1397                                         beforeSend: function( jqXHR ) {
1398                                                 xhr = jqXHR;
1399                                                 return false;
1400                                         }
1401                                 });
1402                                 xhr.error(function() {
1403                                         ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1404                                         strictEqual( window.functionToCleanUp, undefined, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1405                                         plus();
1406                                 });
1407                         },
1408                         error: function(data){
1409                                 ok( false, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1410                                 plus();
1411                         }
1412                 });
1413
1414                 jQuery.ajax({
1415                         type: "POST",
1416                         url: "data/jsonp.php",
1417                         dataType: "jsonp",
1418                         crossDomain: crossDomain,
1419                         success: function(data){
1420                                 ok( data.data, "JSON results returned (POST, no callback)" );
1421                                 plus();
1422                         },
1423                         error: function(data){
1424                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1425                                 plus();
1426                         }
1427                 });
1428
1429                 jQuery.ajax({
1430                         type: "POST",
1431                         url: "data/jsonp.php",
1432                         data: "callback=?",
1433                         dataType: "jsonp",
1434                         crossDomain: crossDomain,
1435                         success: function(data){
1436                                 ok( data.data, "JSON results returned (POST, data callback)" );
1437                                 plus();
1438                         },
1439                         error: function(data){
1440                                 ok( false, "Ajax error JSON (POST, data callback)" );
1441                                 plus();
1442                         }
1443                 });
1444
1445                 jQuery.ajax({
1446                         type: "POST",
1447                         url: "data/jsonp.php",
1448                         jsonp: "callback",
1449                         dataType: "jsonp",
1450                         crossDomain: crossDomain,
1451                         success: function(data){
1452                                 ok( data.data, "JSON results returned (POST, data obj callback)" );
1453                                 plus();
1454                         },
1455                         error: function(data){
1456                                 ok( false, "Ajax error JSON (POST, data obj callback)" );
1457                                 plus();
1458                         }
1459                 });
1460
1461                 //#7578
1462                 jQuery.ajax({
1463                         url: "data/jsonp.php",
1464                         dataType: "jsonp",
1465                         crossDomain: crossDomain,
1466                         beforeSend: function(){
1467                                 strictEqual( this.cache, false, "cache must be false on JSON request" );
1468                                 plus();
1469                                 return false;
1470                         }
1471                 });
1472
1473                 jQuery.ajax({
1474                         url: "data/jsonp.php?callback=XXX",
1475                         dataType: "jsonp",
1476                         jsonp: false,
1477                         jsonpCallback: "XXX",
1478                         crossDomain: crossDomain,
1479                         beforeSend: function() {
1480                                 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1481                                         "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1482                                 plus();
1483                         },
1484                         success: function(data){
1485                                 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1486                                 plus();
1487                         },
1488                         error: function(data){
1489                                 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1490                                 plus();
1491                         }
1492                 });
1493
1494         });
1495 });
1496
1497 test("jQuery.ajax() - script, Remote", function() {
1498         expect(2);
1499
1500         var base = window.location.href.replace(/[^\/]*$/, "");
1501
1502         stop();
1503
1504         jQuery.ajax({
1505                 url: base + "data/test.js",
1506                 dataType: "script",
1507                 success: function(data){
1508                         ok( foobar, "Script results returned (GET, no callback)" );
1509                         start();
1510                 }
1511         });
1512 });
1513
1514 test("jQuery.ajax() - script, Remote with POST", function() {
1515         expect(3);
1516
1517         var base = window.location.href.replace(/[^\/]*$/, "");
1518
1519         stop();
1520
1521         jQuery.ajax({
1522                 url: base + "data/test.js",
1523                 type: "POST",
1524                 dataType: "script",
1525                 success: function(data, status){
1526                         ok( foobar, "Script results returned (POST, no callback)" );
1527                         equals( status, "success", "Script results returned (POST, no callback)" );
1528                         start();
1529                 },
1530                 error: function(xhr) {
1531                         ok( false, "ajax error, status code: " + xhr.status );
1532                         start();
1533                 }
1534         });
1535 });
1536
1537 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1538         expect(2);
1539
1540         var base = window.location.href.replace(/[^\/]*$/, "");
1541         base = base.replace(/^.*?\/\//, "//");
1542
1543         stop();
1544
1545         jQuery.ajax({
1546                 url: base + "data/test.js",
1547                 dataType: "script",
1548                 success: function(data){
1549                         ok( foobar, "Script results returned (GET, no callback)" );
1550                         start();
1551                 }
1552         });
1553 });
1554
1555 test("jQuery.ajax() - malformed JSON", function() {
1556         expect(2);
1557
1558         stop();
1559
1560         jQuery.ajax({
1561                 url: "data/badjson.js",
1562                 dataType: "json",
1563                 success: function(){
1564                         ok( false, "Success." );
1565                         start();
1566                 },
1567                 error: function(xhr, msg, detailedMsg) {
1568                         equals( "parsererror", msg, "A parse error occurred." );
1569                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1570                         start();
1571                 }
1572         });
1573 });
1574
1575 test("jQuery.ajax() - script by content-type", function() {
1576         expect(2);
1577
1578         stop();
1579
1580         jQuery.when(
1581
1582                 jQuery.ajax({
1583                         url: "data/script.php",
1584                         data: { header: "script" }
1585                 }),
1586
1587                 jQuery.ajax({
1588                         url: "data/script.php",
1589                         data: { header: "ecma" }
1590                 })
1591
1592         ).then( start, start );
1593 });
1594
1595 test("jQuery.ajax() - json by content-type", function() {
1596         expect(5);
1597
1598         stop();
1599
1600         jQuery.ajax({
1601                 url: "data/json.php",
1602                 data: { header: "json", json: "array" },
1603                 success: function( json ) {
1604                         ok( json.length >= 2, "Check length");
1605                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1606                         equals( json[0].age, 21, 'Check JSON: first, age' );
1607                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1608                         equals( json[1].age, 25, 'Check JSON: second, age' );
1609                         start();
1610                 }
1611         });
1612 });
1613
1614 test("jQuery.ajax() - json by content-type disabled with options", function() {
1615         expect(6);
1616
1617         stop();
1618
1619         jQuery.ajax({
1620                 url: url("data/json.php"),
1621                 data: { header: "json", json: "array" },
1622                 contents: {
1623                         json: false
1624                 },
1625                 success: function( text ) {
1626                         equals( typeof text , "string" , "json wasn't auto-determined" );
1627                         var json = jQuery.parseJSON( text );
1628                         ok( json.length >= 2, "Check length");
1629                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1630                         equals( json[0].age, 21, 'Check JSON: first, age' );
1631                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1632                         equals( json[1].age, 25, 'Check JSON: second, age' );
1633                         start();
1634                 }
1635         });
1636 });
1637
1638 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1639         expect(5);
1640         stop();
1641         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1642           ok( json.length >= 2, "Check length");
1643           equals( json[0].name, 'John', 'Check JSON: first, name' );
1644           equals( json[0].age, 21, 'Check JSON: first, age' );
1645           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1646           equals( json[1].age, 25, 'Check JSON: second, age' );
1647           start();
1648         });
1649 });
1650
1651 test("jQuery.getJSON(String, Function) - JSON object", function() {
1652         expect(2);
1653         stop();
1654         jQuery.getJSON(url("data/json.php"), function(json) {
1655           if (json && json.data) {
1656                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1657                   equals( json.data.length, 25, 'Check JSON: length' );
1658           }
1659           start();
1660         });
1661 });
1662
1663 test("jQuery.getJSON - Using Native JSON", function() {
1664         expect(2);
1665
1666         var old = window.JSON;
1667         JSON = {
1668                 parse: function(str){
1669                         ok( true, "Verifying that parse method was run" );
1670                         return true;
1671                 }
1672         };
1673
1674         stop();
1675         jQuery.getJSON(url("data/json.php"), function(json) {
1676                 window.JSON = old;
1677                 equals( json, true, "Verifying return value" );
1678                 start();
1679         });
1680 });
1681
1682 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1683         expect(2);
1684
1685         var base = window.location.href.replace(/[^\/]*$/, "");
1686
1687         stop();
1688         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1689           equals( json.data.lang, 'en', 'Check JSON: lang' );
1690           equals( json.data.length, 25, 'Check JSON: length' );
1691           start();
1692         });
1693 });
1694
1695 test("jQuery.post - data", 3, function() {
1696         stop();
1697
1698         jQuery.when(
1699                 jQuery.post( url( "data/name.php" ), { xml: "5-2", length: 3 }, function( xml ) {
1700                         jQuery( 'math', xml ).each( function() {
1701                                 equals( jQuery( 'calculation', this ).text(), '5-2', 'Check for XML' );
1702                                 equals( jQuery( 'result', this ).text(), '3', 'Check for XML' );
1703                         });
1704                 }),
1705
1706                 jQuery.ajax({
1707                         url: url('data/echoData.php'),
1708                         type: "POST",
1709                         data: {
1710                                 'test': {
1711                                         'length': 7,
1712                                                 'foo': 'bar'
1713                                 }
1714                         },
1715                         success: function( data ) {
1716                                 strictEqual( data, 'test%5Blength%5D=7&test%5Bfoo%5D=bar', 'Check if a sub-object with a length param is serialized correctly');
1717                         }
1718                 })
1719         ).then( start, start );
1720
1721 });
1722
1723 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1724         expect(4);
1725         stop();
1726         var done = 0;
1727
1728         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1729           jQuery('math', xml).each(function() {
1730                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1731                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1732                  });
1733           if ( ++done === 2 ) start();
1734         });
1735
1736         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1737           jQuery('math', xml).each(function() {
1738                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1739                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1740                  });
1741           if ( ++done === 2 ) start();
1742         });
1743 });
1744
1745 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1746         stop();
1747
1748         var passed = 0;
1749
1750         jQuery.ajaxSetup({timeout: 1000});
1751
1752         var pass = function() {
1753                 passed++;
1754                 if ( passed == 2 ) {
1755                         ok( true, 'Check local and global callbacks after timeout' );
1756                         jQuery('#main').unbind("ajaxError");
1757                         start();
1758                 }
1759         };
1760
1761         var fail = function(a,b,c) {
1762                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1763                 start();
1764         };
1765
1766         jQuery('#main').ajaxError(pass);
1767
1768         jQuery.ajax({
1769           type: "GET",
1770           url: url("data/name.php?wait=5"),
1771           error: pass,
1772           success: fail
1773         });
1774
1775         // reset timeout
1776         jQuery.ajaxSetup({timeout: 0});
1777 });
1778
1779 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1780         stop();
1781         jQuery.ajaxSetup({timeout: 50});
1782
1783         jQuery.ajax({
1784           type: "GET",
1785           timeout: 15000,
1786           url: url("data/name.php?wait=1"),
1787           error: function() {
1788                    ok( false, 'Check for local timeout failed' );
1789                    start();
1790           },
1791           success: function() {
1792                 ok( true, 'Check for local timeout' );
1793                 start();
1794           }
1795         });
1796
1797         // reset timeout
1798         jQuery.ajaxSetup({timeout: 0});
1799 });
1800
1801 test("jQuery.ajax - simple get", function() {
1802         expect(1);
1803         stop();
1804         jQuery.ajax({
1805           type: "GET",
1806           url: url("data/name.php?name=foo"),
1807           success: function(msg){
1808                 equals( msg, 'bar', 'Check for GET' );
1809                 start();
1810           }
1811         });
1812 });
1813
1814 test("jQuery.ajax - simple post", function() {
1815         expect(1);
1816         stop();
1817         jQuery.ajax({
1818           type: "POST",
1819           url: url("data/name.php"),
1820           data: "name=peter",
1821           success: function(msg){
1822                 equals( msg, 'pan', 'Check for POST' );
1823                 start();
1824           }
1825         });
1826 });
1827
1828 test("ajaxSetup()", function() {
1829         expect(1);
1830         stop();
1831         jQuery.ajaxSetup({
1832                 url: url("data/name.php?name=foo"),
1833                 success: function(msg){
1834                         equals( msg, 'bar', 'Check for GET' );
1835                         start();
1836                 }
1837         });
1838         jQuery.ajax();
1839 });
1840
1841 /*
1842 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1843         stop();
1844         jQuery.ajax({
1845                 url: "data/name.php?wait=1",
1846                 timeout: 500,
1847                 error: function(request, status) {
1848                         ok( status != null, "status shouldn't be null in error handler" );
1849                         equals( "timeout", status );
1850                         start();
1851                 }
1852         });
1853 });
1854 */
1855
1856 test("data option: evaluate function values (#2806)", function() {
1857         stop();
1858         jQuery.ajax({
1859                 url: "data/echoQuery.php",
1860                 data: {
1861                         key: function() {
1862                                 return "value";
1863                         }
1864                 },
1865                 success: function(result) {
1866                         equals( result, "key=value" );
1867                         start();
1868                 }
1869         });
1870 });
1871
1872 test("data option: empty bodies for non-GET requests", function() {
1873         stop();
1874         jQuery.ajax({
1875                 url: "data/echoData.php",
1876                 data: undefined,
1877                 type: "post",
1878                 success: function(result) {
1879                         equals( result, "" );
1880                         start();
1881                 }
1882         });
1883 });
1884
1885 var ifModifiedNow = new Date();
1886
1887 jQuery.each( { " (cache)": true, " (no cache)": false }, function( label, cache ) {
1888
1889         test("jQuery.ajax - If-Modified-Since support" + label, function() {
1890                 expect( 3 );
1891
1892                 stop();
1893
1894                 var url = "data/if_modified_since.php?ts=" + ifModifiedNow++;
1895
1896                 jQuery.ajax({
1897                         url: url,
1898                         ifModified: true,
1899                         cache: cache,
1900                         success: function(data, status) {
1901                                 equals(status, "success" );
1902
1903                                 jQuery.ajax({
1904                                         url: url,
1905                                         ifModified: true,
1906                                         cache: cache,
1907                                         success: function(data, status) {
1908                                                 if ( data === "FAIL" ) {
1909                                                         ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1910                                                         ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1911                                                 } else {
1912                                                         equals(status, "notmodified");
1913                                                         ok(data == null, "response body should be empty");
1914                                                 }
1915                                                 start();
1916                                 },
1917                                         error: function() {
1918                                                 // Do this because opera simply refuses to implement 304 handling :(
1919                                                 // A feature-driven way of detecting this would be appreciated
1920                                                 // See: http://gist.github.com/599419
1921                                                 ok(jQuery.browser.opera, "error");
1922                                                 ok(jQuery.browser.opera, "error");
1923                                                 start();
1924                                         }
1925                                 });
1926                         },
1927                         error: function() {
1928                                 equals(false, "error");
1929                                 // Do this because opera simply refuses to implement 304 handling :(
1930                                 // A feature-driven way of detecting this would be appreciated
1931                                 // See: http://gist.github.com/599419
1932                                 ok(jQuery.browser.opera, "error");
1933                                 start();
1934                         }
1935                 });
1936         });
1937
1938         test("jQuery.ajax - Etag support" + label, function() {
1939                 expect( 3 );
1940
1941                 stop();
1942
1943                 var url = "data/etag.php?ts=" + ifModifiedNow++;
1944
1945                 jQuery.ajax({
1946                         url: url,
1947                         ifModified: true,
1948                         cache: cache,
1949                         success: function(data, status) {
1950                                 equals(status, "success" );
1951
1952                                 jQuery.ajax({
1953                                         url: url,
1954                                         ifModified: true,
1955                                         cache: cache,
1956                                         success: function(data, status) {
1957                                                 if ( data === "FAIL" ) {
1958                                                         ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1959                                                         ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1960                                                 } else {
1961                                                         equals(status, "notmodified");
1962                                                         ok(data == null, "response body should be empty");
1963                                                 }
1964                                                 start();
1965                                 },
1966                                 error: function() {
1967                                                 // Do this because opera simply refuses to implement 304 handling :(
1968                                                 // A feature-driven way of detecting this would be appreciated
1969                                                 // See: http://gist.github.com/599419
1970                                                 ok(jQuery.browser.opera, "error");
1971                                                 ok(jQuery.browser.opera, "error");
1972                                                 start();
1973                                         }
1974                                 });
1975                         },
1976                         error: function() {
1977                                 // Do this because opera simply refuses to implement 304 handling :(
1978                                 // A feature-driven way of detecting this would be appreciated
1979                                 // See: http://gist.github.com/599419
1980                                 ok(jQuery.browser.opera, "error");
1981                                 start();
1982                         }
1983                 });
1984         });
1985 });
1986
1987 test("jQuery ajax - failing cross-domain", function() {
1988
1989         expect( 2 );
1990
1991         stop();
1992
1993         var i = 2;
1994
1995         jQuery.ajax({
1996                 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1997                 success: function(){ ok( false , "success" ); },
1998                 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1999                 complete: function() { if ( ! --i ) start(); }
2000         });
2001
2002         jQuery.ajax({
2003                 url: 'http://www.google.com',
2004                 success: function(){ ok( false , "success" ); },
2005                 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
2006                 complete: function() { if ( ! --i ) start(); }
2007         });
2008
2009 });
2010
2011 test("jQuery ajax - atom+xml", function() {
2012
2013         stop();
2014
2015         jQuery.ajax({
2016                 url: url( 'data/atom+xml.php' ),
2017                 success: function(){ ok( true , "success" ); },
2018                 error: function(){ ok( false , "error" ); },
2019                 complete: function() { start(); }
2020         });
2021
2022 });
2023
2024 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
2025         var success = false;
2026         try {
2027                 var xhr = jQuery.ajax({ url: window.location });
2028                 success = true;
2029                 xhr.abort();
2030         } catch (e) {}
2031
2032         ok( success, "document.location did not generate exception" );
2033 });
2034
2035 test( "jQuery.ajax - statusCode" , function() {
2036
2037         var count = 12;
2038
2039         expect( 20 );
2040         stop();
2041
2042         function countComplete() {
2043                 if ( ! --count ) {
2044                         start();
2045                 }
2046         }
2047
2048         function createStatusCodes( name , isSuccess ) {
2049                 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
2050                 return {
2051                         200: function() {
2052                                 ok( isSuccess , name );
2053                         },
2054                         404: function() {
2055                                 ok( ! isSuccess , name );
2056                         }
2057                 };
2058         }
2059
2060         jQuery.each( {
2061                 "data/name.html": true,
2062                 "data/someFileThatDoesNotExist.html": false
2063         } , function( uri , isSuccess ) {
2064
2065                 jQuery.ajax( url( uri ) , {
2066                         statusCode: createStatusCodes( "in options" , isSuccess ),
2067                         complete: countComplete
2068                 });
2069
2070                 jQuery.ajax( url( uri ) , {
2071                         complete: countComplete
2072                 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
2073
2074                 jQuery.ajax( url( uri ) , {
2075                         complete: function(jqXHR) {
2076                                 jqXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
2077                                 countComplete();
2078                         }
2079                 });
2080
2081                 jQuery.ajax( url( uri ) , {
2082                         complete: function(jqXHR) {
2083                                 setTimeout( function() {
2084                                         jqXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
2085                                         countComplete();
2086                                 } , 100 );
2087                         }
2088                 });
2089
2090                 jQuery.ajax( url( uri ) , {
2091                         statusCode: createStatusCodes( "all (options)" , isSuccess ),
2092                         complete: function(jqXHR) {
2093                                 jqXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2094                                 setTimeout( function() {
2095                                         jqXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2096                                         countComplete();
2097                                 } , 100 );
2098                         }
2099                 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2100
2101                 var testString = "";
2102
2103                 jQuery.ajax( url( uri ), {
2104                         success: function( a , b , jqXHR ) {
2105                                 ok( isSuccess , "success" );
2106                                 var statusCode = {};
2107                                 statusCode[ jqXHR.status ] = function() {
2108                                         testString += "B";
2109                                 };
2110                                 jqXHR.statusCode( statusCode );
2111                                 testString += "A";
2112                         },
2113                         error: function( jqXHR ) {
2114                                 ok( ! isSuccess , "error" );
2115                                 var statusCode = {};
2116                                 statusCode[ jqXHR.status ] = function() {
2117                                         testString += "B";
2118                                 };
2119                                 jqXHR.statusCode( statusCode );
2120                                 testString += "A";
2121                         },
2122                         complete: function() {
2123                                 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2124                                                 ( isSuccess ? "success" :  "error" ) + " callbacks" );
2125                                 countComplete();
2126                         }
2127                 } );
2128
2129         });
2130 });
2131
2132 test("jQuery.ajax - transitive conversions", function() {
2133
2134         expect( 8 );
2135
2136         stop();
2137
2138         jQuery.when(
2139
2140                 jQuery.ajax( url("data/json.php") , {
2141                         converters: {
2142                                 "json myJson": function( data ) {
2143                                         ok( true , "converter called" );
2144                                         return data;
2145                                 }
2146                         },
2147                         dataType: "myJson",
2148                         success: function() {
2149                                 ok( true , "Transitive conversion worked" );
2150                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2151                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2152                         }
2153                 }),
2154
2155                 jQuery.ajax( url("data/json.php") , {
2156                         converters: {
2157                                 "json myJson": function( data ) {
2158                                         ok( true , "converter called (*)" );
2159                                         return data;
2160                                 }
2161                         },
2162                         contents: false, /* headers are wrong so we ignore them */
2163                         dataType: "* myJson",
2164                         success: function() {
2165                                 ok( true , "Transitive conversion worked (*)" );
2166                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2167                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2168                         }
2169                 })
2170
2171         ).then( start , start );
2172
2173 });
2174
2175 test("jQuery.ajax - active counter", function() {
2176     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
2177 });
2178
2179 }
2180
2181 //}