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