Temporarily disabled some tests for Opera 10 - a variety of problems with input eleme...
[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(4);
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[]":["baz", 42, "All your base are belong to us"]};
224         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
225
226         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
227         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" );
228 });
229
230 test("synchronous request", function() {
231         expect(1);
232         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
233 });
234
235 test("synchronous request with callbacks", function() {
236         expect(2);
237         var result;
238         jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
239         ok( /^{ "data"/.test( result ), "check returned text" );
240 });
241
242 test("pass-through request object", function() {
243         expect(8);
244         stop();
245
246         var target = "data/name.html";
247         var successCount = 0;
248         var errorCount = 0;
249         var errorEx = "";
250         var success = function() {
251                 successCount++;
252         };
253         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
254                 errorCount++;
255                 errorEx += ": " + xml.status;
256         });
257         jQuery("#foo").one('ajaxStop', function () {
258                 equals(successCount, 5, "Check all ajax calls successful");
259                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
260                 jQuery("#foo").unbind('ajaxError');
261
262                 start();
263         });
264
265         ok( jQuery.get(url(target), success), "get" );
266         ok( jQuery.post(url(target), success), "post" );
267         ok( jQuery.getScript(url("data/test.js"), success), "script" );
268         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
269         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
270 });
271
272 test("ajax cache", function () {
273         expect(18);
274         stop();
275
276         var count = 0;
277
278         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
279                 var re = /_=(.*?)(&|$)/g;
280         var oldOne = null;
281                 for (var i = 0; i < 6; i++) {
282           var ret = re.exec(s.url);
283                         if (!ret) {
284                                 break;
285                         }
286           oldOne = ret[1];
287                 }
288                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
289                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
290                 if(++count == 6)
291                         start();
292         });
293
294         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
295         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
296         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
297         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
298         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
299         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
300 });
301
302 test("global ajaxSettings", function() {
303         expect(2);
304
305         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
306         var orig = { url: "data/with_fries.xml" };
307         var t;
308
309         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
310
311         t = jQuery.extend({}, orig);
312         t.data = {};
313         jQuery.ajax(t);
314         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
315
316         t = jQuery.extend({}, orig);
317         t.data = { zoo: 'a', ping: 'b' };
318         jQuery.ajax(t);
319         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' }" );
320
321         jQuery.ajaxSettings = tmp;
322 });
323
324 test("load(String)", function() {
325         expect(1);
326         stop(); // check if load can be called with only url
327         jQuery('#first').load("data/name.html", start);
328 });
329
330 test("load('url selector')", function() {
331         expect(1);
332         stop(); // check if load can be called with only url
333         jQuery('#first').load("data/test3.html div.user", function(){
334                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
335                 start();
336         });
337 });
338
339 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
340         expect(1);
341         stop();
342         jQuery.ajaxSetup({ dataType: "json" });
343         jQuery("#first").ajaxComplete(function (e, xml, s) {
344                 equals( s.dataType, "html", "Verify the load() dataType was html" );
345                 jQuery("#first").unbind("ajaxComplete");
346                 jQuery.ajaxSetup({ dataType: "" });
347                 start();
348         });
349         jQuery('#first').load("data/test3.html");
350 });
351
352 test("load(String, Function) - simple: inject text into DOM", function() {
353         expect(2);
354         stop();
355         jQuery('#first').load(url("data/name.html"), function() {
356                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
357                 start();
358         });
359 });
360
361 test("load(String, Function) - check scripts", function() {
362         expect(7);
363         stop();
364
365         var verifyEvaluation = function() {
366                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
367                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
368
369                 start();
370         };
371         jQuery('#first').load(url('data/test.html'), function() {
372                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
373                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
374                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
375                 setTimeout(verifyEvaluation, 600);
376         });
377 });
378
379 test("load(String, Function) - check file with only a script tag", function() {
380         expect(3);
381         stop();
382
383         jQuery('#first').load(url('data/test2.html'), function() {
384                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
385                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
386
387                 start();
388         });
389 });
390
391 test("load(String, Object, Function)", function() {
392         expect(2);
393         stop();
394
395         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
396                 var $post = jQuery(this).find('#post');
397                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
398                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
399                 start();
400         });
401 });
402
403 test("load(String, String, Function)", function() {
404         expect(2);
405         stop();
406
407         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
408                 var $get = jQuery(this).find('#get');
409                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
410                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
411                 start();
412         });
413 });
414
415 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
416         expect(2);
417         stop();
418         jQuery.get(url('data/dashboard.xml'), function(xml) {
419                 var content = [];
420                 jQuery('tab', xml).each(function() {
421                         content.push(jQuery(this).text());
422                 });
423                 equals( content[0], 'blabla', 'Check first tab');
424                 equals( content[1], 'blublu', 'Check second tab');
425                 start();
426         });
427 });
428
429 test("jQuery.getScript(String, Function) - with callback", function() {
430         expect(2);
431         stop();
432         jQuery.getScript(url("data/test.js"), function() {
433                 equals( foobar, "bar", 'Check if script was evaluated' );
434                 setTimeout(start, 100);
435         });
436 });
437
438 test("jQuery.getScript(String, Function) - no callback", function() {
439         expect(1);
440         stop();
441         jQuery.getScript(url("data/test.js"), function(){
442                 start();
443         });
444 });
445
446 test("jQuery.ajax() - JSONP, Local", function() {
447         expect(7);
448
449         var count = 0;
450         function plus(){ if ( ++count == 7 ) start(); }
451
452         stop();
453
454         jQuery.ajax({
455                 url: "data/jsonp.php",
456                 dataType: "jsonp",
457                 success: function(data){
458                         ok( data.data, "JSON results returned (GET, no callback)" );
459                         plus();
460                 },
461                 error: function(data){
462                         ok( false, "Ajax error JSON (GET, no callback)" );
463                         plus();
464                 }
465         });
466
467         jQuery.ajax({
468                 url: "data/jsonp.php?callback=?",
469                 dataType: "jsonp",
470                 success: function(data){
471                         ok( data.data, "JSON results returned (GET, url callback)" );
472                         plus();
473                 },
474                 error: function(data){
475                         ok( false, "Ajax error JSON (GET, url callback)" );
476                         plus();
477                 }
478         });
479
480         jQuery.ajax({
481                 url: "data/jsonp.php",
482                 dataType: "jsonp",
483                 data: "callback=?",
484                 success: function(data){
485                         ok( data.data, "JSON results returned (GET, data callback)" );
486                         plus();
487                 },
488                 error: function(data){
489                         ok( false, "Ajax error JSON (GET, data callback)" );
490                         plus();
491                 }
492         });
493
494         jQuery.ajax({
495                 url: "data/jsonp.php",
496                 dataType: "jsonp",
497                 jsonp: "callback",
498                 success: function(data){
499                         ok( data.data, "JSON results returned (GET, data obj callback)" );
500                         plus();
501                 },
502                 error: function(data){
503                         ok( false, "Ajax error JSON (GET, data obj callback)" );
504                         plus();
505                 }
506         });
507
508         jQuery.ajax({
509                 type: "POST",
510                 url: "data/jsonp.php",
511                 dataType: "jsonp",
512                 success: function(data){
513                         ok( data.data, "JSON results returned (POST, no callback)" );
514                         plus();
515                 },
516                 error: function(data){
517                         ok( false, "Ajax error JSON (GET, data obj callback)" );
518                         plus();
519                 }
520         });
521
522         jQuery.ajax({
523                 type: "POST",
524                 url: "data/jsonp.php",
525                 data: "callback=?",
526                 dataType: "jsonp",
527                 success: function(data){
528                         ok( data.data, "JSON results returned (POST, data callback)" );
529                         plus();
530                 },
531                 error: function(data){
532                         ok( false, "Ajax error JSON (POST, data callback)" );
533                         plus();
534                 }
535         });
536
537         jQuery.ajax({
538                 type: "POST",
539                 url: "data/jsonp.php",
540                 jsonp: "callback",
541                 dataType: "jsonp",
542                 success: function(data){
543                         ok( data.data, "JSON results returned (POST, data obj callback)" );
544                         plus();
545                 },
546                 error: function(data){
547                         ok( false, "Ajax error JSON (POST, data obj callback)" );
548                         plus();
549                 }
550         });
551 });
552
553 test("jQuery.ajax() - JSONP, Remote", function() {
554         expect(4);
555
556         var count = 0;
557         function plus(){ if ( ++count == 4 ) start(); }
558
559         var base = window.location.href.replace(/\?.*$/, "");
560
561         stop();
562
563         jQuery.ajax({
564                 url: base + "data/jsonp.php",
565                 dataType: "jsonp",
566                 success: function(data){
567                         ok( data.data, "JSON results returned (GET, no callback)" );
568                         plus();
569                 },
570                 error: function(data){
571                         ok( false, "Ajax error JSON (GET, no callback)" );
572                         plus();
573                 }
574         });
575
576         jQuery.ajax({
577                 url: base + "data/jsonp.php?callback=?",
578                 dataType: "jsonp",
579                 success: function(data){
580                         ok( data.data, "JSON results returned (GET, url callback)" );
581                         plus();
582                 },
583                 error: function(data){
584                         ok( false, "Ajax error JSON (GET, url callback)" );
585                         plus();
586                 }
587         });
588
589         jQuery.ajax({
590                 url: base + "data/jsonp.php",
591                 dataType: "jsonp",
592                 data: "callback=?",
593                 success: function(data){
594                         ok( data.data, "JSON results returned (GET, data callback)" );
595                         plus();
596                 },
597                 error: function(data){
598                         ok( false, "Ajax error JSON (GET, data callback)" );
599                         plus();
600                 }
601         });
602
603         jQuery.ajax({
604                 url: base + "data/jsonp.php",
605                 dataType: "jsonp",
606                 jsonp: "callback",
607                 success: function(data){
608                         ok( data.data, "JSON results returned (GET, data obj callback)" );
609                         plus();
610                 },
611                 error: function(data){
612                         ok( false, "Ajax error JSON (GET, data obj callback)" );
613                         plus();
614                 }
615         });
616 });
617
618 test("jQuery.ajax() - script, Remote", function() {
619         expect(2);
620
621         var base = window.location.href.replace(/\?.*$/, "");
622
623         stop();
624
625         jQuery.ajax({
626                 url: base + "data/test.js",
627                 dataType: "script",
628                 success: function(data){
629                         ok( foobar, "Script results returned (GET, no callback)" );
630                         start();
631                 }
632         });
633 });
634
635 test("jQuery.ajax() - script, Remote with POST", function() {
636         expect(3);
637
638         var base = window.location.href.replace(/\?.*$/, "");
639
640         stop();
641
642         jQuery.ajax({
643                 url: base + "data/test.js",
644                 type: "POST",
645                 dataType: "script",
646                 success: function(data, status){
647                         ok( foobar, "Script results returned (POST, no callback)" );
648                         equals( status, "success", "Script results returned (POST, no callback)" );
649                         start();
650                 },
651                 error: function(xhr) {
652                         ok( false, "ajax error, status code: " + xhr.status );
653                         start();
654                 }
655         });
656 });
657
658 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
659         expect(2);
660
661         var base = window.location.href.replace(/\?.*$/, "");
662         base = base.replace(/^.*?\/\//, "//");
663
664         stop();
665
666         jQuery.ajax({
667                 url: base + "data/test.js",
668                 dataType: "script",
669                 success: function(data){
670                         ok( foobar, "Script results returned (GET, no callback)" );
671                         start();
672                 }
673         });
674 });
675
676 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
677         expect(4);
678         stop();
679         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
680           equals( json[0].name, 'John', 'Check JSON: first, name' );
681           equals( json[0].age, 21, 'Check JSON: first, age' );
682           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
683           equals( json[1].age, 25, 'Check JSON: second, age' );
684           start();
685         });
686 });
687
688 test("jQuery.getJSON(String, Function) - JSON object", function() {
689         expect(2);
690         stop();
691         jQuery.getJSON(url("data/json.php"), function(json) {
692           equals( json.data.lang, 'en', 'Check JSON: lang' );
693           equals( json.data.length, 25, 'Check JSON: length' );
694           start();
695         });
696 });
697
698 test("jQuery.getJSON - Using Native JSON", function() {
699         expect(2);
700         
701         var old = window.JSON;
702         JSON = {
703                 parse: function(str){
704                         ok( true, "Verifying that parse method was run" );
705                         return true;
706                 }
707         };
708
709         stop();
710         jQuery.getJSON(url("data/json.php"), function(json) {
711                 window.JSON = old;
712           equals( json, true, "Verifying return value" );
713           start();
714         });
715 });
716
717 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
718         expect(2);
719
720         var base = window.location.href.replace(/\?.*$/, "");
721
722         stop();
723         jQuery.getJSON(url(base + "data/json.php"), function(json) {
724           equals( json.data.lang, 'en', 'Check JSON: lang' );
725           equals( json.data.length, 25, 'Check JSON: length' );
726           start();
727         });
728 });
729
730 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
731         expect(4);
732         stop();
733         var done = 0;
734
735         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
736           jQuery('math', xml).each(function() {
737                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
738                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
739                  });
740           if ( ++done === 2 ) start();
741         });
742
743         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
744           jQuery('math', xml).each(function() {
745                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
746                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
747                  });
748           if ( ++done === 2 ) start();
749         });
750 });
751
752 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
753         stop();
754
755         var passed = 0;
756
757         jQuery.ajaxSetup({timeout: 1000});
758
759         var pass = function() {
760                 passed++;
761                 if ( passed == 2 ) {
762                         ok( true, 'Check local and global callbacks after timeout' );
763                         jQuery('#main').unbind("ajaxError");
764                         start();
765                 }
766         };
767
768         var fail = function(a,b,c) {
769                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
770                 start();
771         };
772
773         jQuery('#main').ajaxError(pass);
774
775         jQuery.ajax({
776           type: "GET",
777           url: url("data/name.php?wait=5"),
778           error: pass,
779           success: fail
780         });
781
782         // reset timeout
783         jQuery.ajaxSetup({timeout: 0});
784 });
785
786 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
787         stop();
788         jQuery.ajaxSetup({timeout: 50});
789
790         jQuery.ajax({
791           type: "GET",
792           timeout: 15000,
793           url: url("data/name.php?wait=1"),
794           error: function() {
795                    ok( false, 'Check for local timeout failed' );
796                    start();
797           },
798           success: function() {
799                 ok( true, 'Check for local timeout' );
800                 start();
801           }
802         });
803
804         // reset timeout
805         jQuery.ajaxSetup({timeout: 0});
806 });
807
808 test("jQuery.ajax - simple get", function() {
809         expect(1);
810         stop();
811         jQuery.ajax({
812           type: "GET",
813           url: url("data/name.php?name=foo"),
814           success: function(msg){
815                 equals( msg, 'bar', 'Check for GET' );
816                 start();
817           }
818         });
819 });
820
821 test("jQuery.ajax - simple post", function() {
822         expect(1);
823         stop();
824         jQuery.ajax({
825           type: "POST",
826           url: url("data/name.php"),
827           data: "name=peter",
828           success: function(msg){
829                 equals( msg, 'pan', 'Check for POST' );
830                 start();
831           }
832         });
833 });
834
835 test("ajaxSetup()", function() {
836         expect(1);
837         stop();
838         jQuery.ajaxSetup({
839                 url: url("data/name.php?name=foo"),
840                 success: function(msg){
841                         equals( msg, 'bar', 'Check for GET' );
842                         start();
843                 }
844         });
845         jQuery.ajax();
846 });
847
848 /*
849 test("custom timeout does not set error message when timeout occurs, see #970", function() {
850         stop();
851         jQuery.ajax({
852                 url: "data/name.php?wait=1",
853                 timeout: 500,
854                 error: function(request, status) {
855                         ok( status != null, "status shouldn't be null in error handler" );
856                         equals( "timeout", status );
857                         start();
858                 }
859         });
860 });
861 */
862
863 test("data option: evaluate function values (#2806)", function() {
864         stop();
865         jQuery.ajax({
866                 url: "data/echoQuery.php",
867                 data: {
868                         key: function() {
869                                 return "value";
870                         }
871                 },
872                 success: function(result) {
873                         equals( result, "key=value" );
874                         start();
875                 }
876         })
877 });
878
879 test("jQuery.ajax - If-Modified-Since support", function() {
880         expect( 3 );
881
882         stop();
883
884         var url = "data/if_modified_since.php?ts=" + new Date();
885
886         jQuery.ajax({
887                 url: url,
888                 ifModified: true,
889                 success: function(data, status) { 
890                         equals(status, "success");
891                         
892                         jQuery.ajax({
893                                 url: url,
894                                 ifModified: true,
895                                 success: function(data, status) { 
896                                         if ( data === "FAIL" ) {
897                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
898                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
899                                         } else {
900                                                 equals(status, "notmodified");
901                                                 ok(data == null, "response body should be empty")
902                                         }
903                                         start();
904                                 }
905                         });
906                 }
907         });
908 });
909
910 test("jQuery.ajax - Etag support", function() {
911         expect( 3 );
912
913         stop();
914
915         var url = "data/etag.php?ts=" + new Date();
916
917         jQuery.ajax({
918                 url: url,
919                 ifModified: true,
920                 success: function(data, status) { 
921                         equals(status, "success");
922                         
923                         jQuery.ajax({
924                                 url: url,
925                                 ifModified: true,
926                                 success: function(data, status) { 
927                                         if ( data === "FAIL" ) {
928                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
929                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
930                                         } else {
931                                                 equals(status, "notmodified");
932                                                 ok(data == null, "response body should be empty")
933                                         }
934                                         start();
935                                 }
936                         });
937                 }
938         });
939 });
940
941 }
942
943 //}