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