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