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