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