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