Adds nested param serialization; Closes #4201 (by merbjedi)
[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         setTimeout(function(){
18                 jQuery('#foo').ajaxStart(function(){
19                         ok( true, "ajaxStart" );
20                 }).ajaxStop(function(){
21                         ok( true, "ajaxStop" );
22                         start();
23                 }).ajaxSend(function(){
24                         ok( true, "ajaxSend" );
25                 }).ajaxComplete(function(){
26                         ok( true, "ajaxComplete" );
27                 }).ajaxError(function(){
28                         ok( false, "ajaxError" );
29                 }).ajaxSuccess(function(){
30                         ok( true, "ajaxSuccess" );
31                 });
32
33                 jQuery.ajax({
34                         url: url("data/name.html"),
35                         beforeSend: function(){ ok(true, "beforeSend"); },
36                         success: function(){ ok(true, "success"); },
37                         error: function(){ ok(false, "error"); },
38                         complete: function(){ ok(true, "complete"); }
39                 });
40         }, 13);
41 });
42
43 test("jQuery.ajax() - error callbacks", function() {
44         expect( 8 );
45         stop();
46
47         jQuery('#foo').ajaxStart(function(){
48                 ok( true, "ajaxStart" );
49         }).ajaxStop(function(){
50                 ok( true, "ajaxStop" );
51                 start();
52         }).ajaxSend(function(){
53                 ok( true, "ajaxSend" );
54         }).ajaxComplete(function(){
55                 ok( true, "ajaxComplete" );
56         }).ajaxError(function(){
57                 ok( true, "ajaxError" );
58         }).ajaxSuccess(function(){
59                 ok( false, "ajaxSuccess" );
60         });
61
62         jQuery.ajaxSetup({ timeout: 500 });
63
64         jQuery.ajax({
65                 url: url("data/name.php?wait=5"),
66                 beforeSend: function(){ ok(true, "beforeSend"); },
67                 success: function(){ ok(false, "success"); },
68                 error: function(){ ok(true, "error"); },
69                 complete: function(){ ok(true, "complete"); }
70         });
71 });
72
73 test("jQuery.ajax() - disabled globals", function() {
74         expect( 3 );
75         stop();
76
77         jQuery('#foo').ajaxStart(function(){
78                 ok( false, "ajaxStart" );
79         }).ajaxStop(function(){
80                 ok( false, "ajaxStop" );
81         }).ajaxSend(function(){
82                 ok( false, "ajaxSend" );
83         }).ajaxComplete(function(){
84                 ok( false, "ajaxComplete" );
85         }).ajaxError(function(){
86                 ok( false, "ajaxError" );
87         }).ajaxSuccess(function(){
88                 ok( false, "ajaxSuccess" );
89         });
90
91         jQuery.ajax({
92                 global: false,
93                 url: url("data/name.html"),
94                 beforeSend: function(){ ok(true, "beforeSend"); },
95                 success: function(){ ok(true, "success"); },
96                 error: function(){ ok(false, "error"); },
97                 complete: function(){
98                   ok(true, "complete");
99                   setTimeout(function(){ start(); }, 13);
100                 }
101         });
102 });
103
104 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
105         expect(3);
106         stop();
107         jQuery.ajax({
108           url: url("data/with_fries.xml"),
109           dataType: "xml",
110           success: function(resp) {
111                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
112                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
113                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
114                 start();
115           }
116         });
117 });
118
119 test("jQuery.ajax - beforeSend", function() {
120         expect(1);
121         stop();
122
123         var check = false;
124
125         jQuery.ajaxSetup({ timeout: 0 });
126
127         jQuery.ajax({
128                 url: url("data/name.html"),
129                 beforeSend: function(xml) {
130                         check = true;
131                 },
132                 success: function(data) {
133                         ok( check, "check beforeSend was executed" );
134                         start();
135                 }
136         });
137 });
138
139 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
140         expect(2);
141         var request = jQuery.ajax({
142                 url: url("data/name.html"),
143                 beforeSend: function() {
144                         ok( true, "beforeSend got called, canceling" );
145                         return false;
146                 },
147                 success: function() {
148                         ok( false, "request didn't get canceled" );
149                 },
150                 complete: function() {
151                         ok( false, "request didn't get canceled" );
152                 },
153                 error: function() {
154                         ok( false, "request didn't get canceled" );
155                 }
156         });
157         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
158 });
159
160 window.foobar = null;
161 window.testFoo = undefined;
162
163 test("jQuery.ajax - dataType html", function() {
164         expect(5);
165         stop();
166
167         var verifyEvaluation = function() {
168                 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
169                 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
170
171                 start();
172         };
173
174         jQuery.ajax({
175           dataType: "html",
176           url: url("data/test.html"),
177           success: function(data) {
178                 jQuery("#ap").html(data);
179                 ok( data.match(/^html text/), 'Check content for datatype html' );
180                 setTimeout(verifyEvaluation, 600);
181           }
182         });
183 });
184
185 test("serialize()", function() {
186         expect(5);
187
188         equals( jQuery('#form').serialize(),
189                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2",
190                 'Check form serialization as query string');
191
192         equals( jQuery('#form :input').serialize(),
193                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2",
194                 'Check input serialization as query string');
195
196         equals( jQuery('#testForm').serialize(),
197                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
198                 'Check form serialization as query string');
199
200         equals( jQuery('#testForm :input').serialize(),
201                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
202                 'Check input serialization as query string');
203
204         equals( jQuery('#form, #testForm').serialize(),
205                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
206                 'Multiple form serialization as query string');
207
208   /* Temporarily disabled. Opera 10 has problems with form serialization.
209         equals( jQuery('#form, #testForm :input').serialize(),
210                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
211                 'Mixed form/input serialization as query string');
212         */
213 });
214
215 test("jQuery.param()", function() {
216         expect(8);
217         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
218         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
219
220         params = {someName: [1, 2, 3], regularThing: "blah" };
221         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
222
223         params = {foo: ['a', 'b', 'c']};
224         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
225
226         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
227         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
228
229         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
230         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" );
231
232         params = {foo: {bar: "baz", beep: 42, quux: "All your base are belong to us"}};
233         equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "nested object" );
234
235         params = {foo: {"bar[]": [1,2,3]}};
236         equals( jQuery.param(params), "foo%5Bbar%5D%5B%5D=1&foo%5Bbar%5D%5B%5D=2&foo%5Bbar%5D%5B%5D=3", "nested array");
237
238         params = {foo: [{bar: "baz", beep: 42}, {bar: "baz2", beep: 43}]};
239         equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bbar%5D=baz2&foo%5Bbeep%5D=43", "nested array of objects" );
240
241 });
242
243 test("synchronous request", function() {
244         expect(1);
245         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
246 });
247
248 test("synchronous request with callbacks", function() {
249         expect(2);
250         var result;
251         jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
252         ok( /^{ "data"/.test( result ), "check returned text" );
253 });
254
255 test("pass-through request object", function() {
256         expect(8);
257         stop();
258
259         var target = "data/name.html";
260         var successCount = 0;
261         var errorCount = 0;
262         var errorEx = "";
263         var success = function() {
264                 successCount++;
265         };
266         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
267                 errorCount++;
268                 errorEx += ": " + xml.status;
269         });
270         jQuery("#foo").one('ajaxStop', function () {
271                 equals(successCount, 5, "Check all ajax calls successful");
272                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
273                 jQuery("#foo").unbind('ajaxError');
274
275                 start();
276         });
277
278         ok( jQuery.get(url(target), success), "get" );
279         ok( jQuery.post(url(target), success), "post" );
280         ok( jQuery.getScript(url("data/test.js"), success), "script" );
281         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
282         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
283 });
284
285 test("ajax cache", function () {
286         expect(18);
287         stop();
288
289         var count = 0;
290
291         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
292                 var re = /_=(.*?)(&|$)/g;
293         var oldOne = null;
294                 for (var i = 0; i < 6; i++) {
295           var ret = re.exec(s.url);
296                         if (!ret) {
297                                 break;
298                         }
299           oldOne = ret[1];
300                 }
301                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
302                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
303                 if(++count == 6)
304                         start();
305         });
306
307         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
308         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
309         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
310         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
311         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
312         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
313 });
314
315 test("global ajaxSettings", function() {
316         expect(2);
317
318         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
319         var orig = { url: "data/with_fries.xml" };
320         var t;
321
322         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
323
324         t = jQuery.extend({}, orig);
325         t.data = {};
326         jQuery.ajax(t);
327         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
328
329         t = jQuery.extend({}, orig);
330         t.data = { zoo: 'a', ping: 'b' };
331         jQuery.ajax(t);
332         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' }" );
333
334         jQuery.ajaxSettings = tmp;
335 });
336
337 test("load(String)", function() {
338         expect(1);
339         stop(); // check if load can be called with only url
340         jQuery('#first').load("data/name.html", start);
341 });
342
343 test("load('url selector')", function() {
344         expect(1);
345         stop(); // check if load can be called with only url
346         jQuery('#first').load("data/test3.html div.user", function(){
347                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
348                 start();
349         });
350 });
351
352 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
353         expect(1);
354         stop();
355         jQuery.ajaxSetup({ dataType: "json" });
356         jQuery("#first").ajaxComplete(function (e, xml, s) {
357                 equals( s.dataType, "html", "Verify the load() dataType was html" );
358                 jQuery("#first").unbind("ajaxComplete");
359                 jQuery.ajaxSetup({ dataType: "" });
360                 start();
361         });
362         jQuery('#first').load("data/test3.html");
363 });
364
365 test("load(String, Function) - simple: inject text into DOM", function() {
366         expect(2);
367         stop();
368         jQuery('#first').load(url("data/name.html"), function() {
369                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
370                 start();
371         });
372 });
373
374 test("load(String, Function) - check scripts", function() {
375         expect(7);
376         stop();
377
378         var verifyEvaluation = function() {
379                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
380                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
381
382                 start();
383         };
384         jQuery('#first').load(url('data/test.html'), function() {
385                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
386                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
387                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
388                 setTimeout(verifyEvaluation, 600);
389         });
390 });
391
392 test("load(String, Function) - check file with only a script tag", function() {
393         expect(3);
394         stop();
395
396         jQuery('#first').load(url('data/test2.html'), function() {
397                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
398                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
399
400                 start();
401         });
402 });
403
404 test("load(String, Object, Function)", function() {
405         expect(2);
406         stop();
407
408         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
409                 var $post = jQuery(this).find('#post');
410                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
411                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
412                 start();
413         });
414 });
415
416 test("load(String, String, Function)", function() {
417         expect(2);
418         stop();
419
420         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
421                 var $get = jQuery(this).find('#get');
422                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
423                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
424                 start();
425         });
426 });
427
428 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
429         expect(2);
430         stop();
431         jQuery.get(url('data/dashboard.xml'), function(xml) {
432                 var content = [];
433                 jQuery('tab', xml).each(function() {
434                         content.push(jQuery(this).text());
435                 });
436                 equals( content[0], 'blabla', 'Check first tab');
437                 equals( content[1], 'blublu', 'Check second tab');
438                 start();
439         });
440 });
441
442 test("jQuery.getScript(String, Function) - with callback", function() {
443         expect(2);
444         stop();
445         jQuery.getScript(url("data/test.js"), function() {
446                 equals( foobar, "bar", 'Check if script was evaluated' );
447                 setTimeout(start, 100);
448         });
449 });
450
451 test("jQuery.getScript(String, Function) - no callback", function() {
452         expect(1);
453         stop();
454         jQuery.getScript(url("data/test.js"), function(){
455                 start();
456         });
457 });
458
459 test("jQuery.ajax() - JSONP, Local", function() {
460         expect(7);
461
462         var count = 0;
463         function plus(){ if ( ++count == 7 ) start(); }
464
465         stop();
466
467         jQuery.ajax({
468                 url: "data/jsonp.php",
469                 dataType: "jsonp",
470                 success: function(data){
471                         ok( data.data, "JSON results returned (GET, no callback)" );
472                         plus();
473                 },
474                 error: function(data){
475                         ok( false, "Ajax error JSON (GET, no callback)" );
476                         plus();
477                 }
478         });
479
480         jQuery.ajax({
481                 url: "data/jsonp.php?callback=?",
482                 dataType: "jsonp",
483                 success: function(data){
484                         ok( data.data, "JSON results returned (GET, url callback)" );
485                         plus();
486                 },
487                 error: function(data){
488                         ok( false, "Ajax error JSON (GET, url callback)" );
489                         plus();
490                 }
491         });
492
493         jQuery.ajax({
494                 url: "data/jsonp.php",
495                 dataType: "jsonp",
496                 data: "callback=?",
497                 success: function(data){
498                         ok( data.data, "JSON results returned (GET, data callback)" );
499                         plus();
500                 },
501                 error: function(data){
502                         ok( false, "Ajax error JSON (GET, data callback)" );
503                         plus();
504                 }
505         });
506
507         jQuery.ajax({
508                 url: "data/jsonp.php",
509                 dataType: "jsonp",
510                 jsonp: "callback",
511                 success: function(data){
512                         ok( data.data, "JSON results returned (GET, data obj callback)" );
513                         plus();
514                 },
515                 error: function(data){
516                         ok( false, "Ajax error JSON (GET, data obj callback)" );
517                         plus();
518                 }
519         });
520
521         jQuery.ajax({
522                 type: "POST",
523                 url: "data/jsonp.php",
524                 dataType: "jsonp",
525                 success: function(data){
526                         ok( data.data, "JSON results returned (POST, no callback)" );
527                         plus();
528                 },
529                 error: function(data){
530                         ok( false, "Ajax error JSON (GET, data obj callback)" );
531                         plus();
532                 }
533         });
534
535         jQuery.ajax({
536                 type: "POST",
537                 url: "data/jsonp.php",
538                 data: "callback=?",
539                 dataType: "jsonp",
540                 success: function(data){
541                         ok( data.data, "JSON results returned (POST, data callback)" );
542                         plus();
543                 },
544                 error: function(data){
545                         ok( false, "Ajax error JSON (POST, data callback)" );
546                         plus();
547                 }
548         });
549
550         jQuery.ajax({
551                 type: "POST",
552                 url: "data/jsonp.php",
553                 jsonp: "callback",
554                 dataType: "jsonp",
555                 success: function(data){
556                         ok( data.data, "JSON results returned (POST, data obj callback)" );
557                         plus();
558                 },
559                 error: function(data){
560                         ok( false, "Ajax error JSON (POST, data obj callback)" );
561                         plus();
562                 }
563         });
564 });
565
566 test("jQuery.ajax() - JSONP, Remote", function() {
567         expect(4);
568
569         var count = 0;
570         function plus(){ if ( ++count == 4 ) start(); }
571
572         var base = window.location.href.replace(/\?.*$/, "");
573
574         stop();
575
576         jQuery.ajax({
577                 url: base + "data/jsonp.php",
578                 dataType: "jsonp",
579                 success: function(data){
580                         ok( data.data, "JSON results returned (GET, no callback)" );
581                         plus();
582                 },
583                 error: function(data){
584                         ok( false, "Ajax error JSON (GET, no callback)" );
585                         plus();
586                 }
587         });
588
589         jQuery.ajax({
590                 url: base + "data/jsonp.php?callback=?",
591                 dataType: "jsonp",
592                 success: function(data){
593                         ok( data.data, "JSON results returned (GET, url callback)" );
594                         plus();
595                 },
596                 error: function(data){
597                         ok( false, "Ajax error JSON (GET, url callback)" );
598                         plus();
599                 }
600         });
601
602         jQuery.ajax({
603                 url: base + "data/jsonp.php",
604                 dataType: "jsonp",
605                 data: "callback=?",
606                 success: function(data){
607                         ok( data.data, "JSON results returned (GET, data callback)" );
608                         plus();
609                 },
610                 error: function(data){
611                         ok( false, "Ajax error JSON (GET, data callback)" );
612                         plus();
613                 }
614         });
615
616         jQuery.ajax({
617                 url: base + "data/jsonp.php",
618                 dataType: "jsonp",
619                 jsonp: "callback",
620                 success: function(data){
621                         ok( data.data, "JSON results returned (GET, data obj callback)" );
622                         plus();
623                 },
624                 error: function(data){
625                         ok( false, "Ajax error JSON (GET, data obj callback)" );
626                         plus();
627                 }
628         });
629 });
630
631 test("jQuery.ajax() - script, Remote", function() {
632         expect(2);
633
634         var base = window.location.href.replace(/\?.*$/, "");
635
636         stop();
637
638         jQuery.ajax({
639                 url: base + "data/test.js",
640                 dataType: "script",
641                 success: function(data){
642                         ok( foobar, "Script results returned (GET, no callback)" );
643                         start();
644                 }
645         });
646 });
647
648 test("jQuery.ajax() - script, Remote with POST", function() {
649         expect(3);
650
651         var base = window.location.href.replace(/\?.*$/, "");
652
653         stop();
654
655         jQuery.ajax({
656                 url: base + "data/test.js",
657                 type: "POST",
658                 dataType: "script",
659                 success: function(data, status){
660                         ok( foobar, "Script results returned (POST, no callback)" );
661                         equals( status, "success", "Script results returned (POST, no callback)" );
662                         start();
663                 },
664                 error: function(xhr) {
665                         ok( false, "ajax error, status code: " + xhr.status );
666                         start();
667                 }
668         });
669 });
670
671 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
672         expect(2);
673
674         var base = window.location.href.replace(/\?.*$/, "");
675         base = base.replace(/^.*?\/\//, "//");
676
677         stop();
678
679         jQuery.ajax({
680                 url: base + "data/test.js",
681                 dataType: "script",
682                 success: function(data){
683                         ok( foobar, "Script results returned (GET, no callback)" );
684                         start();
685                 }
686         });
687 });
688
689 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
690         expect(4);
691         stop();
692         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
693           equals( json[0].name, 'John', 'Check JSON: first, name' );
694           equals( json[0].age, 21, 'Check JSON: first, age' );
695           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
696           equals( json[1].age, 25, 'Check JSON: second, age' );
697           start();
698         });
699 });
700
701 test("jQuery.getJSON(String, Function) - JSON object", function() {
702         expect(2);
703         stop();
704         jQuery.getJSON(url("data/json.php"), function(json) {
705           equals( json.data.lang, 'en', 'Check JSON: lang' );
706           equals( json.data.length, 25, 'Check JSON: length' );
707           start();
708         });
709 });
710
711 test("jQuery.getJSON - Using Native JSON", function() {
712         expect(2);
713         
714         var old = window.JSON;
715         JSON = {
716                 parse: function(str){
717                         ok( true, "Verifying that parse method was run" );
718                         return true;
719                 }
720         };
721
722         stop();
723         jQuery.getJSON(url("data/json.php"), function(json) {
724                 window.JSON = old;
725           equals( json, true, "Verifying return value" );
726           start();
727         });
728 });
729
730 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
731         expect(2);
732
733         var base = window.location.href.replace(/\?.*$/, "");
734
735         stop();
736         jQuery.getJSON(url(base + "data/json.php"), function(json) {
737           equals( json.data.lang, 'en', 'Check JSON: lang' );
738           equals( json.data.length, 25, 'Check JSON: length' );
739           start();
740         });
741 });
742
743 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
744         expect(4);
745         stop();
746         var done = 0;
747
748         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
749           jQuery('math', xml).each(function() {
750                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
751                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
752                  });
753           if ( ++done === 2 ) start();
754         });
755
756         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
757           jQuery('math', xml).each(function() {
758                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
759                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
760                  });
761           if ( ++done === 2 ) start();
762         });
763 });
764
765 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
766         stop();
767
768         var passed = 0;
769
770         jQuery.ajaxSetup({timeout: 1000});
771
772         var pass = function() {
773                 passed++;
774                 if ( passed == 2 ) {
775                         ok( true, 'Check local and global callbacks after timeout' );
776                         jQuery('#main').unbind("ajaxError");
777                         start();
778                 }
779         };
780
781         var fail = function(a,b,c) {
782                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
783                 start();
784         };
785
786         jQuery('#main').ajaxError(pass);
787
788         jQuery.ajax({
789           type: "GET",
790           url: url("data/name.php?wait=5"),
791           error: pass,
792           success: fail
793         });
794
795         // reset timeout
796         jQuery.ajaxSetup({timeout: 0});
797 });
798
799 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
800         stop();
801         jQuery.ajaxSetup({timeout: 50});
802
803         jQuery.ajax({
804           type: "GET",
805           timeout: 15000,
806           url: url("data/name.php?wait=1"),
807           error: function() {
808                    ok( false, 'Check for local timeout failed' );
809                    start();
810           },
811           success: function() {
812                 ok( true, 'Check for local timeout' );
813                 start();
814           }
815         });
816
817         // reset timeout
818         jQuery.ajaxSetup({timeout: 0});
819 });
820
821 test("jQuery.ajax - simple get", function() {
822         expect(1);
823         stop();
824         jQuery.ajax({
825           type: "GET",
826           url: url("data/name.php?name=foo"),
827           success: function(msg){
828                 equals( msg, 'bar', 'Check for GET' );
829                 start();
830           }
831         });
832 });
833
834 test("jQuery.ajax - simple post", function() {
835         expect(1);
836         stop();
837         jQuery.ajax({
838           type: "POST",
839           url: url("data/name.php"),
840           data: "name=peter",
841           success: function(msg){
842                 equals( msg, 'pan', 'Check for POST' );
843                 start();
844           }
845         });
846 });
847
848 test("ajaxSetup()", function() {
849         expect(1);
850         stop();
851         jQuery.ajaxSetup({
852                 url: url("data/name.php?name=foo"),
853                 success: function(msg){
854                         equals( msg, 'bar', 'Check for GET' );
855                         start();
856                 }
857         });
858         jQuery.ajax();
859 });
860
861 /*
862 test("custom timeout does not set error message when timeout occurs, see #970", function() {
863         stop();
864         jQuery.ajax({
865                 url: "data/name.php?wait=1",
866                 timeout: 500,
867                 error: function(request, status) {
868                         ok( status != null, "status shouldn't be null in error handler" );
869                         equals( "timeout", status );
870                         start();
871                 }
872         });
873 });
874 */
875
876 test("data option: evaluate function values (#2806)", function() {
877         stop();
878         jQuery.ajax({
879                 url: "data/echoQuery.php",
880                 data: {
881                         key: function() {
882                                 return "value";
883                         }
884                 },
885                 success: function(result) {
886                         equals( result, "key=value" );
887                         start();
888                 }
889         })
890 });
891
892 test("jQuery.ajax - If-Modified-Since support", function() {
893         expect( 3 );
894
895         stop();
896
897         var url = "data/if_modified_since.php?ts=" + new Date();
898
899         jQuery.ajax({
900                 url: url,
901                 ifModified: true,
902                 success: function(data, status) { 
903                         equals(status, "success");
904                         
905                         jQuery.ajax({
906                                 url: url,
907                                 ifModified: true,
908                                 success: function(data, status) { 
909                                         if ( data === "FAIL" ) {
910                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
911                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
912                                         } else {
913                                                 equals(status, "notmodified");
914                                                 ok(data == null, "response body should be empty")
915                                         }
916                                         start();
917                                 }
918                         });
919                 }
920         });
921 });
922
923 test("jQuery.ajax - Etag support", function() {
924         expect( 3 );
925
926         stop();
927
928         var url = "data/etag.php?ts=" + new Date();
929
930         jQuery.ajax({
931                 url: url,
932                 ifModified: true,
933                 success: function(data, status) { 
934                         equals(status, "success");
935                         
936                         jQuery.ajax({
937                                 url: url,
938                                 ifModified: true,
939                                 success: function(data, status) { 
940                                         if ( data === "FAIL" ) {
941                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
942                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
943                                         } else {
944                                                 equals(status, "notmodified");
945                                                 ok(data == null, "response body should be empty")
946                                         }
947                                         start();
948                                 }
949                         });
950                 }
951         });
952 });
953
954 }
955
956 //}