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