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