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