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