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