Reorganzing the jQuery source (first phase).
[jquery.git] / test / unit / ajaxTest.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 test("$.ajax() - success callbacks", function() {
9         expect( 8 );
10         
11         $.ajaxSetup({ timeout: 0 });
12         
13         stop();
14         
15         setTimeout(function(){  
16         $('#foo').ajaxStart(function(){
17             ok( true, "ajaxStart" );
18         }).ajaxStop(function(){
19             ok( true, "ajaxStop" );
20             start();
21         }).ajaxSend(function(){
22             ok( true, "ajaxSend" );
23         }).ajaxComplete(function(){
24             ok( true, "ajaxComplete" );
25         }).ajaxError(function(){
26             ok( false, "ajaxError" );
27         }).ajaxSuccess(function(){
28             ok( true, "ajaxSuccess" );
29         });
30         
31         $.ajax({
32             url: url("data/name.html"),
33             beforeSend: function(){ ok(true, "beforeSend"); },
34             success: function(){ ok(true, "success"); },
35             error: function(){ ok(false, "error"); },
36             complete: function(){ ok(true, "complete"); }
37         });
38     }, 13);
39 });
40
41 if ( !isLocal ) {
42         test("$.ajax() - error callbacks", function() {
43                 expect( 8 );
44                 stop();
45                 
46                 $('#foo').ajaxStart(function(){
47                         ok( true, "ajaxStart" );
48                 }).ajaxStop(function(){
49                         ok( true, "ajaxStop" );
50                         start();
51                 }).ajaxSend(function(){
52                         ok( true, "ajaxSend" );
53                 }).ajaxComplete(function(){
54                         ok( true, "ajaxComplete" );
55                 }).ajaxError(function(){
56                         ok( true, "ajaxError" );
57                 }).ajaxSuccess(function(){
58                         ok( false, "ajaxSuccess" );
59                 });
60                 
61                 $.ajaxSetup({ timeout: 500 });
62                 
63                 $.ajax({
64                         url: url("data/name.php?wait=5"),
65                         beforeSend: function(){ ok(true, "beforeSend"); },
66                         success: function(){ ok(false, "success"); },
67                         error: function(){ ok(true, "error"); },
68                         complete: function(){ ok(true, "complete"); }
69                 });
70         });
71 }
72
73 test("$.ajax() - disabled globals", function() {
74         expect( 3 );
75         stop();
76         
77         $('#foo').ajaxStart(function(){
78                 ok( false, "ajaxStart" );
79         }).ajaxStop(function(){
80                 ok( false, "ajaxStop" );
81         }).ajaxSend(function(){
82                 ok( false, "ajaxSend" );
83         }).ajaxComplete(function(){
84                 ok( false, "ajaxComplete" );
85         }).ajaxError(function(){
86                 ok( false, "ajaxError" );
87         }).ajaxSuccess(function(){
88                 ok( false, "ajaxSuccess" );
89         });
90         
91         $.ajax({
92                 global: false,
93                 url: url("data/name.html"),
94                 beforeSend: function(){ ok(true, "beforeSend"); },
95                 success: function(){ ok(true, "success"); },
96                 error: function(){ ok(false, "error"); },
97                 complete: function(){
98                   ok(true, "complete");
99                   setTimeout(function(){ start(); }, 13);
100         }
101         });
102 });
103
104 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
105         expect(3);
106         stop();
107         $.ajax({
108           url: url("data/with_fries.xml"),
109           dataType: "xml",
110           success: function(resp) {
111             equals( $("properties", resp).length, 1, 'properties in responseXML' );
112             equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
113             equals( $("thing", resp).length, 2, 'things in responseXML' );
114             start();
115           }
116         });
117 });
118
119 test("$.ajax - beforeSend", function() {
120         expect(1);
121         stop();
122         
123         var check = false;
124         
125         $.ajaxSetup({ timeout: 0 });
126         
127         $.ajax({
128                 url: url("data/name.html"), 
129                 beforeSend: function(xml) {
130                         check = true;
131                 },
132                 success: function(data) {
133                         ok( check, "check beforeSend was executed" );
134                         start();
135                 }
136         });
137 });
138
139 var foobar;
140
141 test("$.ajax - dataType html", function() {
142         expect(5);
143         stop();
144         
145         foobar = null;
146         testFoo = undefined;
147
148         var verifyEvaluation = function() {
149           ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
150           ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
151           start();
152         };
153
154         $.ajax({
155           dataType: "html",
156           url: url("data/test.html"),
157           success: function(data) {
158                 $("#ap").html(data);
159             ok( data.match(/^html text/), 'Check content for datatype html' );
160             setTimeout(verifyEvaluation, 600);
161           }
162         });
163 });
164
165 test("serialize()", function() {
166         expect(6);
167         
168         equals( $('#form').serialize(),
169                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1",
170                 'Check form serialization as query string');
171                 
172         equals( $('#form :input').serialize(),
173                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1",
174                 'Check input serialization as query string');
175         
176         equals( $('#testForm').serialize(), 
177                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', 
178                 'Check form serialization as query string');
179                 
180         equals( $('#testForm :input').serialize(), 
181                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', 
182                 'Check input serialization as query string');
183                 
184         equals( $('#form, #testForm').serialize(),
185                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
186                 'Multiple form serialization as query string');
187                 
188         equals( $('#form, #testForm :input').serialize(),
189                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
190                 'Mixed form/input serialization as query string');
191 });
192
193 test("$.param()", function() {
194         expect(4);
195         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
196         equals( $.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
197         
198         params = {someName: [1, 2, 3], regularThing: "blah" };
199         equals( $.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
200         
201         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
202         equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
203         
204         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
205         equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
206 });
207
208 test("synchronous request", function() {
209         expect(1);
210         ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
211 });
212
213 test("synchronous request with callbacks", function() {
214         expect(2);
215         var result;
216         $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
217         ok( /^{ "data"/.test( result ), "check returned text" );
218 });
219
220 test("pass-through request object", function() {
221         expect(1);
222         stop(true);
223         
224         var target = "data/name.html";
225         var count = 0;
226         var success = function() {
227                 // Disabled
228                 //if(count++ == 5)
229                 start();
230         };
231         
232         /* Test disabled, too many simultaneous requests
233         ok( $.get(url(target), success), "get" );
234         ok( $.post(url(target), success), "post" );
235         ok( $.getScript(url("data/test.js"), success), "script" );
236         ok( $.getJSON(url("data/json_obj.js"), success), "json" );
237         */
238         ok( $.ajax({url: url(target), success: success}), "generic" );
239 });
240
241 test("global ajaxSettings", function() {
242         expect(3);
243
244         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
245     var orig = { url: "data/with_fries.xml", data: null };
246         var t;
247
248         $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
249
250     t = jQuery.extend({}, orig);
251     $.ajax(t);
252         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
253
254     t = jQuery.extend({}, orig);
255         t.data = {};
256     $.ajax(t);
257         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
258
259     t = jQuery.extend({}, orig);
260         t.data = { zoo: 'a', ping: 'b' };
261     $.ajax(t);
262         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' }" );
263         
264         jQuery.ajaxSettings = tmp;
265 });
266
267 test("load(String)", function() {
268         expect(1);
269         stop(true); // check if load can be called with only url
270         $('#first').load("data/name.html", start);
271 });
272
273 test("load('url selector')", function() {
274         expect(1);
275         stop(true); // check if load can be called with only url
276         $('#first').load("data/test3.html div.user", function(){
277                 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
278                 start();
279         });
280 });
281
282 test("load(String, Function) - simple: inject text into DOM", function() {
283         expect(2);
284         stop();
285         $('#first').load(url("data/name.html"), function() {
286                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
287                 start();
288         });
289 });
290
291 test("load(String, Function) - check scripts", function() {
292         expect(7);
293         stop();
294         window.testFoo = undefined;
295         window.foobar = null;
296         var verifyEvaluation = function() {
297           equals( foobar, "bar", 'Check if script src was evaluated after load' );
298           equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
299           start();
300         };
301         $('#first').load(url('data/test.html'), function() {
302           ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
303           equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
304           equals( testFoo, "foo", 'Check if script was evaluated after load' );
305           setTimeout(verifyEvaluation, 600);
306         });
307 });
308
309 test("load(String, Function) - check file with only a script tag", function() {
310         expect(3);
311         stop();
312         testFoo = undefined;
313         $('#first').load(url('data/test2.html'), function() {
314           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
315           ok( testFoo == "foo", 'Check if script was evaluated after load' );
316           start();
317         });
318 });
319
320 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
321         expect(2);
322         stop();
323         $.get(url('data/dashboard.xml'), function(xml) {
324                 var content = [];
325                 $('tab', xml).each(function() {
326                         content.push($(this).text());
327                 });
328                 equals( content[0], 'blabla', 'Check first tab');
329                 equals( content[1], 'blublu', 'Check second tab');
330                 start();
331         });
332 });
333
334 test("$.getScript(String, Function) - with callback", function() {
335         expect(2);
336         stop();
337         $.getScript(url("data/test.js"), function() {
338                 equals( foobar, "bar", 'Check if script was evaluated' );
339                 setTimeout(start, 100);
340         });
341 });
342
343 test("$.getScript(String, Function) - no callback", function() {
344         expect(1);
345         stop(true);
346         $.getScript(url("data/test.js"), start);
347 });
348
349 if ( !isLocal ) {
350
351 test("$.ajax() - JSONP, Local", function() {
352         expect(7);
353
354         var count = 0;
355         function plus(){ if ( ++count == 7 ) start(); }
356
357         stop();
358
359         $.ajax({
360                 url: "data/jsonp.php",
361                 dataType: "jsonp",
362                 success: function(data){
363                         ok( data.data, "JSON results returned (GET, no callback)" );
364                         plus();
365                 }
366         });
367
368         $.ajax({
369                 url: "data/jsonp.php?callback=?",
370                 dataType: "jsonp",
371                 success: function(data){
372                         ok( data.data, "JSON results returned (GET, url callback)" );
373                         plus();
374                 }
375         });
376
377         $.ajax({
378                 url: "data/jsonp.php",
379                 dataType: "jsonp",
380                 data: "callback=?",
381                 success: function(data){
382                         ok( data.data, "JSON results returned (GET, data callback)" );
383                         plus();
384                 }
385         });
386
387         $.ajax({
388                 url: "data/jsonp.php",
389                 dataType: "jsonp",
390                 data: { callback: "?" },
391                 success: function(data){
392                         ok( data.data, "JSON results returned (GET, data obj callback)" );
393                         plus();
394                 }
395         });
396
397         $.ajax({
398                 type: "POST",
399                 url: "data/jsonp.php",
400                 dataType: "jsonp",
401                 success: function(data){
402                         ok( data.data, "JSON results returned (POST, no callback)" );
403                         plus();
404                 }
405         });
406
407         $.ajax({
408                 type: "POST",
409                 url: "data/jsonp.php",
410                 data: "callback=?",
411                 dataType: "jsonp",
412                 success: function(data){
413                         ok( data.data, "JSON results returned (POST, data callback)" );
414                         plus();
415                 }
416         });
417
418         $.ajax({
419                 type: "POST",
420                 url: "data/jsonp.php",
421                 data: { callback: "?" },
422                 dataType: "jsonp",
423                 success: function(data){
424                         ok( data.data, "JSON results returned (POST, data obj callback)" );
425                         plus();
426                 }
427         });
428 });
429
430 test("$.ajax() - JSONP, Remote", function() {
431         expect(4);
432
433         var count = 0;
434         function plus(){ if ( ++count == 4 ) start(); }
435
436         var base = window.location.href.replace(/\?.*$/, "");
437
438         stop();
439
440         $.ajax({
441                 url: base + "data/jsonp.php",
442                 dataType: "jsonp",
443                 success: function(data){
444                         ok( data.data, "JSON results returned (GET, no callback)" );
445                         plus();
446                 }
447         });
448
449         $.ajax({
450                 url: base + "data/jsonp.php?callback=?",
451                 dataType: "jsonp",
452                 success: function(data){
453                         ok( data.data, "JSON results returned (GET, url callback)" );
454                         plus();
455                 }
456         });
457
458         $.ajax({
459                 url: base + "data/jsonp.php",
460                 dataType: "jsonp",
461                 data: "callback=?",
462                 success: function(data){
463                         ok( data.data, "JSON results returned (GET, data callback)" );
464                         plus();
465                 }
466         });
467
468         $.ajax({
469                 url: base + "data/jsonp.php",
470                 dataType: "jsonp",
471                 data: { callback: "?" },
472                 success: function(data){
473                         ok( data.data, "JSON results returned (GET, data obj callback)" );
474                         plus();
475                 }
476         });
477 });
478
479 test("$.ajax() - script, Remote", function() {
480         expect(2);
481
482         var base = window.location.href.replace(/\?.*$/, "");
483
484         stop();
485
486         $.ajax({
487                 url: base + "data/test.js",
488                 dataType: "script",
489                 success: function(data){
490                         ok( foobar, "Script results returned (GET, no callback)" );
491                         start();
492                 }
493         });
494 });
495
496 test("$.getJSON(String, Hash, Function) - JSON array", function() {
497         expect(4);
498         stop();
499         $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
500           ok( json[0].name == 'John', 'Check JSON: first, name' );
501           ok( json[0].age == 21, 'Check JSON: first, age' );
502           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
503           ok( json[1].age == 25, 'Check JSON: second, age' );
504           start();
505         });
506 });
507
508 test("$.getJSON(String, Function) - JSON object", function() {
509         expect(2);
510         stop();
511         $.getJSON(url("data/json.php"), function(json) {
512           ok( json.data.lang == 'en', 'Check JSON: lang' );
513           ok( json.data.length == 25, 'Check JSON: length' );
514           start();
515         });
516 });
517
518 test("$.post(String, Hash, Function) - simple with xml", function() {
519         expect(2);
520         stop();
521         $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
522           $('math', xml).each(function() {
523                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
524                     ok( $('result', this).text() == '3', 'Check for XML' );
525                  });
526           start();
527         });
528 });
529
530 test("$.ajaxSetup({timeout: Number}) - with global timeout", function() {
531         stop();
532         
533         var passed = 0;
534
535         $.ajaxSetup({timeout: 1000});
536         
537         var pass = function() {
538                 passed++;
539                 if ( passed == 2 ) {
540                         ok( true, 'Check local and global callbacks after timeout' );
541                 $('#main').unbind("ajaxError");
542                         start();
543                 }
544         };
545         
546         var fail = function(a,b,c) {
547                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
548                 start();
549         };
550         
551         $('#main').ajaxError(pass);
552         
553         $.ajax({
554           type: "GET",
555           url: url("data/name.php?wait=5"),
556           error: pass,
557           success: fail
558         });
559         
560         // reset timeout
561         $.ajaxSetup({timeout: 0});
562 });
563
564 test("$.ajaxSetup({timeout: Number}) with localtimeout", function() {
565         stop();
566         $.ajaxSetup({timeout: 50});
567
568         $.ajax({
569           type: "GET",
570           timeout: 5000,
571           url: url("data/name.php?wait=1"),
572           error: function() {
573                    ok( false, 'Check for local timeout failed' );
574                    start();
575           },
576           success: function() {
577             ok( true, 'Check for local timeout' );
578             start();
579           }
580         });
581
582         // reset timeout
583         $.ajaxSetup({timeout: 0});
584 });
585
586 test("$.ajax - simple get", function() {
587         expect(1);
588         stop();
589         $.ajax({
590           type: "GET",
591           url: url("data/name.php?name=foo"),
592           success: function(msg){
593             ok( msg == 'bar', 'Check for GET' );
594             start();
595           }
596         });
597 });
598
599 test("$.ajax - simple post", function() {
600         expect(1);
601         stop();
602         $.ajax({
603           type: "POST",
604           url: url("data/name.php"),
605           data: "name=peter",
606           success: function(msg){
607             ok( msg == 'pan', 'Check for POST' );
608             start();
609           }
610         });
611 });
612
613 test("ajaxSetup()", function() {
614         expect(1);
615         stop();
616         $.ajaxSetup({
617                 url: url("data/name.php?name=foo"),
618                 success: function(msg){
619                 ok( msg == 'bar', 'Check for GET' );
620                         start();
621                 }
622         });
623         $.ajax();
624 });
625
626 test("custom timeout does not set error message when timeout occurs, see #970", function() {
627         stop();
628         $.ajax({
629                 url: "data/name.php?wait=10",
630                 timeout: 500,
631                 error: function(request, status) {
632                         ok( status != null, "status shouldn't be null in error handler" );
633                         equals( "timeout", status );
634                         start();
635                 }
636         });
637 });
638
639 }
640
641 //}