Make sure that null params aren't traversed. Fixes #5794.
[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(18);
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         params = { param1: null };
340         equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
341 });
342
343 test("synchronous request", function() {
344         expect(1);
345         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
346 });
347
348 test("synchronous request with callbacks", function() {
349         expect(2);
350         var result;
351         jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
352         ok( /^{ "data"/.test( result ), "check returned text" );
353 });
354
355 test("pass-through request object", function() {
356         expect(8);
357         stop();
358
359         var target = "data/name.html";
360         var successCount = 0;
361         var errorCount = 0;
362         var errorEx = "";
363         var success = function() {
364                 successCount++;
365         };
366         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
367                 errorCount++;
368                 errorEx += ": " + xml.status;
369         });
370         jQuery("#foo").one('ajaxStop', function () {
371                 equals(successCount, 5, "Check all ajax calls successful");
372                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
373                 jQuery("#foo").unbind('ajaxError');
374
375                 start();
376         });
377
378         ok( jQuery.get(url(target), success), "get" );
379         ok( jQuery.post(url(target), success), "post" );
380         ok( jQuery.getScript(url("data/test.js"), success), "script" );
381         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
382         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
383 });
384
385 test("ajax cache", function () {
386         expect(18);
387         
388         stop();
389
390         var count = 0;
391
392         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
393                 var re = /_=(.*?)(&|$)/g;
394                 var oldOne = null;
395                 for (var i = 0; i < 6; i++) {
396                         var ret = re.exec(s.url);
397                         if (!ret) {
398                                 break;
399                         }
400                         oldOne = ret[1];
401                 }
402                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
403                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
404                 if(++count == 6)
405                         start();
406         });
407
408         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
409         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
410         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
411         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
412         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
413         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
414 });
415
416 /*
417  * Test disabled.
418  * The assertions expect that the passed-in object will be modified,
419  * which shouldn't be the case. Fixes #5439.
420 test("global ajaxSettings", function() {
421         expect(2);
422
423         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
424         var orig = { url: "data/with_fries.xml" };
425         var t;
426
427         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
428
429         t = jQuery.extend({}, orig);
430         t.data = {};
431         jQuery.ajax(t);
432         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
433
434         t = jQuery.extend({}, orig);
435         t.data = { zoo: 'a', ping: 'b' };
436         jQuery.ajax(t);
437         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' }" );
438
439         jQuery.ajaxSettings = tmp;
440 });
441 */
442
443 test("load(String)", function() {
444         expect(1);
445         stop(); // check if load can be called with only url
446         jQuery('#first').load("data/name.html", start);
447 });
448
449 test("load('url selector')", function() {
450         expect(1);
451         stop(); // check if load can be called with only url
452         jQuery('#first').load("data/test3.html div.user", function(){
453                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
454                 start();
455         });
456 });
457
458 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
459         expect(1);
460         stop();
461         jQuery.ajaxSetup({ dataType: "json" });
462         jQuery("#first").ajaxComplete(function (e, xml, s) {
463                 equals( s.dataType, "html", "Verify the load() dataType was html" );
464                 jQuery("#first").unbind("ajaxComplete");
465                 jQuery.ajaxSetup({ dataType: "" });
466                 start();
467         });
468         jQuery('#first').load("data/test3.html");
469 });
470
471 test("load(String, Function) - simple: inject text into DOM", function() {
472         expect(2);
473         stop();
474         jQuery('#first').load(url("data/name.html"), function() {
475                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
476                 start();
477         });
478 });
479
480 test("load(String, Function) - check scripts", function() {
481         expect(7);
482         stop();
483
484         var verifyEvaluation = function() {
485                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
486                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
487
488                 start();
489         };
490         jQuery('#first').load(url('data/test.html'), function() {
491                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
492                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
493                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
494                 setTimeout(verifyEvaluation, 600);
495         });
496 });
497
498 test("load(String, Function) - check file with only a script tag", function() {
499         expect(3);
500         stop();
501
502         jQuery('#first').load(url('data/test2.html'), function() {
503                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
504                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
505
506                 start();
507         });
508 });
509
510 test("load(String, Object, Function)", function() {
511         expect(2);
512         stop();
513
514         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
515                 var $post = jQuery(this).find('#post');
516                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
517                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
518                 start();
519         });
520 });
521
522 test("load(String, String, Function)", function() {
523         expect(2);
524         stop();
525
526         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
527                 var $get = jQuery(this).find('#get');
528                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
529                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
530                 start();
531         });
532 });
533
534 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
535         expect(2);
536         stop();
537         jQuery.get(url('data/dashboard.xml'), function(xml) {
538                 var content = [];
539                 jQuery('tab', xml).each(function() {
540                         content.push(jQuery(this).text());
541                 });
542                 equals( content[0], 'blabla', 'Check first tab');
543                 equals( content[1], 'blublu', 'Check second tab');
544                 start();
545         });
546 });
547
548 test("jQuery.getScript(String, Function) - with callback", function() {
549         expect(2);
550         stop();
551         jQuery.getScript(url("data/test.js"), function() {
552                 equals( foobar, "bar", 'Check if script was evaluated' );
553                 setTimeout(start, 100);
554         });
555 });
556
557 test("jQuery.getScript(String, Function) - no callback", function() {
558         expect(1);
559         stop();
560         jQuery.getScript(url("data/test.js"), function(){
561                 start();
562         });
563 });
564
565 test("jQuery.ajax() - JSONP, Local", function() {
566         expect(8);
567
568         var count = 0;
569         function plus(){ if ( ++count == 8 ) start(); }
570
571         stop();
572
573         jQuery.ajax({
574                 url: "data/jsonp.php",
575                 dataType: "jsonp",
576                 success: function(data){
577                         ok( data.data, "JSON results returned (GET, no callback)" );
578                         plus();
579                 },
580                 error: function(data){
581                         ok( false, "Ajax error JSON (GET, no callback)" );
582                         plus();
583                 }
584         });
585
586         jQuery.ajax({
587                 url: "data/jsonp.php?callback=?",
588                 dataType: "jsonp",
589                 success: function(data){
590                         ok( data.data, "JSON results returned (GET, url callback)" );
591                         plus();
592                 },
593                 error: function(data){
594                         ok( false, "Ajax error JSON (GET, url callback)" );
595                         plus();
596                 }
597         });
598
599         jQuery.ajax({
600                 url: "data/jsonp.php",
601                 dataType: "jsonp",
602                 data: "callback=?",
603                 success: function(data){
604                         ok( data.data, "JSON results returned (GET, data callback)" );
605                         plus();
606                 },
607                 error: function(data){
608                         ok( false, "Ajax error JSON (GET, data callback)" );
609                         plus();
610                 }
611         });
612
613         jQuery.ajax({
614                 url: "data/jsonp.php",
615                 dataType: "jsonp",
616                 jsonp: "callback",
617                 success: function(data){
618                         ok( data.data, "JSON results returned (GET, data obj callback)" );
619                         plus();
620                 },
621                 error: function(data){
622                         ok( false, "Ajax error JSON (GET, data obj callback)" );
623                         plus();
624                 }
625         });
626
627         jQuery.ajax({
628                 url: "data/jsonp.php",
629                 dataType: "jsonp",
630                 jsonpCallback: "jsonpResults",
631                 success: function(data){
632                         ok( data.data, "JSON results returned (GET, custom callback name)" );
633                         plus();
634                 },
635                 error: function(data){
636                         ok( false, "Ajax error JSON (GET, custom callback name)" );
637                         plus();
638                 }
639         });
640
641         jQuery.ajax({
642                 type: "POST",
643                 url: "data/jsonp.php",
644                 dataType: "jsonp",
645                 success: function(data){
646                         ok( data.data, "JSON results returned (POST, no callback)" );
647                         plus();
648                 },
649                 error: function(data){
650                         ok( false, "Ajax error JSON (GET, data obj callback)" );
651                         plus();
652                 }
653         });
654
655         jQuery.ajax({
656                 type: "POST",
657                 url: "data/jsonp.php",
658                 data: "callback=?",
659                 dataType: "jsonp",
660                 success: function(data){
661                         ok( data.data, "JSON results returned (POST, data callback)" );
662                         plus();
663                 },
664                 error: function(data){
665                         ok( false, "Ajax error JSON (POST, data callback)" );
666                         plus();
667                 }
668         });
669
670         jQuery.ajax({
671                 type: "POST",
672                 url: "data/jsonp.php",
673                 jsonp: "callback",
674                 dataType: "jsonp",
675                 success: function(data){
676                         ok( data.data, "JSON results returned (POST, data obj callback)" );
677                         plus();
678                 },
679                 error: function(data){
680                         ok( false, "Ajax error JSON (POST, data obj callback)" );
681                         plus();
682                 }
683         });
684 });
685
686 test("JSONP - Custom JSONP Callback", function() {
687         expect(1);
688         stop();
689
690         window.jsonpResults = function(data) {
691                 ok( data.data, "JSON results returned (GET, custom callback function)" );
692                 start();
693         };
694
695         jQuery.ajax({
696                 url: "data/jsonp.php",
697                 dataType: "jsonp",
698                 jsonpCallback: "jsonpResults"
699         });
700 });
701
702 test("jQuery.ajax() - JSONP, Remote", function() {
703         expect(4);
704
705         var count = 0;
706         function plus(){ if ( ++count == 4 ) start(); }
707
708         var base = window.location.href.replace(/\?.*$/, "");
709
710         stop();
711
712         jQuery.ajax({
713                 url: base + "data/jsonp.php",
714                 dataType: "jsonp",
715                 success: function(data){
716                         ok( data.data, "JSON results returned (GET, no callback)" );
717                         plus();
718                 },
719                 error: function(data){
720                         ok( false, "Ajax error JSON (GET, no callback)" );
721                         plus();
722                 }
723         });
724
725         jQuery.ajax({
726                 url: base + "data/jsonp.php?callback=?",
727                 dataType: "jsonp",
728                 success: function(data){
729                         ok( data.data, "JSON results returned (GET, url callback)" );
730                         plus();
731                 },
732                 error: function(data){
733                         ok( false, "Ajax error JSON (GET, url callback)" );
734                         plus();
735                 }
736         });
737
738         jQuery.ajax({
739                 url: base + "data/jsonp.php",
740                 dataType: "jsonp",
741                 data: "callback=?",
742                 success: function(data){
743                         ok( data.data, "JSON results returned (GET, data callback)" );
744                         plus();
745                 },
746                 error: function(data){
747                         ok( false, "Ajax error JSON (GET, data callback)" );
748                         plus();
749                 }
750         });
751
752         jQuery.ajax({
753                 url: base + "data/jsonp.php",
754                 dataType: "jsonp",
755                 jsonp: "callback",
756                 success: function(data){
757                         ok( data.data, "JSON results returned (GET, data obj callback)" );
758                         plus();
759                 },
760                 error: function(data){
761                         ok( false, "Ajax error JSON (GET, data obj callback)" );
762                         plus();
763                 }
764         });
765 });
766
767 test("jQuery.ajax() - script, Remote", function() {
768         expect(2);
769
770         var base = window.location.href.replace(/\?.*$/, "");
771
772         stop();
773
774         jQuery.ajax({
775                 url: base + "data/test.js",
776                 dataType: "script",
777                 success: function(data){
778                         ok( foobar, "Script results returned (GET, no callback)" );
779                         start();
780                 }
781         });
782 });
783
784 test("jQuery.ajax() - script, Remote with POST", function() {
785         expect(3);
786
787         var base = window.location.href.replace(/\?.*$/, "");
788
789         stop();
790
791         jQuery.ajax({
792                 url: base + "data/test.js",
793                 type: "POST",
794                 dataType: "script",
795                 success: function(data, status){
796                         ok( foobar, "Script results returned (POST, no callback)" );
797                         equals( status, "success", "Script results returned (POST, no callback)" );
798                         start();
799                 },
800                 error: function(xhr) {
801                         ok( false, "ajax error, status code: " + xhr.status );
802                         start();
803                 }
804         });
805 });
806
807 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
808         expect(2);
809
810         var base = window.location.href.replace(/\?.*$/, "");
811         base = base.replace(/^.*?\/\//, "//");
812
813         stop();
814
815         jQuery.ajax({
816                 url: base + "data/test.js",
817                 dataType: "script",
818                 success: function(data){
819                         ok( foobar, "Script results returned (GET, no callback)" );
820                         start();
821                 }
822         });
823 });
824
825 test("jQuery.ajax() - malformed JSON", function() {
826         expect(1);
827
828         stop();
829
830         jQuery.ajax({
831                 url: "data/badjson.js",
832                 dataType: "json",
833                 success: function(){
834                         ok( false, "Success." );
835                         start();
836                 },
837                 error: function(xhr, msg) {
838                         equals( "parsererror", msg, "A parse error occurred." );
839                         start();
840                 }
841         });
842 });
843
844 test("jQuery.ajax() - script by content-type", function() {
845         expect(1);
846
847         stop();
848
849         jQuery.ajax({
850                 url: "data/script.php",
851                 data: { header: "script" },
852                 success: function() {
853                         start();
854                 }
855         });
856 });
857
858 test("jQuery.ajax() - json by content-type", function() {
859         expect(5);
860
861         stop();
862
863         jQuery.ajax({
864                 url: "data/json.php",
865                 data: { header: "json", json: "array" },
866                 success: function( json ) {
867                         ok( json.length >= 2, "Check length");
868                         equals( json[0].name, 'John', 'Check JSON: first, name' );
869                         equals( json[0].age, 21, 'Check JSON: first, age' );
870                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
871                         equals( json[1].age, 25, 'Check JSON: second, age' );
872                         start();
873                 }
874         });
875 });
876
877 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
878         expect(5);
879         stop();
880         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
881           ok( json.length >= 2, "Check length");
882           equals( json[0].name, 'John', 'Check JSON: first, name' );
883           equals( json[0].age, 21, 'Check JSON: first, age' );
884           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
885           equals( json[1].age, 25, 'Check JSON: second, age' );
886           start();
887         });
888 });
889
890 test("jQuery.getJSON(String, Function) - JSON object", function() {
891         expect(2);
892         stop();
893         jQuery.getJSON(url("data/json.php"), function(json) {
894           if (json && json.data) {
895                   equals( json.data.lang, 'en', 'Check JSON: lang' );
896                   equals( json.data.length, 25, 'Check JSON: length' );
897           }
898           start();
899         });
900 });
901
902 test("jQuery.getJSON - Using Native JSON", function() {
903         expect(2);
904         
905         var old = window.JSON;
906         JSON = {
907                 parse: function(str){
908                         ok( true, "Verifying that parse method was run" );
909                         return true;
910                 }
911         };
912
913         stop();
914         jQuery.getJSON(url("data/json.php"), function(json) {
915                 window.JSON = old;
916                 equals( json, true, "Verifying return value" );
917                 start();
918         });
919 });
920
921 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
922         expect(2);
923
924         var base = window.location.href.replace(/\?.*$/, "");
925
926         stop();
927         jQuery.getJSON(url(base + "data/json.php"), function(json) {
928           equals( json.data.lang, 'en', 'Check JSON: lang' );
929           equals( json.data.length, 25, 'Check JSON: length' );
930           start();
931         });
932 });
933
934 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
935         expect(4);
936         stop();
937         var done = 0;
938
939         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
940           jQuery('math', xml).each(function() {
941                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
942                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
943                  });
944           if ( ++done === 2 ) start();
945         });
946
947         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
948           jQuery('math', xml).each(function() {
949                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
950                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
951                  });
952           if ( ++done === 2 ) start();
953         });
954 });
955
956 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
957         stop();
958
959         var passed = 0;
960
961         jQuery.ajaxSetup({timeout: 1000});
962
963         var pass = function() {
964                 passed++;
965                 if ( passed == 2 ) {
966                         ok( true, 'Check local and global callbacks after timeout' );
967                         jQuery('#main').unbind("ajaxError");
968                         start();
969                 }
970         };
971
972         var fail = function(a,b,c) {
973                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
974                 start();
975         };
976
977         jQuery('#main').ajaxError(pass);
978
979         jQuery.ajax({
980           type: "GET",
981           url: url("data/name.php?wait=5"),
982           error: pass,
983           success: fail
984         });
985
986         // reset timeout
987         jQuery.ajaxSetup({timeout: 0});
988 });
989
990 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
991         stop();
992         jQuery.ajaxSetup({timeout: 50});
993
994         jQuery.ajax({
995           type: "GET",
996           timeout: 15000,
997           url: url("data/name.php?wait=1"),
998           error: function() {
999                    ok( false, 'Check for local timeout failed' );
1000                    start();
1001           },
1002           success: function() {
1003                 ok( true, 'Check for local timeout' );
1004                 start();
1005           }
1006         });
1007
1008         // reset timeout
1009         jQuery.ajaxSetup({timeout: 0});
1010 });
1011
1012 test("jQuery.ajax - simple get", function() {
1013         expect(1);
1014         stop();
1015         jQuery.ajax({
1016           type: "GET",
1017           url: url("data/name.php?name=foo"),
1018           success: function(msg){
1019                 equals( msg, 'bar', 'Check for GET' );
1020                 start();
1021           }
1022         });
1023 });
1024
1025 test("jQuery.ajax - simple post", function() {
1026         expect(1);
1027         stop();
1028         jQuery.ajax({
1029           type: "POST",
1030           url: url("data/name.php"),
1031           data: "name=peter",
1032           success: function(msg){
1033                 equals( msg, 'pan', 'Check for POST' );
1034                 start();
1035           }
1036         });
1037 });
1038
1039 test("ajaxSetup()", function() {
1040         expect(1);
1041         stop();
1042         jQuery.ajaxSetup({
1043                 url: url("data/name.php?name=foo"),
1044                 success: function(msg){
1045                         equals( msg, 'bar', 'Check for GET' );
1046                         start();
1047                 }
1048         });
1049         jQuery.ajax();
1050 });
1051
1052 /*
1053 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1054         stop();
1055         jQuery.ajax({
1056                 url: "data/name.php?wait=1",
1057                 timeout: 500,
1058                 error: function(request, status) {
1059                         ok( status != null, "status shouldn't be null in error handler" );
1060                         equals( "timeout", status );
1061                         start();
1062                 }
1063         });
1064 });
1065 */
1066
1067 test("data option: evaluate function values (#2806)", function() {
1068         stop();
1069         jQuery.ajax({
1070                 url: "data/echoQuery.php",
1071                 data: {
1072                         key: function() {
1073                                 return "value";
1074                         }
1075                 },
1076                 success: function(result) {
1077                         equals( result, "key=value" );
1078                         start();
1079                 }
1080         })
1081 });
1082
1083 test("jQuery.ajax - If-Modified-Since support", function() {
1084         expect( 3 );
1085
1086         stop();
1087
1088         var url = "data/if_modified_since.php?ts=" + new Date();
1089
1090         jQuery.ajax({
1091                 url: url,
1092                 ifModified: true,
1093                 success: function(data, status) { 
1094                         equals(status, "success");
1095                         
1096                         jQuery.ajax({
1097                                 url: url,
1098                                 ifModified: true,
1099                                 success: function(data, status) { 
1100                                         if ( data === "FAIL" ) {
1101                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1102                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1103                                         } else {
1104                                                 equals(status, "notmodified");
1105                                                 ok(data == null, "response body should be empty")
1106                                         }
1107                                         start();
1108                                 }
1109                         });
1110                 }
1111         });
1112 });
1113
1114 test("jQuery.ajax - Etag support", function() {
1115         expect( 3 );
1116
1117         stop();
1118
1119         var url = "data/etag.php?ts=" + new Date();
1120
1121         jQuery.ajax({
1122                 url: url,
1123                 ifModified: true,
1124                 success: function(data, status) { 
1125                         equals(status, "success");
1126                         
1127                         jQuery.ajax({
1128                                 url: url,
1129                                 ifModified: true,
1130                                 success: function(data, status) { 
1131                                         if ( data === "FAIL" ) {
1132                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1133                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1134                                         } else {
1135                                                 equals(status, "notmodified");
1136                                                 ok(data == null, "response body should be empty")
1137                                         }
1138                                         start();
1139                                 }
1140                         });
1141                 }
1142         });
1143 });
1144
1145 }
1146
1147 //}