Fixed #1557, although it doesn't appear to be just an FF3 problem. In this case,...
[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(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 test("$.ajax() - JSONP, Local", function() {
350         expect(7);
351
352         var count = 0;
353         function plus(){ if ( ++count == 7 ) start(); }
354
355         stop();
356
357         $.ajax({
358                 url: "data/jsonp.php",
359                 dataType: "jsonp",
360                 success: function(data){
361                         ok( data.data, "JSON results returned (GET, no callback)" );
362                         plus();
363                 }
364         });
365
366         $.ajax({
367                 url: "data/jsonp.php?callback=?",
368                 dataType: "jsonp",
369                 success: function(data){
370                         ok( data.data, "JSON results returned (GET, url callback)" );
371                         plus();
372                 }
373         });
374
375         $.ajax({
376                 url: "data/jsonp.php",
377                 dataType: "jsonp",
378                 data: "callback=?",
379                 success: function(data){
380                         ok( data.data, "JSON results returned (GET, data callback)" );
381                         plus();
382                 }
383         });
384
385         $.ajax({
386                 url: "data/jsonp.php",
387                 dataType: "jsonp",
388                 data: { callback: "?" },
389                 success: function(data){
390                         ok( data.data, "JSON results returned (GET, data obj callback)" );
391                         plus();
392                 }
393         });
394
395         $.ajax({
396                 type: "POST",
397                 url: "data/jsonp.php",
398                 dataType: "jsonp",
399                 success: function(data){
400                         ok( data.data, "JSON results returned (POST, no callback)" );
401                         plus();
402                 }
403         });
404
405         $.ajax({
406                 type: "POST",
407                 url: "data/jsonp.php",
408                 data: "callback=?",
409                 dataType: "jsonp",
410                 success: function(data){
411                         ok( data.data, "JSON results returned (POST, data callback)" );
412                         plus();
413                 }
414         });
415
416         $.ajax({
417                 type: "POST",
418                 url: "data/jsonp.php",
419                 data: { callback: "?" },
420                 dataType: "jsonp",
421                 success: function(data){
422                         ok( data.data, "JSON results returned (POST, data obj callback)" );
423                         plus();
424                 }
425         });
426 });
427
428 test("$.ajax() - JSONP, Remote", function() {
429         expect(4);
430
431         var count = 0;
432         function plus(){ if ( ++count == 4 ) start(); }
433
434         var base = window.location.href.replace(/\?.*$/, "");
435
436         stop();
437
438         $.ajax({
439                 url: base + "data/jsonp.php",
440                 dataType: "jsonp",
441                 success: function(data){
442                         ok( data.data, "JSON results returned (GET, no callback)" );
443                         plus();
444                 }
445         });
446
447         $.ajax({
448                 url: base + "data/jsonp.php?callback=?",
449                 dataType: "jsonp",
450                 success: function(data){
451                         ok( data.data, "JSON results returned (GET, url callback)" );
452                         plus();
453                 }
454         });
455
456         $.ajax({
457                 url: base + "data/jsonp.php",
458                 dataType: "jsonp",
459                 data: "callback=?",
460                 success: function(data){
461                         ok( data.data, "JSON results returned (GET, data callback)" );
462                         plus();
463                 }
464         });
465
466         $.ajax({
467                 url: base + "data/jsonp.php",
468                 dataType: "jsonp",
469                 data: { callback: "?" },
470                 success: function(data){
471                         ok( data.data, "JSON results returned (GET, data obj callback)" );
472                         plus();
473                 }
474         });
475 });
476
477 test("$.ajax() - script, Remote", function() {
478         expect(2);
479
480         var base = window.location.href.replace(/\?.*$/, "");
481
482         stop();
483
484         $.ajax({
485                 url: base + "data/test.js",
486                 dataType: "script",
487                 success: function(data){
488                         ok( foobar, "Script results returned (GET, no callback)" );
489                         start();
490                 }
491         });
492 });
493
494 test("$.getJSON(String, Hash, Function) - JSON array", function() {
495         expect(4);
496         stop();
497         $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
498           ok( json[0].name == 'John', 'Check JSON: first, name' );
499           ok( json[0].age == 21, 'Check JSON: first, age' );
500           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
501           ok( json[1].age == 25, 'Check JSON: second, age' );
502           start();
503         });
504 });
505
506 test("$.getJSON(String, Function) - JSON object", function() {
507         expect(2);
508         stop();
509         $.getJSON(url("data/json.php"), function(json) {
510           ok( json.data.lang == 'en', 'Check JSON: lang' );
511           ok( json.data.length == 25, 'Check JSON: length' );
512           start();
513         });
514 });
515
516 test("$.getJSON(String, Function) - Remote JSON object with assignment", function() {
517         expect(2);
518
519         var base = window.location.href.replace(/\?.*$/, "");
520
521         stop();
522         $.getJSON(base + "data/json_assigned_obj.js", function() {
523           ok( typeof json_assigned_obj == "object", 'Check JSON loaded' );
524           equals( json_assigned_obj.test, "worked", 'Check JSON obj.test' );
525           start();
526         });
527 });
528
529 test("$.post(String, Hash, Function) - simple with xml", function() {
530         expect(4);
531         stop();
532         $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
533           $('math', xml).each(function() {
534                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
535                     ok( $('result', this).text() == '3', 'Check for XML' );
536                  });
537         });
538
539         $.post(url("data/name.php?xml=5-2"), {}, function(xml){
540           $('math', xml).each(function() {
541                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
542                     ok( $('result', this).text() == '3', 'Check for XML' );
543                  });
544           start();
545         });
546 });
547
548 test("$.ajaxSetup({timeout: Number}) - with global timeout", function() {
549         stop();
550         
551         var passed = 0;
552
553         $.ajaxSetup({timeout: 1000});
554         
555         var pass = function() {
556                 passed++;
557                 if ( passed == 2 ) {
558                         ok( true, 'Check local and global callbacks after timeout' );
559                 $('#main').unbind("ajaxError");
560                         start();
561                 }
562         };
563         
564         var fail = function(a,b,c) {
565                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
566                 start();
567         };
568         
569         $('#main').ajaxError(pass);
570         
571         $.ajax({
572           type: "GET",
573           url: url("data/name.php?wait=5"),
574           error: pass,
575           success: fail
576         });
577         
578         // reset timeout
579         $.ajaxSetup({timeout: 0});
580 });
581
582 test("$.ajaxSetup({timeout: Number}) with localtimeout", function() {
583         stop();
584         $.ajaxSetup({timeout: 50});
585
586         $.ajax({
587           type: "GET",
588           timeout: 5000,
589           url: url("data/name.php?wait=1"),
590           error: function() {
591                    ok( false, 'Check for local timeout failed' );
592                    start();
593           },
594           success: function() {
595             ok( true, 'Check for local timeout' );
596             start();
597           }
598         });
599
600         // reset timeout
601         $.ajaxSetup({timeout: 0});
602 });
603
604 test("$.ajax - simple get", function() {
605         expect(1);
606         stop();
607         $.ajax({
608           type: "GET",
609           url: url("data/name.php?name=foo"),
610           success: function(msg){
611             ok( msg == 'bar', 'Check for GET' );
612             start();
613           }
614         });
615 });
616
617 test("$.ajax - simple post", function() {
618         expect(1);
619         stop();
620         $.ajax({
621           type: "POST",
622           url: url("data/name.php"),
623           data: "name=peter",
624           success: function(msg){
625             ok( msg == 'pan', 'Check for POST' );
626             start();
627           }
628         });
629 });
630
631 test("ajaxSetup()", function() {
632         expect(1);
633         stop();
634         $.ajaxSetup({
635                 url: url("data/name.php?name=foo"),
636                 success: function(msg){
637                 ok( msg == 'bar', 'Check for GET' );
638                         start();
639                 }
640         });
641         $.ajax();
642 });
643
644 test("custom timeout does not set error message when timeout occurs, see #970", function() {
645         stop();
646         $.ajax({
647                 url: "data/name.php?wait=10",
648                 timeout: 500,
649                 error: function(request, status) {
650                         ok( status != null, "status shouldn't be null in error handler" );
651                         equals( "timeout", status );
652                         start();
653                 }
654         });
655 });
656
657 }
658
659 //}