c694cc12798bae52406dfa3ba758bd1d9ccbd3a3
[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( $.post(url(target), success), "post" );
215         ok( $.getScript(url("data/test.js"), success), "script" );
216         ok( $.getJSON(url("data/json_obj.js"), success), "json" );
217         */
218         ok( $.ajax({url: url(target), success: success}), "generic" );
219 });
220
221 test("global ajaxSettings", function() {
222         expect(3);
223
224         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
225     var orig = { url: "data/with_fries.xml", data: null };
226         var t;
227
228         $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
229
230     t = jQuery.extend({}, orig);
231     $.ajax(t);
232         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
233
234     t = jQuery.extend({}, orig);
235         t.data = {};
236     $.ajax(t);
237         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
238
239     t = jQuery.extend({}, orig);
240         t.data = { zoo: 'a', ping: 'b' };
241     $.ajax(t);
242         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' }" );
243         
244         jQuery.ajaxSettings = tmp;
245 });
246
247 test("load(String)", function() {
248         expect(1);
249         stop(true); // check if load can be called with only url
250         $('#first').load("data/name.html", start);
251 });
252
253 test("load('url selector')", function() {
254         expect(1);
255         stop(true); // check if load can be called with only url
256         $('#first').load("data/test3.html div.user", function(){
257                 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
258                 start();
259         });
260 });
261
262 test("load(String, Function) - simple: inject text into DOM", function() {
263         expect(2);
264         stop();
265         $('#first').load(url("data/name.html"), function() {
266                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
267                 start();
268         });
269 });
270
271 test("load(String, Function) - check scripts", function() {
272         expect(7);
273         stop();
274         window.testFoo = undefined;
275         window.foobar = null;
276         var verifyEvaluation = function() {
277           equals( foobar, "bar", 'Check if script src was evaluated after load' );
278           equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
279           start();
280         };
281         $('#first').load(url('data/test.html'), function() {
282           ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
283           equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
284           equals( testFoo, "foo", 'Check if script was evaluated after load' );
285           setTimeout(verifyEvaluation, 600);
286         });
287 });
288
289 test("load(String, Function) - check file with only a script tag", function() {
290         expect(3);
291         stop();
292         testFoo = undefined;
293         $('#first').load(url('data/test2.html'), function() {
294           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
295           ok( testFoo == "foo", 'Check if script was evaluated after load' );
296           start();
297         });
298 });
299
300 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
301         expect(2);
302         stop();
303         $.get(url('data/dashboard.xml'), function(xml) {
304                 var content = [];
305                 $('tab', xml).each(function() {
306                         content.push($(this).text());
307                 });
308                 equals( content[0], 'blabla', 'Check first tab');
309                 equals( content[1], 'blublu', 'Check second tab');
310                 start();
311         });
312 });
313
314 test("$.getScript(String, Function) - with callback", function() {
315         expect(2);
316         stop();
317         $.getScript(url("data/test.js"), function() {
318                 equals( foobar, "bar", 'Check if script was evaluated' );
319                 setTimeout(start, 100);
320         });
321 });
322
323 test("$.getScript(String, Function) - no callback", function() {
324         expect(1);
325         stop(true);
326         $.getScript(url("data/test.js"), start);
327 });
328
329 if ( !isLocal ) {
330
331 test("$.ajax() - JSONP, Local", function() {
332         expect(7);
333
334         var count = 0;
335         function plus(){ if ( ++count == 7 ) start(); }
336
337         stop();
338
339         $.ajax({
340                 url: "data/jsonp.php",
341                 dataType: "jsonp",
342                 success: function(data){
343                         ok( data.data, "JSON results returned (GET, no callback)" );
344                         plus();
345                 }
346         });
347
348         $.ajax({
349                 url: "data/jsonp.php?callback=?",
350                 dataType: "jsonp",
351                 success: function(data){
352                         ok( data.data, "JSON results returned (GET, url callback)" );
353                         plus();
354                 }
355         });
356
357         $.ajax({
358                 url: "data/jsonp.php",
359                 dataType: "jsonp",
360                 data: "callback=?",
361                 success: function(data){
362                         ok( data.data, "JSON results returned (GET, data 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 obj callback)" );
373                         plus();
374                 }
375         });
376
377         $.ajax({
378                 type: "POST",
379                 url: "data/jsonp.php",
380                 dataType: "jsonp",
381                 success: function(data){
382                         ok( data.data, "JSON results returned (POST, no callback)" );
383                         plus();
384                 }
385         });
386
387         $.ajax({
388                 type: "POST",
389                 url: "data/jsonp.php",
390                 data: "callback=?",
391                 dataType: "jsonp",
392                 success: function(data){
393                         ok( data.data, "JSON results returned (POST, data callback)" );
394                         plus();
395                 }
396         });
397
398         $.ajax({
399                 type: "POST",
400                 url: "data/jsonp.php",
401                 data: { callback: "?" },
402                 dataType: "jsonp",
403                 success: function(data){
404                         ok( data.data, "JSON results returned (POST, data obj callback)" );
405                         plus();
406                 }
407         });
408 });
409
410 test("$.ajax() - JSONP, Remote", function() {
411         expect(4);
412
413         var count = 0;
414         function plus(){ if ( ++count == 4 ) start(); }
415
416         var base = window.location.href.replace(/\?.*$/, "");
417
418         stop();
419
420         $.ajax({
421                 url: base + "data/jsonp.php",
422                 dataType: "jsonp",
423                 success: function(data){
424                         ok( data.data, "JSON results returned (GET, no callback)" );
425                         plus();
426                 }
427         });
428
429         $.ajax({
430                 url: base + "data/jsonp.php?callback=?",
431                 dataType: "jsonp",
432                 success: function(data){
433                         ok( data.data, "JSON results returned (GET, url callback)" );
434                         plus();
435                 }
436         });
437
438         $.ajax({
439                 url: base + "data/jsonp.php",
440                 dataType: "jsonp",
441                 data: "callback=?",
442                 success: function(data){
443                         ok( data.data, "JSON results returned (GET, data 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 obj callback)" );
454                         plus();
455                 }
456         });
457 });
458
459 test("$.ajax() - script, Remote", function() {
460         expect(2);
461
462         var base = window.location.href.replace(/\?.*$/, "");
463
464         stop();
465
466         $.ajax({
467                 url: base + "data/test.js",
468                 dataType: "script",
469                 success: function(data){
470                         ok( foobar, "Script results returned (GET, no callback)" );
471                         start();
472                 }
473         });
474 });
475
476 test("$.getJSON(String, Hash, Function) - JSON array", function() {
477         expect(4);
478         stop();
479         $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
480           ok( json[0].name == 'John', 'Check JSON: first, name' );
481           ok( json[0].age == 21, 'Check JSON: first, age' );
482           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
483           ok( json[1].age == 25, 'Check JSON: second, age' );
484           start();
485         });
486 });
487
488 test("$.getJSON(String, Function) - JSON object", function() {
489         expect(2);
490         stop();
491         $.getJSON(url("data/json.php"), function(json) {
492           ok( json.data.lang == 'en', 'Check JSON: lang' );
493           ok( json.data.length == 25, 'Check JSON: length' );
494           start();
495         });
496 });
497
498 test("$.post(String, Hash, Function) - simple with xml", function() {
499         expect(2);
500         stop();
501         $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
502           $('math', xml).each(function() {
503                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
504                     ok( $('result', this).text() == '3', 'Check for XML' );
505                  });
506           start();
507         });
508 });
509
510 test("$.ajaxSetup({timeout: Number}) - with global timeout", function() {
511         stop();
512         
513         var passed = 0;
514
515         $.ajaxSetup({timeout: 1000});
516         
517         var pass = function() {
518                 passed++;
519                 if ( passed == 2 ) {
520                         ok( true, 'Check local and global callbacks after timeout' );
521                 $('#main').unbind("ajaxError");
522                         start();
523                 }
524         };
525         
526         var fail = function(a,b,c) {
527                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
528                 start();
529         };
530         
531         $('#main').ajaxError(pass);
532         
533         $.ajax({
534           type: "GET",
535           url: url("data/name.php?wait=5"),
536           error: pass,
537           success: fail
538         });
539         
540         // reset timeout
541         $.ajaxSetup({timeout: 0});
542 });
543
544 test("$.ajaxSetup({timeout: Number}) with localtimeout", function() {
545         stop();
546         $.ajaxSetup({timeout: 50});
547
548         $.ajax({
549           type: "GET",
550           timeout: 5000,
551           url: url("data/name.php?wait=1"),
552           error: function() {
553                    ok( false, 'Check for local timeout failed' );
554                    start();
555           },
556           success: function() {
557             ok( true, 'Check for local timeout' );
558             start();
559           }
560         });
561
562         // reset timeout
563         $.ajaxSetup({timeout: 0});
564 });
565
566 test("$.ajax - simple get", function() {
567         expect(1);
568         stop();
569         $.ajax({
570           type: "GET",
571           url: url("data/name.php?name=foo"),
572           success: function(msg){
573             ok( msg == 'bar', 'Check for GET' );
574             start();
575           }
576         });
577 });
578
579 test("$.ajax - simple post", function() {
580         expect(1);
581         stop();
582         $.ajax({
583           type: "POST",
584           url: url("data/name.php"),
585           data: "name=peter",
586           success: function(msg){
587             ok( msg == 'pan', 'Check for POST' );
588             start();
589           }
590         });
591 });
592
593 test("ajaxSetup()", function() {
594         expect(1);
595         stop();
596         $.ajaxSetup({
597                 url: url("data/name.php?name=foo"),
598                 success: function(msg){
599                 ok( msg == 'bar', 'Check for GET' );
600                         start();
601                 }
602         });
603         $.ajax();
604 });
605
606 test("custom timeout does not set error message when timeout occurs, see #970", function() {
607         stop();
608         $.ajax({
609                 url: "data/name.php?wait=10",
610                 timeout: 500,
611                 error: function(request, status) {
612                         ok( status != null, "status shouldn't be null in error handler" );
613                         equals( "timeout", status );
614                         start();
615                 }
616         });
617 });
618
619 }
620
621 }