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