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