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