Fixes #5856. Adds document protocol at the beginning of URLs without protocol (thanks...
[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(2);
1196         stop();
1197         jQuery.getScript(url("data/test.js"), function() {
1198                 equals( foobar, "bar", 'Check if script was evaluated' );
1199                 setTimeout(start, 100);
1200         });
1201 });
1202
1203 test("jQuery.getScript(String, Function) - no callback", function() {
1204         expect(1);
1205         stop();
1206         jQuery.getScript(url("data/test.js"), function(){
1207                 start();
1208         });
1209 });
1210
1211 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1212
1213         test("jQuery.ajax() - JSONP, " + label, function() {
1214                 expect(16);
1215
1216                 var count = 0;
1217                 function plus(){ if ( ++count == 16 ) start(); }
1218
1219                 stop();
1220
1221                 jQuery.ajax({
1222                         url: "data/jsonp.php",
1223                         dataType: "jsonp",
1224                         crossDomain: crossDomain,
1225                         success: function(data){
1226                                 ok( data.data, "JSON results returned (GET, no callback)" );
1227                                 plus();
1228                         },
1229                         error: function(data){
1230                                 ok( false, "Ajax error JSON (GET, no callback)" );
1231                                 plus();
1232                         }
1233                 });
1234
1235                 jQuery.ajax({
1236                         url: "data/jsonp.php?callback=?",
1237                         dataType: "jsonp",
1238                         crossDomain: crossDomain,
1239                         success: function(data){
1240                                 ok( data.data, "JSON results returned (GET, url callback)" );
1241                                 plus();
1242                         },
1243                         error: function(data){
1244                                 ok( false, "Ajax error JSON (GET, url callback)" );
1245                                 plus();
1246                         }
1247                 });
1248
1249                 jQuery.ajax({
1250                         url: "data/jsonp.php",
1251                         dataType: "jsonp",
1252                         crossDomain: crossDomain,
1253                         data: "callback=?",
1254                         success: function(data){
1255                                 ok( data.data, "JSON results returned (GET, data callback)" );
1256                                 plus();
1257                         },
1258                         error: function(data){
1259                                 ok( false, "Ajax error JSON (GET, data callback)" );
1260                                 plus();
1261                         }
1262                 });
1263
1264                 jQuery.ajax({
1265                         url: "data/jsonp.php?callback=??",
1266                         dataType: "jsonp",
1267                         crossDomain: crossDomain,
1268                         success: function(data){
1269                                 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1270                                 plus();
1271                         },
1272                         error: function(data){
1273                                 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1274                                 plus();
1275                         }
1276                 });
1277
1278                 jQuery.ajax({
1279                         url: "data/jsonp.php",
1280                         dataType: "jsonp",
1281                         crossDomain: crossDomain,
1282                         data: "callback=??",
1283                         success: function(data){
1284                                 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1285                                 plus();
1286                         },
1287                         error: function(data){
1288                                 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1289                                 plus();
1290                         }
1291                 });
1292
1293                 jQuery.ajax({
1294                         url: "data/jsonp.php/??",
1295                         dataType: "jsonp",
1296                         crossDomain: crossDomain,
1297                         success: function(data){
1298                                 ok( data.data, "JSON results returned (GET, REST-like)" );
1299                                 plus();
1300                         },
1301                         error: function(data){
1302                                 ok( false, "Ajax error JSON (GET, REST-like)" );
1303                                 plus();
1304                         }
1305                 });
1306
1307                 jQuery.ajax({
1308                         url: "data/jsonp.php/???json=1",
1309                         dataType: "jsonp",
1310                         crossDomain: crossDomain,
1311                         success: function(data){
1312                                 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1313                                 plus();
1314                         },
1315                         error: function(data){
1316                                 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1317                                 plus();
1318                         }
1319                 });
1320
1321                 jQuery.ajax({
1322                         url: "data/jsonp.php",
1323                         dataType: "jsonp",
1324                         crossDomain: crossDomain,
1325                         jsonp: "callback",
1326                         success: function(data){
1327                                 ok( data.data, "JSON results returned (GET, data obj callback)" );
1328                                 plus();
1329                         },
1330                         error: function(data){
1331                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1332                                 plus();
1333                         }
1334                 });
1335
1336                 window.jsonpResults = function(data) {
1337                         ok( data.data, "JSON results returned (GET, custom callback function)" );
1338                         window.jsonpResults = undefined;
1339                         plus();
1340                 };
1341
1342                 jQuery.ajax({
1343                         url: "data/jsonp.php",
1344                         dataType: "jsonp",
1345                         crossDomain: crossDomain,
1346                         jsonpCallback: "jsonpResults",
1347                         success: function(data){
1348                                 ok( data.data, "JSON results returned (GET, custom callback name)" );
1349                                 plus();
1350                         },
1351                         error: function(data){
1352                                 ok( false, "Ajax error JSON (GET, custom callback name)" );
1353                                 plus();
1354                         }
1355                 });
1356
1357                 jQuery.ajax({
1358                         type: "POST",
1359                         url: "data/jsonp.php",
1360                         dataType: "jsonp",
1361                         crossDomain: crossDomain,
1362                         success: function(data){
1363                                 ok( data.data, "JSON results returned (POST, no callback)" );
1364                                 plus();
1365                         },
1366                         error: function(data){
1367                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1368                                 plus();
1369                         }
1370                 });
1371
1372                 jQuery.ajax({
1373                         type: "POST",
1374                         url: "data/jsonp.php",
1375                         data: "callback=?",
1376                         dataType: "jsonp",
1377                         crossDomain: crossDomain,
1378                         success: function(data){
1379                                 ok( data.data, "JSON results returned (POST, data callback)" );
1380                                 plus();
1381                         },
1382                         error: function(data){
1383                                 ok( false, "Ajax error JSON (POST, data callback)" );
1384                                 plus();
1385                         }
1386                 });
1387
1388                 jQuery.ajax({
1389                         type: "POST",
1390                         url: "data/jsonp.php",
1391                         jsonp: "callback",
1392                         dataType: "jsonp",
1393                         crossDomain: crossDomain,
1394                         success: function(data){
1395                                 ok( data.data, "JSON results returned (POST, data obj callback)" );
1396                                 plus();
1397                         },
1398                         error: function(data){
1399                                 ok( false, "Ajax error JSON (POST, data obj callback)" );
1400                                 plus();
1401                         }
1402                 });
1403
1404                 //#7578
1405                 jQuery.ajax({
1406                         url: "data/jsonp.php",
1407                         dataType: "jsonp",
1408                         crossDomain: crossDomain,
1409                         beforeSend: function(){
1410                                 strictEqual( this.cache, false, "cache must be false on JSON request" );
1411                                 plus();
1412                                 return false;
1413                         }
1414                 });
1415
1416                 jQuery.ajax({
1417                         url: "data/jsonp.php?callback=XXX",
1418                         dataType: "jsonp",
1419                         jsonp: false,
1420                         jsonpCallback: "XXX",
1421                         crossDomain: crossDomain,
1422                         beforeSend: function() {
1423                                 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1424                                         "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1425                                 plus();
1426                         },
1427                         success: function(data){
1428                                 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1429                                 plus();
1430                         },
1431                         error: function(data){
1432                                 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1433                                 plus();
1434                         }
1435                 });
1436
1437         });
1438 });
1439
1440 test("jQuery.ajax() - script, Remote", function() {
1441         expect(2);
1442
1443         var base = window.location.href.replace(/[^\/]*$/, "");
1444
1445         stop();
1446
1447         jQuery.ajax({
1448                 url: base + "data/test.js",
1449                 dataType: "script",
1450                 success: function(data){
1451                         ok( foobar, "Script results returned (GET, no callback)" );
1452                         start();
1453                 }
1454         });
1455 });
1456
1457 test("jQuery.ajax() - script, Remote with POST", function() {
1458         expect(3);
1459
1460         var base = window.location.href.replace(/[^\/]*$/, "");
1461
1462         stop();
1463
1464         jQuery.ajax({
1465                 url: base + "data/test.js",
1466                 type: "POST",
1467                 dataType: "script",
1468                 success: function(data, status){
1469                         ok( foobar, "Script results returned (POST, no callback)" );
1470                         equals( status, "success", "Script results returned (POST, no callback)" );
1471                         start();
1472                 },
1473                 error: function(xhr) {
1474                         ok( false, "ajax error, status code: " + xhr.status );
1475                         start();
1476                 }
1477         });
1478 });
1479
1480 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1481         expect(2);
1482
1483         var base = window.location.href.replace(/[^\/]*$/, "");
1484         base = base.replace(/^.*?\/\//, "//");
1485
1486         stop();
1487
1488         jQuery.ajax({
1489                 url: base + "data/test.js",
1490                 dataType: "script",
1491                 success: function(data){
1492                         ok( foobar, "Script results returned (GET, no callback)" );
1493                         start();
1494                 }
1495         });
1496 });
1497
1498 test("jQuery.ajax() - malformed JSON", function() {
1499         expect(2);
1500
1501         stop();
1502
1503         jQuery.ajax({
1504                 url: "data/badjson.js",
1505                 dataType: "json",
1506                 success: function(){
1507                         ok( false, "Success." );
1508                         start();
1509                 },
1510                 error: function(xhr, msg, detailedMsg) {
1511                         equals( "parsererror", msg, "A parse error occurred." );
1512                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1513                         start();
1514                 }
1515         });
1516 });
1517
1518 test("jQuery.ajax() - script by content-type", function() {
1519         expect(1);
1520
1521         stop();
1522
1523         jQuery.ajax({
1524                 url: "data/script.php",
1525                 data: { header: "script" },
1526                 success: function() {
1527                         start();
1528                 }
1529         });
1530 });
1531
1532 test("jQuery.ajax() - json by content-type", function() {
1533         expect(5);
1534
1535         stop();
1536
1537         jQuery.ajax({
1538                 url: "data/json.php",
1539                 data: { header: "json", json: "array" },
1540                 success: function( json ) {
1541                         ok( json.length >= 2, "Check length");
1542                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1543                         equals( json[0].age, 21, 'Check JSON: first, age' );
1544                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1545                         equals( json[1].age, 25, 'Check JSON: second, age' );
1546                         start();
1547                 }
1548         });
1549 });
1550
1551 test("jQuery.ajax() - json by content-type disabled with options", function() {
1552         expect(6);
1553
1554         stop();
1555
1556         jQuery.ajax({
1557                 url: url("data/json.php"),
1558                 data: { header: "json", json: "array" },
1559                 contents: {
1560                         json: false
1561                 },
1562                 success: function( text ) {
1563                         equals( typeof text , "string" , "json wasn't auto-determined" );
1564                         var json = jQuery.parseJSON( text );
1565                         ok( json.length >= 2, "Check length");
1566                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1567                         equals( json[0].age, 21, 'Check JSON: first, age' );
1568                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1569                         equals( json[1].age, 25, 'Check JSON: second, age' );
1570                         start();
1571                 }
1572         });
1573 });
1574
1575 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1576         expect(5);
1577         stop();
1578         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1579           ok( json.length >= 2, "Check length");
1580           equals( json[0].name, 'John', 'Check JSON: first, name' );
1581           equals( json[0].age, 21, 'Check JSON: first, age' );
1582           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1583           equals( json[1].age, 25, 'Check JSON: second, age' );
1584           start();
1585         });
1586 });
1587
1588 test("jQuery.getJSON(String, Function) - JSON object", function() {
1589         expect(2);
1590         stop();
1591         jQuery.getJSON(url("data/json.php"), function(json) {
1592           if (json && json.data) {
1593                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1594                   equals( json.data.length, 25, 'Check JSON: length' );
1595           }
1596           start();
1597         });
1598 });
1599
1600 test("jQuery.getJSON - Using Native JSON", function() {
1601         expect(2);
1602
1603         var old = window.JSON;
1604         JSON = {
1605                 parse: function(str){
1606                         ok( true, "Verifying that parse method was run" );
1607                         return true;
1608                 }
1609         };
1610
1611         stop();
1612         jQuery.getJSON(url("data/json.php"), function(json) {
1613                 window.JSON = old;
1614                 equals( json, true, "Verifying return value" );
1615                 start();
1616         });
1617 });
1618
1619 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1620         expect(2);
1621
1622         var base = window.location.href.replace(/[^\/]*$/, "");
1623
1624         stop();
1625         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1626           equals( json.data.lang, 'en', 'Check JSON: lang' );
1627           equals( json.data.length, 25, 'Check JSON: length' );
1628           start();
1629         });
1630 });
1631
1632 test("jQuery.post - data", function() {
1633         expect(2);
1634         stop();
1635
1636         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1637                 jQuery('math', xml).each(function() {
1638                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1639                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1640                 });
1641                 start();
1642         });
1643 });
1644
1645 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1646         expect(4);
1647         stop();
1648         var done = 0;
1649
1650         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1651           jQuery('math', xml).each(function() {
1652                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1653                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1654                  });
1655           if ( ++done === 2 ) start();
1656         });
1657
1658         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1659           jQuery('math', xml).each(function() {
1660                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1661                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1662                  });
1663           if ( ++done === 2 ) start();
1664         });
1665 });
1666
1667 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1668         stop();
1669
1670         var passed = 0;
1671
1672         jQuery.ajaxSetup({timeout: 1000});
1673
1674         var pass = function() {
1675                 passed++;
1676                 if ( passed == 2 ) {
1677                         ok( true, 'Check local and global callbacks after timeout' );
1678                         jQuery('#main').unbind("ajaxError");
1679                         start();
1680                 }
1681         };
1682
1683         var fail = function(a,b,c) {
1684                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1685                 start();
1686         };
1687
1688         jQuery('#main').ajaxError(pass);
1689
1690         jQuery.ajax({
1691           type: "GET",
1692           url: url("data/name.php?wait=5"),
1693           error: pass,
1694           success: fail
1695         });
1696
1697         // reset timeout
1698         jQuery.ajaxSetup({timeout: 0});
1699 });
1700
1701 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1702         stop();
1703         jQuery.ajaxSetup({timeout: 50});
1704
1705         jQuery.ajax({
1706           type: "GET",
1707           timeout: 15000,
1708           url: url("data/name.php?wait=1"),
1709           error: function() {
1710                    ok( false, 'Check for local timeout failed' );
1711                    start();
1712           },
1713           success: function() {
1714                 ok( true, 'Check for local timeout' );
1715                 start();
1716           }
1717         });
1718
1719         // reset timeout
1720         jQuery.ajaxSetup({timeout: 0});
1721 });
1722
1723 test("jQuery.ajax - simple get", function() {
1724         expect(1);
1725         stop();
1726         jQuery.ajax({
1727           type: "GET",
1728           url: url("data/name.php?name=foo"),
1729           success: function(msg){
1730                 equals( msg, 'bar', 'Check for GET' );
1731                 start();
1732           }
1733         });
1734 });
1735
1736 test("jQuery.ajax - simple post", function() {
1737         expect(1);
1738         stop();
1739         jQuery.ajax({
1740           type: "POST",
1741           url: url("data/name.php"),
1742           data: "name=peter",
1743           success: function(msg){
1744                 equals( msg, 'pan', 'Check for POST' );
1745                 start();
1746           }
1747         });
1748 });
1749
1750 test("ajaxSetup()", function() {
1751         expect(1);
1752         stop();
1753         jQuery.ajaxSetup({
1754                 url: url("data/name.php?name=foo"),
1755                 success: function(msg){
1756                         equals( msg, 'bar', 'Check for GET' );
1757                         start();
1758                 }
1759         });
1760         jQuery.ajax();
1761 });
1762
1763 /*
1764 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1765         stop();
1766         jQuery.ajax({
1767                 url: "data/name.php?wait=1",
1768                 timeout: 500,
1769                 error: function(request, status) {
1770                         ok( status != null, "status shouldn't be null in error handler" );
1771                         equals( "timeout", status );
1772                         start();
1773                 }
1774         });
1775 });
1776 */
1777
1778 test("data option: evaluate function values (#2806)", function() {
1779         stop();
1780         jQuery.ajax({
1781                 url: "data/echoQuery.php",
1782                 data: {
1783                         key: function() {
1784                                 return "value";
1785                         }
1786                 },
1787                 success: function(result) {
1788                         equals( result, "key=value" );
1789                         start();
1790                 }
1791         });
1792 });
1793
1794 test("data option: empty bodies for non-GET requests", function() {
1795         stop();
1796         jQuery.ajax({
1797                 url: "data/echoData.php",
1798                 data: undefined,
1799                 type: "post",
1800                 success: function(result) {
1801                         equals( result, "" );
1802                         start();
1803                 }
1804         });
1805 });
1806
1807 test("jQuery.ajax - If-Modified-Since support", function() {
1808         expect( 3 );
1809
1810         stop();
1811
1812         var url = "data/if_modified_since.php?ts=" + new Date();
1813
1814         jQuery.ajax({
1815                 url: url,
1816                 ifModified: true,
1817                 success: function(data, status) {
1818                         equals(status, "success");
1819
1820                         jQuery.ajax({
1821                                 url: url,
1822                                 ifModified: true,
1823                                 success: function(data, status) {
1824                                         if ( data === "FAIL" ) {
1825                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1826                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1827                                         } else {
1828                                                 equals(status, "notmodified");
1829                                                 ok(data == null, "response body should be empty");
1830                                         }
1831                                         start();
1832                         },
1833                                 error: function() {
1834                                         // Do this because opera simply refuses to implement 304 handling :(
1835                                         // A feature-driven way of detecting this would be appreciated
1836                                         // See: http://gist.github.com/599419
1837                                         ok(jQuery.browser.opera, "error");
1838                                         ok(jQuery.browser.opera, "error");
1839                                         start();
1840                         }
1841                         });
1842                 },
1843                 error: function() {
1844                         equals(false, "error");
1845                         // Do this because opera simply refuses to implement 304 handling :(
1846                         // A feature-driven way of detecting this would be appreciated
1847                         // See: http://gist.github.com/599419
1848                         ok(jQuery.browser.opera, "error");
1849                         start();
1850                 }
1851         });
1852 });
1853
1854 test("jQuery.ajax - Etag support", function() {
1855         expect( 3 );
1856
1857         stop();
1858
1859         var url = "data/etag.php?ts=" + new Date();
1860
1861         jQuery.ajax({
1862                 url: url,
1863                 ifModified: true,
1864                 success: function(data, status) {
1865                         equals(status, "success");
1866
1867                         jQuery.ajax({
1868                                 url: url,
1869                                 ifModified: true,
1870                                 success: function(data, status) {
1871                                         if ( data === "FAIL" ) {
1872                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1873                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1874                                         } else {
1875                                                 equals(status, "notmodified");
1876                                                 ok(data == null, "response body should be empty");
1877                                         }
1878                                         start();
1879                         },
1880                         error: function() {
1881                                         // Do this because opera simply refuses to implement 304 handling :(
1882                                         // A feature-driven way of detecting this would be appreciated
1883                                         // See: http://gist.github.com/599419
1884                                         ok(jQuery.browser.opera, "error");
1885                                         ok(jQuery.browser.opera, "error");
1886                                         start();
1887                                 }
1888                         });
1889                 },
1890                 error: function() {
1891                         // Do this because opera simply refuses to implement 304 handling :(
1892                         // A feature-driven way of detecting this would be appreciated
1893                         // See: http://gist.github.com/599419
1894                         ok(jQuery.browser.opera, "error");
1895                         start();
1896                 }
1897         });
1898 });
1899
1900 test("jQuery ajax - failing cross-domain", function() {
1901
1902         expect( 2 );
1903
1904         stop();
1905
1906         var i = 2;
1907
1908         jQuery.ajax({
1909                 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1910                 success: function(){ ok( false , "success" ); },
1911                 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1912                 complete: function() { if ( ! --i ) start(); }
1913         });
1914
1915         jQuery.ajax({
1916                 url: 'http://www.google.com',
1917                 success: function(){ ok( false , "success" ); },
1918                 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1919                 complete: function() { if ( ! --i ) start(); }
1920         });
1921
1922 });
1923
1924 test("jQuery ajax - atom+xml", function() {
1925
1926         stop();
1927
1928         jQuery.ajax({
1929                 url: url( 'data/atom+xml.php' ),
1930                 success: function(){ ok( true , "success" ); },
1931                 error: function(){ ok( false , "error" ); },
1932                 complete: function() { start(); }
1933         });
1934
1935 });
1936
1937 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1938         var success = false;
1939         try {
1940                 var xhr = jQuery.ajax({ url: window.location });
1941                 success = true;
1942                 xhr.abort();
1943         } catch (e) {}
1944
1945         ok( success, "document.location did not generate exception" );
1946 });
1947
1948 test( "jQuery.ajax - statusCode" , function() {
1949
1950         var count = 12;
1951
1952         expect( 20 );
1953         stop();
1954
1955         function countComplete() {
1956                 if ( ! --count ) {
1957                         start();
1958                 }
1959         }
1960
1961         function createStatusCodes( name , isSuccess ) {
1962                 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
1963                 return {
1964                         200: function() {
1965                                 ok( isSuccess , name );
1966                         },
1967                         404: function() {
1968                                 ok( ! isSuccess , name );
1969                         }
1970                 };
1971         }
1972
1973         jQuery.each( {
1974                 "data/name.html": true,
1975                 "data/someFileThatDoesNotExist.html": false
1976         } , function( uri , isSuccess ) {
1977
1978                 jQuery.ajax( url( uri ) , {
1979                         statusCode: createStatusCodes( "in options" , isSuccess ),
1980                         complete: countComplete
1981                 });
1982
1983                 jQuery.ajax( url( uri ) , {
1984                         complete: countComplete
1985                 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
1986
1987                 jQuery.ajax( url( uri ) , {
1988                         complete: function(jXHR) {
1989                                 jXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
1990                                 countComplete();
1991                         }
1992                 });
1993
1994                 jQuery.ajax( url( uri ) , {
1995                         complete: function(jXHR) {
1996                                 setTimeout( function() {
1997                                         jXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
1998                                         countComplete();
1999                                 } , 100 );
2000                         }
2001                 });
2002
2003                 jQuery.ajax( url( uri ) , {
2004                         statusCode: createStatusCodes( "all (options)" , isSuccess ),
2005                         complete: function(jXHR) {
2006                                 jXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2007                                 setTimeout( function() {
2008                                         jXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2009                                         countComplete();
2010                                 } , 100 );
2011                         }
2012                 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2013
2014                 var testString = "";
2015
2016                 jQuery.ajax( url( uri ), {
2017                         success: function( a , b , jXHR ) {
2018                                 ok( isSuccess , "success" );
2019                                 var statusCode = {};
2020                                 statusCode[ jXHR.status ] = function() {
2021                                         testString += "B";
2022                                 };
2023                                 jXHR.statusCode( statusCode );
2024                                 testString += "A";
2025                         },
2026                         error: function( jXHR ) {
2027                                 ok( ! isSuccess , "error" );
2028                                 var statusCode = {};
2029                                 statusCode[ jXHR.status ] = function() {
2030                                         testString += "B";
2031                                 };
2032                                 jXHR.statusCode( statusCode );
2033                                 testString += "A";
2034                         },
2035                         complete: function() {
2036                                 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2037                                                 ( isSuccess ? "success" :  "error" ) + " callbacks" );
2038                                 countComplete();
2039                         }
2040                 } );
2041
2042         });
2043 });
2044
2045 test("jQuery.ajax - transitive conversions", function() {
2046
2047         expect( 8 );
2048
2049         stop();
2050
2051         jQuery.when(
2052
2053                 jQuery.ajax( url("data/json.php") , {
2054                         converters: {
2055                                 "json myjson": function( data ) {
2056                                         ok( true , "converter called" );
2057                                         return data;
2058                                 }
2059                         },
2060                         dataType: "myjson",
2061                         success: function() {
2062                                 ok( true , "Transitive conversion worked" );
2063                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2064                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2065                         }
2066                 }),
2067
2068                 jQuery.ajax( url("data/json.php") , {
2069                         converters: {
2070                                 "json myjson": function( data ) {
2071                                         ok( true , "converter called (*)" );
2072                                         return data;
2073                                 }
2074                         },
2075                         contents: false, /* headers are wrong so we ignore them */
2076                         dataType: "* myjson",
2077                         success: function() {
2078                                 ok( true , "Transitive conversion worked (*)" );
2079                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2080                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2081                         }
2082                 })
2083
2084         ).then( start , start );
2085
2086 });
2087
2088 test("jQuery.ajax - active counter", function() {
2089     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
2090 });
2091
2092 }
2093
2094 //}