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