Added support for:
[jquery.git] / src / ajax / 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(1);
167         // ignore button, IE takes text content as value, not relevant for this test
168         equals( $(':input').not('button').serialize(), 
169                 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo%5Bbar%5D=&name=name&=foobar&select1=&select2=3&select3=1&test=&id=', 
170                 'Check form serialization as query string');
171 });
172
173 test("$.param()", function() {
174         expect(4);
175         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
176         equals( $.param(params), "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
177         
178         params = {someName: [1, 2, 3], regularThing: "blah" };
179         equals( $.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
180         
181         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
182         equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All%20your%20base%20are%20belong%20to%20us", "more array" );
183         
184         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
185         equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
186 });
187
188 test("synchronous request", function() {
189         expect(1);
190         ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
191 });
192
193 test("synchronous request with callbacks", function() {
194         expect(2);
195         var result;
196         $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
197         ok( /^{ "data"/.test( result ), "check returned text" );
198 });
199
200 test("pass-through request object", function() {
201         expect(1);
202         stop(true);
203         
204         var target = "data/name.html";
205         var count = 0;
206         var success = function() {
207                 // Disabled
208                 //if(count++ == 5)
209                 start();
210         };
211         
212         /* Test disabled, too many simultaneous requests
213         ok( $.get(url(target), success), "get" );
214         ok( $.getIfModified(url(target), success), "getIfModified" );
215         ok( $.post(url(target), success), "post" );
216         ok( $.getScript(url("data/test.js"), success), "script" );
217         ok( $.getJSON(url("data/json_obj.js"), success), "json" );
218         */
219         ok( $.ajax({url: url(target), success: success}), "generic" );
220 });
221
222 test("global ajaxSettings", function() {
223         expect(3);
224
225         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
226     var orig = { url: "data/with_fries.xml", data: null };
227         var t;
228
229         $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
230
231     t = jQuery.extend({}, orig);
232     $.ajax(t);
233         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
234
235     t = jQuery.extend({}, orig);
236         t.data = {};
237     $.ajax(t);
238         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
239
240     t = jQuery.extend({}, orig);
241         t.data = { zoo: 'a', ping: 'b' };
242     $.ajax(t);
243         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' }" );
244         
245         jQuery.ajaxSettings = tmp;
246 });
247
248 test("load(String)", function() {
249         expect(1);
250         stop(true); // check if load can be called with only url
251         $('#first').load("data/name.html", start);
252 });
253
254 test("load('url selector')", function() {
255         expect(1);
256         stop(true); // check if load can be called with only url
257         $('#first').load("data/test3.html div.user", function(){
258                 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
259                 start();
260         });
261 });
262
263 test("load(String, Function) - simple: inject text into DOM", function() {
264         expect(2);
265         stop();
266         $('#first').load(url("data/name.html"), function() {
267                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
268                 start();
269         });
270 });
271
272 test("load(String, Function) - check scripts", function() {
273         expect(7);
274         stop();
275         window.testFoo = undefined;
276         window.foobar = null;
277         var verifyEvaluation = function() {
278           equals( foobar, "bar", 'Check if script src was evaluated after load' );
279           equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
280           start();
281         };
282         $('#first').load(url('data/test.html'), function() {
283           ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
284           equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
285           equals( testFoo, "foo", 'Check if script was evaluated after load' );
286           setTimeout(verifyEvaluation, 600);
287         });
288 });
289
290 test("load(String, Function) - check file with only a script tag", function() {
291         expect(3);
292         stop();
293         testFoo = undefined;
294         $('#first').load(url('data/test2.html'), function() {
295           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
296           ok( testFoo == "foo", 'Check if script was evaluated after load' );
297           start();
298         });
299 });
300
301 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
302         expect(2);
303         stop();
304         $.get(url('data/dashboard.xml'), function(xml) {
305                 var content = [];
306                 $('tab', xml).each(function() {
307                         content.push($(this).text());
308                 });
309                 equals( content[0], 'blabla', 'Check first tab');
310                 equals( content[1], 'blublu', 'Check second tab');
311                 start();
312         });
313 });
314
315 test("$.getIfModified(String, Hash, Function)", function() {
316         expect(1);
317         stop();
318         $.getIfModified(url("data/name.html"), function(msg) {
319             ok( /^ERROR/.test(msg), 'Check ifModified' );
320             start();
321         });
322 });
323
324 test("$.getScript(String, Function) - with callback", function() {
325         expect(2);
326         stop();
327         $.getScript(url("data/test.js"), function() {
328                 equals( foobar, "bar", 'Check if script was evaluated' );
329                 setTimeout(start, 100);
330         });
331 });
332
333 test("$.getScript(String, Function) - no callback", function() {
334         expect(1);
335         stop(true);
336         $.getScript(url("data/test.js"), start);
337 });
338
339 if ( !isLocal ) {
340
341 test("$.ajax() - JSONP, Local", function() {
342         expect(7);
343
344         var count = 0;
345         function plus(){ if ( ++count == 7 ) start(); }
346
347         stop();
348
349         $.ajax({
350                 url: "data/jsonp.php",
351                 dataType: "jsonp",
352                 success: function(data){
353                         ok( data.data, "JSON results returned (GET, no callback)" );
354                         plus();
355                 }
356         });
357
358         $.ajax({
359                 url: "data/jsonp.php?callback=?",
360                 dataType: "jsonp",
361                 success: function(data){
362                         ok( data.data, "JSON results returned (GET, url callback)" );
363                         plus();
364                 }
365         });
366
367         $.ajax({
368                 url: "data/jsonp.php",
369                 dataType: "jsonp",
370                 data: "callback=?",
371                 success: function(data){
372                         ok( data.data, "JSON results returned (GET, data 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 obj callback)" );
383                         plus();
384                 }
385         });
386
387         $.ajax({
388                 type: "POST",
389                 url: "data/jsonp.php",
390                 dataType: "jsonp",
391                 success: function(data){
392                         ok( data.data, "JSON results returned (POST, no callback)" );
393                         plus();
394                 }
395         });
396
397         $.ajax({
398                 type: "POST",
399                 url: "data/jsonp.php",
400                 data: "callback=?",
401                 dataType: "jsonp",
402                 success: function(data){
403                         ok( data.data, "JSON results returned (POST, data callback)" );
404                         plus();
405                 }
406         });
407
408         $.ajax({
409                 type: "POST",
410                 url: "data/jsonp.php",
411                 data: { callback: "?" },
412                 dataType: "jsonp",
413                 success: function(data){
414                         ok( data.data, "JSON results returned (POST, data obj callback)" );
415                         plus();
416                 }
417         });
418 });
419
420 test("$.ajax() - JSONP, Remote", function() {
421         expect(4);
422
423         var count = 0;
424         function plus(){ if ( ++count == 4 ) start(); }
425
426         var base = window.location.href.replace(/\?.*$/, "");
427
428         stop();
429
430         $.ajax({
431                 url: base + "data/jsonp.php",
432                 dataType: "jsonp",
433                 success: function(data){
434                         ok( data.data, "JSON results returned (GET, no callback)" );
435                         plus();
436                 }
437         });
438
439         $.ajax({
440                 url: base + "data/jsonp.php?callback=?",
441                 dataType: "jsonp",
442                 success: function(data){
443                         ok( data.data, "JSON results returned (GET, url callback)" );
444                         plus();
445                 }
446         });
447
448         $.ajax({
449                 url: base + "data/jsonp.php",
450                 dataType: "jsonp",
451                 data: "callback=?",
452                 success: function(data){
453                         ok( data.data, "JSON results returned (GET, data 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 obj callback)" );
464                         plus();
465                 }
466         });
467 });
468
469 test("$.ajax() - script, Remote", function() {
470         expect(2);
471
472         var base = window.location.href.replace(/\?.*$/, "");
473
474         stop();
475
476         $.ajax({
477                 url: base + "data/test.js",
478                 dataType: "script",
479                 success: function(data){
480                         ok( foobar, "Script results returned (GET, no callback)" );
481                         start();
482                 }
483         });
484 });
485
486 test("$.getJSON(String, Hash, Function) - JSON array", function() {
487         expect(4);
488         stop();
489         $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
490           ok( json[0].name == 'John', 'Check JSON: first, name' );
491           ok( json[0].age == 21, 'Check JSON: first, age' );
492           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
493           ok( json[1].age == 25, 'Check JSON: second, age' );
494           start();
495         });
496 });
497
498 test("$.getJSON(String, Function) - JSON object", function() {
499         expect(2);
500         stop();
501         $.getJSON(url("data/json.php"), function(json) {
502           ok( json.data.lang == 'en', 'Check JSON: lang' );
503           ok( json.data.length == 25, 'Check JSON: length' );
504           start();
505         });
506 });
507
508 test("$.post(String, Hash, Function) - simple with xml", function() {
509         expect(2);
510         stop();
511         $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
512           $('math', xml).each(function() {
513                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
514                     ok( $('result', this).text() == '3', 'Check for XML' );
515                  });
516           start();
517         });
518 });
519
520 test("$.ajaxTimeout(Number) - with global timeout", function() {
521         stop();
522         
523         var passed = 0;
524
525         $.ajaxTimeout(1000);
526         
527         var pass = function() {
528                 passed++;
529                 if ( passed == 2 ) {
530                         ok( true, 'Check local and global callbacks after timeout' );
531                 $('#main').unbind("ajaxError");
532                         start();
533                 }
534         };
535         
536         var fail = function(a,b,c) {
537                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
538                 start();
539         };
540         
541         $('#main').ajaxError(pass);
542         
543         $.ajax({
544           type: "GET",
545           url: url("data/name.php?wait=5"),
546           error: pass,
547           success: fail
548         });
549         
550         // reset timeout
551         $.ajaxTimeout(0);
552 });
553
554 test("$.ajaxTimeout(Number) with localtimeout", function() {
555         stop(); $.ajaxTimeout(50);
556         $.ajax({
557           type: "GET",
558           timeout: 5000,
559           url: url("data/name.php?wait=1"),
560           error: function() {
561                    ok( false, 'Check for local timeout failed' );
562                    start();
563           },
564           success: function() {
565             ok( true, 'Check for local timeout' );
566             start();
567           }
568         });
569         // reset timeout
570         $.ajaxTimeout(0);
571 });
572
573 test("$.ajax - simple get", function() {
574         expect(1);
575         stop();
576         $.ajax({
577           type: "GET",
578           url: url("data/name.php?name=foo"),
579           success: function(msg){
580             ok( msg == 'bar', 'Check for GET' );
581             start();
582           }
583         });
584 });
585
586 test("$.ajax - simple post", function() {
587         expect(1);
588         stop();
589         $.ajax({
590           type: "POST",
591           url: url("data/name.php"),
592           data: "name=peter",
593           success: function(msg){
594             ok( msg == 'pan', 'Check for POST' );
595             start();
596           }
597         });
598 });
599
600 test("ajaxSetup()", function() {
601         expect(1);
602         stop();
603         $.ajaxSetup({
604                 url: url("data/name.php?name=foo"),
605                 success: function(msg){
606                 ok( msg == 'bar', 'Check for GET' );
607                         start();
608                 }
609         });
610         $.ajax();
611 });
612
613 test("custom timeout does not set error message when timeout occurs, see #970", function() {
614         stop();
615         $.ajax({
616                 url: "data/name.php?wait=10",
617                 timeout: 500,
618                 error: function(request, status) {
619                         ok( status != null, "status shouldn't be null in error handler" );
620                         equals( "timeout", status );
621                         start();
622                 }
623         });
624 });
625
626 }
627
628 //}