9f46c4134aa1b185a5357d22b935d2ac79a0d8c5
[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(10);
1085
1086         var count = 0;
1087         function plus(){ if ( ++count == 10 ) 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                 data: {
1135                         callback: "?"
1136                 },
1137                 success: function(data){
1138                         ok( data.data, "JSON results returned (GET, processed data callback)" );
1139                         plus();
1140                 },
1141                 error: function(data){
1142                         ok( false, "Ajax error JSON (GET, processed data callback)" );
1143                         plus();
1144                 }
1145         });
1146
1147         jQuery.ajax({
1148                 url: "data/jsonp.php",
1149                 dataType: "jsonp",
1150                 jsonp: "callback",
1151                 success: function(data){
1152                         ok( data.data, "JSON results returned (GET, data obj callback)" );
1153                         plus();
1154                 },
1155                 error: function(data){
1156                         ok( false, "Ajax error JSON (GET, data obj callback)" );
1157                         plus();
1158                 }
1159         });
1160
1161         jQuery.ajax({
1162                 url: "data/jsonp.php",
1163                 dataType: "jsonp",
1164                 jsonpCallback: "jsonpResults",
1165                 success: function(data){
1166                         ok( data.data, "JSON results returned (GET, custom callback name)" );
1167                         plus();
1168                 },
1169                 error: function(data){
1170                         ok( false, "Ajax error JSON (GET, custom callback name)" );
1171                         plus();
1172                 }
1173         });
1174
1175         jQuery.ajax({
1176                 type: "POST",
1177                 url: "data/jsonp.php",
1178                 dataType: "jsonp",
1179                 success: function(data){
1180                         ok( data.data, "JSON results returned (POST, no callback)" );
1181                         plus();
1182                 },
1183                 error: function(data){
1184                         ok( false, "Ajax error JSON (GET, data obj callback)" );
1185                         plus();
1186                 }
1187         });
1188
1189         jQuery.ajax({
1190                 type: "POST",
1191                 url: "data/jsonp.php",
1192                 data: "callback=?",
1193                 dataType: "jsonp",
1194                 success: function(data){
1195                         ok( data.data, "JSON results returned (POST, data callback)" );
1196                         plus();
1197                 },
1198                 error: function(data){
1199                         ok( false, "Ajax error JSON (POST, data callback)" );
1200                         plus();
1201                 }
1202         });
1203
1204         jQuery.ajax({
1205                 type: "POST",
1206                 url: "data/jsonp.php",
1207                 jsonp: "callback",
1208                 dataType: "jsonp",
1209                 success: function(data){
1210                         ok( data.data, "JSON results returned (POST, data obj callback)" );
1211                         plus();
1212                 },
1213                 error: function(data){
1214                         ok( false, "Ajax error JSON (POST, data obj callback)" );
1215                         plus();
1216                 }
1217         });
1218
1219         //#7578
1220         jQuery.ajax({
1221                 url: "data/jsonp.php",
1222                 dataType: "jsonp",
1223                 beforeSend: function(){
1224                         strictEqual( this.cache, false, "cache must be false on JSON request" );
1225                         plus();
1226                         return false;
1227                 }
1228         });
1229 });
1230
1231 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1232         expect(1);
1233         stop();
1234
1235         window.jsonpResults = function(data) {
1236                 ok( data.data, "JSON results returned (GET, custom callback function)" );
1237                 window.jsonpResults = undefined;
1238                 start();
1239         };
1240
1241         jQuery.ajax({
1242                 url: "data/jsonp.php",
1243                 dataType: "jsonp",
1244                 jsonpCallback: "jsonpResults"
1245         });
1246 });
1247
1248 test("jQuery.ajax() - JSONP, Remote", function() {
1249         expect(4);
1250
1251         var count = 0;
1252         function plus(){ if ( ++count == 4 ) start(); }
1253
1254         var base = window.location.href.replace(/[^\/]*$/, "");
1255
1256         stop();
1257
1258         jQuery.ajax({
1259                 url: base + "data/jsonp.php",
1260                 dataType: "jsonp",
1261                 success: function(data){
1262                         ok( data.data, "JSON results returned (GET, no callback)" );
1263                         plus();
1264                 },
1265                 error: function(data){
1266                         ok( false, "Ajax error JSON (GET, no callback)" );
1267                         plus();
1268                 }
1269         });
1270
1271         jQuery.ajax({
1272                 url: base + "data/jsonp.php?callback=?",
1273                 dataType: "jsonp",
1274                 success: function(data){
1275                         ok( data.data, "JSON results returned (GET, url callback)" );
1276                         plus();
1277                 },
1278                 error: function(data){
1279                         ok( false, "Ajax error JSON (GET, url callback)" );
1280                         plus();
1281                 }
1282         });
1283
1284         jQuery.ajax({
1285                 url: base + "data/jsonp.php",
1286                 dataType: "jsonp",
1287                 data: "callback=?",
1288                 success: function(data){
1289                         ok( data.data, "JSON results returned (GET, data callback)" );
1290                         plus();
1291                 },
1292                 error: function(data){
1293                         ok( false, "Ajax error JSON (GET, data callback)" );
1294                         plus();
1295                 }
1296         });
1297
1298         jQuery.ajax({
1299                 url: base + "data/jsonp.php",
1300                 dataType: "jsonp",
1301                 jsonp: "callback",
1302                 success: function(data){
1303                         ok( data.data, "JSON results returned (GET, data obj callback)" );
1304                         plus();
1305                 },
1306                 error: function(data){
1307                         ok( false, "Ajax error JSON (GET, data obj callback)" );
1308                         plus();
1309                 }
1310         });
1311 });
1312
1313 test("jQuery.ajax() - script, Remote", function() {
1314         expect(2);
1315
1316         var base = window.location.href.replace(/[^\/]*$/, "");
1317
1318         stop();
1319
1320         jQuery.ajax({
1321                 url: base + "data/test.js",
1322                 dataType: "script",
1323                 success: function(data){
1324                         ok( foobar, "Script results returned (GET, no callback)" );
1325                         start();
1326                 }
1327         });
1328 });
1329
1330 test("jQuery.ajax() - script, Remote with POST", function() {
1331         expect(3);
1332
1333         var base = window.location.href.replace(/[^\/]*$/, "");
1334
1335         stop();
1336
1337         jQuery.ajax({
1338                 url: base + "data/test.js",
1339                 type: "POST",
1340                 dataType: "script",
1341                 success: function(data, status){
1342                         ok( foobar, "Script results returned (POST, no callback)" );
1343                         equals( status, "success", "Script results returned (POST, no callback)" );
1344                         start();
1345                 },
1346                 error: function(xhr) {
1347                         ok( false, "ajax error, status code: " + xhr.status );
1348                         start();
1349                 }
1350         });
1351 });
1352
1353 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1354         expect(2);
1355
1356         var base = window.location.href.replace(/[^\/]*$/, "");
1357         base = base.replace(/^.*?\/\//, "//");
1358
1359         stop();
1360
1361         jQuery.ajax({
1362                 url: base + "data/test.js",
1363                 dataType: "script",
1364                 success: function(data){
1365                         ok( foobar, "Script results returned (GET, no callback)" );
1366                         start();
1367                 }
1368         });
1369 });
1370
1371 test("jQuery.ajax() - malformed JSON", function() {
1372         expect(2);
1373
1374         stop();
1375
1376         jQuery.ajax({
1377                 url: "data/badjson.js",
1378                 dataType: "json",
1379                 success: function(){
1380                         ok( false, "Success." );
1381                         start();
1382                 },
1383                 error: function(xhr, msg, detailedMsg) {
1384                         equals( "parsererror", msg, "A parse error occurred." );
1385                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1386                         start();
1387                 }
1388         });
1389 });
1390
1391 test("jQuery.ajax() - script by content-type", function() {
1392         expect(1);
1393
1394         stop();
1395
1396         jQuery.ajax({
1397                 url: "data/script.php",
1398                 data: { header: "script" },
1399                 success: function() {
1400                         start();
1401                 }
1402         });
1403 });
1404
1405 test("jQuery.ajax() - json by content-type", function() {
1406         expect(5);
1407
1408         stop();
1409
1410         jQuery.ajax({
1411                 url: "data/json.php",
1412                 data: { header: "json", json: "array" },
1413                 success: function( json ) {
1414                         ok( json.length >= 2, "Check length");
1415                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1416                         equals( json[0].age, 21, 'Check JSON: first, age' );
1417                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1418                         equals( json[1].age, 25, 'Check JSON: second, age' );
1419                         start();
1420                 }
1421         });
1422 });
1423
1424 test("jQuery.ajax() - json by content-type disabled with options", function() {
1425         expect(6);
1426
1427         stop();
1428
1429         jQuery.ajax({
1430                 url: url("data/json.php"),
1431                 data: { header: "json", json: "array" },
1432                 contents: {
1433                         json: false
1434                 },
1435                 success: function( text ) {
1436                         equals( typeof text , "string" , "json wasn't auto-determined" );
1437                         var json = jQuery.parseJSON( text );
1438                         ok( json.length >= 2, "Check length");
1439                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1440                         equals( json[0].age, 21, 'Check JSON: first, age' );
1441                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1442                         equals( json[1].age, 25, 'Check JSON: second, age' );
1443                         start();
1444                 }
1445         });
1446 });
1447
1448 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1449         expect(5);
1450         stop();
1451         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1452           ok( json.length >= 2, "Check length");
1453           equals( json[0].name, 'John', 'Check JSON: first, name' );
1454           equals( json[0].age, 21, 'Check JSON: first, age' );
1455           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1456           equals( json[1].age, 25, 'Check JSON: second, age' );
1457           start();
1458         });
1459 });
1460
1461 test("jQuery.getJSON(String, Function) - JSON object", function() {
1462         expect(2);
1463         stop();
1464         jQuery.getJSON(url("data/json.php"), function(json) {
1465           if (json && json.data) {
1466                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1467                   equals( json.data.length, 25, 'Check JSON: length' );
1468           }
1469           start();
1470         });
1471 });
1472
1473 test("jQuery.getJSON - Using Native JSON", function() {
1474         expect(2);
1475
1476         var old = window.JSON;
1477         JSON = {
1478                 parse: function(str){
1479                         ok( true, "Verifying that parse method was run" );
1480                         return true;
1481                 }
1482         };
1483
1484         stop();
1485         jQuery.getJSON(url("data/json.php"), function(json) {
1486                 window.JSON = old;
1487                 equals( json, true, "Verifying return value" );
1488                 start();
1489         });
1490 });
1491
1492 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1493         expect(2);
1494
1495         var base = window.location.href.replace(/[^\/]*$/, "");
1496
1497         stop();
1498         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1499           equals( json.data.lang, 'en', 'Check JSON: lang' );
1500           equals( json.data.length, 25, 'Check JSON: length' );
1501           start();
1502         });
1503 });
1504
1505 test("jQuery.post - data", function() {
1506         expect(2);
1507         stop();
1508
1509         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1510                 jQuery('math', xml).each(function() {
1511                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1512                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1513                 });
1514                 start();
1515         });
1516 });
1517
1518 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1519         expect(4);
1520         stop();
1521         var done = 0;
1522
1523         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1524           jQuery('math', xml).each(function() {
1525                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1526                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1527                  });
1528           if ( ++done === 2 ) start();
1529         });
1530
1531         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1532           jQuery('math', xml).each(function() {
1533                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1534                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1535                  });
1536           if ( ++done === 2 ) start();
1537         });
1538 });
1539
1540 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1541         stop();
1542
1543         var passed = 0;
1544
1545         jQuery.ajaxSetup({timeout: 1000});
1546
1547         var pass = function() {
1548                 passed++;
1549                 if ( passed == 2 ) {
1550                         ok( true, 'Check local and global callbacks after timeout' );
1551                         jQuery('#main').unbind("ajaxError");
1552                         start();
1553                 }
1554         };
1555
1556         var fail = function(a,b,c) {
1557                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1558                 start();
1559         };
1560
1561         jQuery('#main').ajaxError(pass);
1562
1563         jQuery.ajax({
1564           type: "GET",
1565           url: url("data/name.php?wait=5"),
1566           error: pass,
1567           success: fail
1568         });
1569
1570         // reset timeout
1571         jQuery.ajaxSetup({timeout: 0});
1572 });
1573
1574 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1575         stop();
1576         jQuery.ajaxSetup({timeout: 50});
1577
1578         jQuery.ajax({
1579           type: "GET",
1580           timeout: 15000,
1581           url: url("data/name.php?wait=1"),
1582           error: function() {
1583                    ok( false, 'Check for local timeout failed' );
1584                    start();
1585           },
1586           success: function() {
1587                 ok( true, 'Check for local timeout' );
1588                 start();
1589           }
1590         });
1591
1592         // reset timeout
1593         jQuery.ajaxSetup({timeout: 0});
1594 });
1595
1596 test("jQuery.ajax - simple get", function() {
1597         expect(1);
1598         stop();
1599         jQuery.ajax({
1600           type: "GET",
1601           url: url("data/name.php?name=foo"),
1602           success: function(msg){
1603                 equals( msg, 'bar', 'Check for GET' );
1604                 start();
1605           }
1606         });
1607 });
1608
1609 test("jQuery.ajax - simple post", function() {
1610         expect(1);
1611         stop();
1612         jQuery.ajax({
1613           type: "POST",
1614           url: url("data/name.php"),
1615           data: "name=peter",
1616           success: function(msg){
1617                 equals( msg, 'pan', 'Check for POST' );
1618                 start();
1619           }
1620         });
1621 });
1622
1623 test("ajaxSetup()", function() {
1624         expect(1);
1625         stop();
1626         jQuery.ajaxSetup({
1627                 url: url("data/name.php?name=foo"),
1628                 success: function(msg){
1629                         equals( msg, 'bar', 'Check for GET' );
1630                         start();
1631                 }
1632         });
1633         jQuery.ajax();
1634 });
1635
1636 /*
1637 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1638         stop();
1639         jQuery.ajax({
1640                 url: "data/name.php?wait=1",
1641                 timeout: 500,
1642                 error: function(request, status) {
1643                         ok( status != null, "status shouldn't be null in error handler" );
1644                         equals( "timeout", status );
1645                         start();
1646                 }
1647         });
1648 });
1649 */
1650
1651 test("data option: evaluate function values (#2806)", function() {
1652         stop();
1653         jQuery.ajax({
1654                 url: "data/echoQuery.php",
1655                 data: {
1656                         key: function() {
1657                                 return "value";
1658                         }
1659                 },
1660                 success: function(result) {
1661                         equals( result, "key=value" );
1662                         start();
1663                 }
1664         });
1665 });
1666
1667 test("data option: empty bodies for non-GET requests", function() {
1668         stop();
1669         jQuery.ajax({
1670                 url: "data/echoData.php",
1671                 data: undefined,
1672                 type: "post",
1673                 success: function(result) {
1674                         equals( result, "" );
1675                         start();
1676                 }
1677         });
1678 });
1679
1680 test("jQuery.ajax - If-Modified-Since support", function() {
1681         expect( 3 );
1682
1683         stop();
1684
1685         var url = "data/if_modified_since.php?ts=" + new Date();
1686
1687         jQuery.ajax({
1688                 url: url,
1689                 ifModified: true,
1690                 success: function(data, status) {
1691                         equals(status, "success");
1692
1693                         jQuery.ajax({
1694                                 url: url,
1695                                 ifModified: true,
1696                                 success: function(data, status) {
1697                                         if ( data === "FAIL" ) {
1698                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1699                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1700                                         } else {
1701                                                 equals(status, "notmodified");
1702                                                 ok(data == null, "response body should be empty");
1703                                         }
1704                                         start();
1705                         },
1706                                 error: function() {
1707                                         // Do this because opera simply refuses to implement 304 handling :(
1708                                         // A feature-driven way of detecting this would be appreciated
1709                                         // See: http://gist.github.com/599419
1710                                         ok(jQuery.browser.opera, "error");
1711                                         ok(jQuery.browser.opera, "error");
1712                                         start();
1713                         }
1714                         });
1715                 },
1716                 error: function() {
1717                         equals(false, "error");
1718                         // Do this because opera simply refuses to implement 304 handling :(
1719                         // A feature-driven way of detecting this would be appreciated
1720                         // See: http://gist.github.com/599419
1721                         ok(jQuery.browser.opera, "error");
1722                         start();
1723                 }
1724         });
1725 });
1726
1727 test("jQuery.ajax - Etag support", function() {
1728         expect( 3 );
1729
1730         stop();
1731
1732         var url = "data/etag.php?ts=" + new Date();
1733
1734         jQuery.ajax({
1735                 url: url,
1736                 ifModified: true,
1737                 success: function(data, status) {
1738                         equals(status, "success");
1739
1740                         jQuery.ajax({
1741                                 url: url,
1742                                 ifModified: true,
1743                                 success: function(data, status) {
1744                                         if ( data === "FAIL" ) {
1745                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1746                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1747                                         } else {
1748                                                 equals(status, "notmodified");
1749                                                 ok(data == null, "response body should be empty");
1750                                         }
1751                                         start();
1752                         },
1753                         error: function() {
1754                                         // Do this because opera simply refuses to implement 304 handling :(
1755                                         // A feature-driven way of detecting this would be appreciated
1756                                         // See: http://gist.github.com/599419
1757                                         ok(jQuery.browser.opera, "error");
1758                                         ok(jQuery.browser.opera, "error");
1759                                         start();
1760                                 }
1761                         });
1762                 },
1763                 error: function() {
1764                         // Do this because opera simply refuses to implement 304 handling :(
1765                         // A feature-driven way of detecting this would be appreciated
1766                         // See: http://gist.github.com/599419
1767                         ok(jQuery.browser.opera, "error");
1768                         start();
1769                 }
1770         });
1771 });
1772
1773 test("jQuery ajax - failing cross-domain", function() {
1774
1775         expect( 2 );
1776
1777         stop();
1778
1779         var i = 2;
1780
1781         if ( jQuery.ajax({
1782                 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1783                 success: function(){ ok( false , "success" ); },
1784                 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1785                 complete: function() { if ( ! --i ) start(); }
1786         }) === false ) {
1787                 ok( true , "no transport" );
1788                 if ( ! --i ) start();
1789         }
1790
1791         if ( jQuery.ajax({
1792                 url: 'http://www.google.com',
1793                 success: function(){ ok( false , "success" ); },
1794                 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1795                 complete: function() { if ( ! --i ) start(); }
1796         }) === false ) {
1797                 ok( true , "no transport" );
1798                 if ( ! --i ) start();
1799         }
1800
1801 });
1802
1803 test("jQuery ajax - atom+xml", function() {
1804
1805         stop();
1806
1807         jQuery.ajax({
1808                 url: url( 'data/atom+xml.php' ),
1809                 success: function(){ ok( true , "success" ); },
1810                 error: function(){ ok( false , "error" ); },
1811                 complete: function() { start(); }
1812         });
1813
1814 });
1815
1816 test("jQuery.ajax - active counter", function() {
1817     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1818 });
1819
1820 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1821         var success = false;
1822         try {
1823                 var xhr = jQuery.ajax({ url: window.location });
1824                 success = true;
1825                 xhr.abort();
1826         } catch (e) {}
1827
1828         ok( success, "document.location did not generate exception" );
1829 });
1830
1831 }
1832
1833 //}