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