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