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