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