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