0fd4888ac9d74a63a72b3f5b316f4f5e99e45455
[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("serialize()", function() {
9         expect(1);
10         // ignore button, IE takes text content as value, not relevant for this test
11         equals( $(':input').not('button').serialize(), 
12                 '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=', 
13                 'Check form serialization as query string');
14 });
15
16 test("$.param()", function() {
17         expect(4);
18         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
19         equals( $.param(params), "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
20         
21         params = {someName: [1, 2, 3], regularThing: "blah" };
22         equals( $.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
23         
24         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
25         equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All%20your%20base%20are%20belong%20to%20us", "more array" );
26         
27         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
28         equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
29 });
30
31 test("synchronous request", function() {
32         expect(1);
33         ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
34 });
35
36 test("synchronous request with callbacks", function() {
37         expect(2);
38         var result;
39         $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
40         ok( /^{ "data"/.test( result ), "check returned text" );
41 });
42
43 test("pass-through request object", function() {
44         expect(1);
45         stop(true);
46         
47         var target = "data/name.html";
48         var count = 0;
49         var success = function() {
50                 // Disabled
51                 //if(count++ == 5)
52                 start();
53         };
54         
55         /* Test disabled, too many simultaneous requests
56         ok( $.get(url(target), success), "get" );
57         ok( $.getIfModified(url(target), success), "getIfModified" );
58         ok( $.post(url(target), success), "post" );
59         ok( $.getScript(url("data/test.js"), success), "script" );
60         ok( $.getJSON(url("data/json_obj.js"), success), "json" );
61         */
62         ok( $.ajax({url: url(target), success: success}), "generic" );
63 });
64
65 test("global ajaxSettings", function() {
66         expect(3);
67
68         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
69         var orig = { url: "data/with_fries.xml", data: null };
70         var t;
71
72         $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
73
74         t = jQuery.extend({}, orig);
75         $.ajax(t);
76         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
77
78         t = jQuery.extend({}, orig);
79         t.data = {};
80         $.ajax(t);
81         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
82
83         t = jQuery.extend({}, orig);
84         t.data = { zoo: 'a', ping: 'b' };
85         $.ajax(t);
86         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' }" );
87         
88         jQuery.ajaxSettings = tmp;
89 });
90
91 test("load(String)", function() {
92         expect(1);
93         stop(true); // check if load can be called with only url
94         $('#first').load("data/name.html", start);
95 });
96
97 test("load('url selector')", function() {
98         expect(1);
99         stop(true); // check if load can be called with only url
100         $('#first').load("data/test3.html div.user", function(){
101                 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
102                 start();
103         });
104 });
105
106 test("load(String, Function) - simple: inject text into DOM", function() {
107         expect(2);
108         stop();
109         $('#first').load(url("data/name.html"), function() {
110                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
111                 start();
112         });
113 });
114
115 test("load(String, Function) - check scripts", function() {
116         expect(7);
117         stop();
118         window.testFoo = undefined;
119         window.foobar = null;
120         var verifyEvaluation = function() {
121           equals( foobar, "bar", 'Check if script src was evaluated after load' );
122           equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
123           start();
124         };
125         $('#first').load(url('data/test.html'), function() {
126           ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
127           equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
128           equals( testFoo, "foo", 'Check if script was evaluated after load' );
129           setTimeout(verifyEvaluation, 600);
130         });
131 });
132
133 test("load(String, Function) - check file with only a script tag", function() {
134         expect(3);
135         stop();
136         testFoo = undefined;
137         $('#first').load(url('data/test2.html'), function() {
138           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
139           ok( testFoo == "foo", 'Check if script was evaluated after load' );
140           start();
141         });
142 });
143
144 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
145         expect(2);
146         stop();
147         $.get(url('data/dashboard.xml'), function(xml) {
148                 var content = [];
149                 $('tab', xml).each(function() {
150                         content.push($(this).text());
151                 });
152                 equals( content[0], 'blabla', 'Check first tab');
153                 equals( content[1], 'blublu', 'Check second tab');
154                 start();
155         });
156 });
157
158 test("$.getIfModified(String, Hash, Function)", function() {
159         expect(1);
160         stop();
161         $.getIfModified(url("data/name.html"), function(msg) {
162             ok( /^ERROR/.test(msg), 'Check ifModified' );
163             start();
164         });
165 });
166
167 test("$.getScript(String, Function) - with callback", function() {
168         expect(2);
169         stop();
170         $.getScript(url("data/test.js"), function() {
171                 equals( foobar, "bar", 'Check if script was evaluated' );
172                 setTimeout(start, 100);
173         });
174 });
175
176 test("$.getScript(String, Function) - no callback", function() {
177         expect(1);
178         stop(true);
179         $.getScript(url("data/test.js"), start);
180 });
181
182 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
183         expect(3);
184         stop();
185         $.ajax({
186           url: url("data/with_fries.xml"),
187           dataType: "xml",
188           success: function(resp) {
189             equals( $("properties", resp).length, 1, 'properties in responseXML' );
190             equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
191             equals( $("thing", resp).length, 2, 'things in responseXML' );
192             start();
193           }
194         });
195 });
196
197 test("test global handlers - success", function() {
198         expect( isLocal ? 4 : 8 );
199         stop();
200         
201         var counter = { complete: 0, success: 0, error: 0, send: 0 },
202                 success = function() { counter.success++ },
203                 error = function() { counter.error++ },
204                 complete = function() { counter.complete++ },
205                 send = function() { counter.send++ };
206
207         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
208         
209         // start with successful test
210         $.ajax({url: url("data/name.html"), beforeSend: send, success: success, error: error, complete: function() {
211           equals( counter.error, 0, 'Check succesful request, error callback' );
212           equals( counter.success, 2, 'Check succesful request, success callback' );
213           equals( counter.complete, 3, 'Check succesful request, complete callback' );
214           equals( counter.send, 2, 'Check succesful request, send callback' );
215           
216           if ( !isLocal ) {
217                   counter.error = counter.success = counter.complete = counter.send = 0;
218                   $.ajaxTimeout(500);
219                   
220                   $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() {
221                         equals( counter.error, 2, 'Check failed request, error callback' );
222                         equals( counter.success, 0, 'Check failed request, success callback' );
223                         equals( counter.complete, 3, 'Check failed request, failed callback' );
224                         equals( counter.send, 2, 'Check failed request, send callback' );
225                         start();
226                   }});
227           } else
228                   start();
229         }});
230 });
231
232 test("test global handlers - failure", function() {
233         expect( isLocal ? 4 : 8 );
234         stop();
235         
236         var counter = { complete: 0, success: 0, error: 0, send: 0 },
237                 success = function() { counter.success++ },
238                 error = function() { counter.error++ },
239                 complete = function() { counter.complete++ },
240                 send = function() { counter.send++ };
241                 
242         $.ajaxTimeout(0);
243         
244         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
245         
246         $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() {
247           ok( counter.error == 0, 'Check sucesful request without globals' );
248           ok( counter.success == 1, 'Check sucesful request without globals' );
249           ok( counter.complete == 0, 'Check sucesful request without globals' );
250           ok( counter.send == 1, 'Check sucesful request without globals' );
251           
252           if ( !isLocal ) {
253                   counter.error = counter.success = counter.complete = counter.send = 0;
254                   $.ajaxTimeout(500);
255                   
256                   $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
257                          var x = counter;
258                          ok( counter.error == 1, 'Check failed request without globals' );
259                          ok( counter.success == 0, 'Check failed request without globals' );
260                          ok( counter.complete == 0, 'Check failed request without globals' );
261                          ok( counter.send == 1, 'Check failed request without globals' );
262                          start();
263                   }});
264           } else
265                   start();
266         }});
267 });
268
269 test("$.ajax - beforeSend", function() {
270         expect(1);
271         stop();
272         
273         var check = false;
274         
275         $.ajaxSetup({ timeout: 0 });
276         
277         $.ajax({
278                 url: url("data/name.html"), 
279                 beforeSend: function(xml) {
280                         check = true;
281                 },
282                 success: function(data) {
283                         ok( check, "check beforeSend was executed" );
284                         start();
285                 }
286         });
287 });
288
289 test("$.ajax - dataType html", function() {
290         expect(5);
291         stop();
292         
293         foobar = null;
294         testFoo = undefined;
295         
296         var verifyEvaluation = function() {
297           ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
298           ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
299           start();
300         };
301         
302         $.ajax({
303           dataType: "html",
304           url: url("data/test.html"),
305           success: function(data) {
306                 $("#ap").html(data);
307             ok( data.match(/^html text/), 'Check content for datatype html' );
308             setTimeout(verifyEvaluation, 600);
309           }
310         });
311 });
312
313 if ( !isLocal ) {
314
315 test("$.getJSON(String, Hash, Function) - JSON array", function() {
316         expect(4);
317         stop();
318         $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
319           ok( json[0].name == 'John', 'Check JSON: first, name' );
320           ok( json[0].age == 21, 'Check JSON: first, age' );
321           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
322           ok( json[1].age == 25, 'Check JSON: second, age' );
323           start();
324         });
325 });
326
327 test("$.getJSON(String, Function) - JSON object", function() {
328         expect(2);
329         stop();
330         $.getJSON(url("data/json.php"), function(json) {
331           ok( json.data.lang == 'en', 'Check JSON: lang' );
332           ok( json.data.length == 25, 'Check JSON: length' );
333           start();
334         });
335 });
336
337 test("$.post(String, Hash, Function) - simple with xml", function() {
338         expect(2);
339         stop();
340         $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
341           $('math', xml).each(function() {
342                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
343                     ok( $('result', this).text() == '3', 'Check for XML' );
344                  });
345           start();
346         });
347 });
348
349 test("$.ajaxTimeout(Number) - with global timeout", function() {
350         stop();
351         
352         var passed = 0;
353
354         $.ajaxTimeout(1000);
355         
356         var pass = function() {
357                 passed++;
358                 if ( passed == 2 ) {
359                         ok( true, 'Check local and global callbacks after timeout' );
360                 $('#main').unbind("ajaxError");
361                         start();
362                 }
363         };
364         
365         var fail = function(a,b,c) {
366                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
367                 start();
368         };
369         
370         $('#main').ajaxError(pass);
371         
372         $.ajax({
373           type: "GET",
374           url: url("data/name.php?wait=5"),
375           error: pass,
376           success: fail
377         });
378         
379         // reset timeout
380         $.ajaxTimeout(0);
381 });
382
383 test("$.ajaxTimeout(Number) with localtimeout", function() {
384         stop(); $.ajaxTimeout(50);
385         $.ajax({
386           type: "GET",
387           timeout: 5000,
388           url: url("data/name.php?wait=1"),
389           error: function() {
390                    ok( false, 'Check for local timeout failed' );
391                    start();
392           },
393           success: function() {
394             ok( true, 'Check for local timeout' );
395             start();
396           }
397         });
398         // reset timeout
399         $.ajaxTimeout(0);
400 });
401
402 test("$.ajax - simple get", function() {
403         expect(1);
404         stop();
405         $.ajax({
406           type: "GET",
407           url: url("data/name.php?name=foo"),
408           success: function(msg){
409             ok( msg == 'bar', 'Check for GET' );
410             start();
411           }
412         });
413 });
414
415 test("$.ajax - simple post", function() {
416         expect(1);
417         stop();
418         $.ajax({
419           type: "POST",
420           url: url("data/name.php"),
421           data: "name=peter",
422           success: function(msg){
423             ok( msg == 'pan', 'Check for POST' );
424             start();
425           }
426         });
427 });
428
429 test("ajaxSetup()", function() {
430         expect(1);
431         stop();
432         $.ajaxSetup({
433                 url: url("data/name.php?name=foo"),
434                 success: function(msg){
435                 ok( msg == 'bar', 'Check for GET' );
436                         start();
437                 }
438         });
439         $.ajax();
440 });
441
442 test("custom timeout does not set error message when timeout occurs, see #970", function() {
443         stop();
444         $.ajax({
445                 url: "data/name.php?wait=10",
446                 timeout: 500,
447                 error: function(request, status) {
448                         ok( status != null, "status shouldn't be null in error handler" );
449                         equals( "timeout", status );
450                         start();
451                 }
452         });
453 });
454
455 }
456
457 }