]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Request.html
initial commit
[syp.git] / openlayers / tests / Request.html
1 <html>
2 <head>
3     <script src="../lib/OpenLayers.js"></script>
4     <script type="text/javascript">
5     function setup() {
6         window._xhr = OpenLayers.Request.XMLHttpRequest;
7         var anon = new Function();
8         OpenLayers.Request.XMLHttpRequest = function() {};
9         OpenLayers.Request.XMLHttpRequest.prototype = {
10             open: anon,
11             setRequestHeader: anon,
12             send: anon
13         };
14         OpenLayers.Request.XMLHttpRequest.DONE = 4;
15     }
16     function teardown() {
17         OpenLayers.Request.XMLHttpRequest = window._xhr;
18     }
19     
20     function test_issue(t) {
21         setup();
22
23         t.plan(21);
24         var request, config;
25         var proto = OpenLayers.Request.XMLHttpRequest.prototype;
26         var issue = OpenLayers.Function.bind(OpenLayers.Request.issue,
27                                              OpenLayers.Request);
28
29         // test that issue returns a new XMLHttpRequest - 1 test
30         request = issue();
31         t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
32              "returns an XMLHttpRequest instance");
33         
34         // test that issue calls xhr.open with correct args from config - 5 tests
35         var _open = proto.open;
36         config = {
37             method: "foo",
38             url: "http://nowhere",
39             async: "bar",
40             user: "uncle",
41             password: "sam"
42         };
43         proto.open = function(method, url, async, user, password) {
44             t.eq(method, config.method, "open called with correct method");
45             t.eq(url, config.url, "open called with correct url");
46             t.eq(async, config.async, "open called with correct async");
47             t.eq(user, config.user, "open called with correct user");
48             t.eq(password, config.password, "open called with correct password");
49         };
50         request = issue(config);
51         
52         // test that params are serialized as query string - 1 test
53         config = {
54             method: "GET",
55             url: "http://example.com/",
56             params: {"foo": "bar"}
57         };
58         proto.open = function(method, url, async, user, password) {
59             t.eq(url, config.url + "?foo=bar", "params serialized as query string");
60         };
61         request = issue(config);
62         
63         // test that empty params object doesn't produce query string - 1 test
64         config = {
65             method: "GET",
66             url: "http://example.com/",
67             params: {}
68         };
69         proto.open = function(method, url, async, user, password) {
70             t.eq(url, config.url, "empty params doesn't produce query string");
71         }
72         request = issue(config);
73         
74         // test that query string doesn't get two ? separators
75         config = {
76             method: "GET",
77             url: "http://example.com/?existing=query",
78             params: {"foo": "bar"}
79         };
80         proto.open = function(method, url, async, user, password) {
81             t.eq(url, config.url + "&foo=bar", "existing query string gets extended with &");
82         }
83         request = issue(config);
84
85         // reset open method
86         proto.open = _open;
87         
88         // test that headers are correctly set - 4 tests
89         var _setRequestHeader = proto.setRequestHeader;
90         config = {
91             headers: {
92                 foo: "bar",
93                 chicken: "soup"
94             }
95         };
96         proto.setRequestHeader = function(key, value) {
97             t.ok(key in config.headers, "setRequestHeader called with key: " + key);
98             t.eq(value, config.headers[key], "setRequestHeader called with correct value: " + value);
99         }
100         request = issue(config);
101         proto.setRequestHeader = _setRequestHeader;
102         
103         // test that callback is called (no scope) - 1 test
104         var unbound = function(request) {
105             t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
106                  "unbound callback called with xhr instance");
107         }
108         config = {
109             callback: unbound
110         };
111         request = issue(config);
112         request.readyState = OpenLayers.Request.XMLHttpRequest.DONE;
113         request.onreadystatechange();
114
115         // test that callback is called (with scope) - 2 tests
116         var obj = {};
117         var bound = function(request) {
118             t.ok(this === obj, "bound callback has correct scope");
119             t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
120                  "bound callback called with xhr instance");
121         }
122         config = {
123             callback: bound,
124             scope: obj
125         };
126         request = issue(config);
127         request.readyState = 4;
128         request.onreadystatechange();
129         
130         // test that optional success callback is only called with 200s and
131         // failure is only called with non-200s
132         var _send = proto.send;
133         proto.send = function() {};
134         
135         config = {
136             success: function(req) {
137                 t.ok(!req.status || (req.status >= 200 && req.status < 300),
138                      "success callback called with " + req.status + " status");
139             },
140             failure: function(req) {
141                 t.ok(req.status && (req.status < 200 || req.status >= 300),
142                      "failure callback called with " + req.status + " status");
143             }
144         };
145         request = issue(config);
146         request.readyState = 4;
147         
148         // mock up status 200 (1 test)
149         request.status = 200;
150         request.onreadystatechange();
151         
152         // mock up status 299 (1 test)
153         request.status = 299;
154         request.onreadystatechange();
155         
156         // mock up status 100 (1 test)
157         request.status = 100;
158         request.onreadystatechange();
159
160         // mock up status 300 (1 test)
161         request.status = 300;
162         request.onreadystatechange();
163         
164         // mock up a status null (1 test)
165         request.status = null;
166         request.onreadystatechange();
167
168         proto.send = _send;
169
170         teardown();
171     }
172     
173     function test_delayed_send(t) {
174         t.plan(1);
175         var proto = OpenLayers.Request.XMLHttpRequest.prototype;
176         var _send = proto.send;
177         
178         // test that send is called with data - 1 test
179         var config = {
180             method: "PUT",
181             data: "bling"
182         };
183         var got = {};
184         proto.send = function(data) {
185             got.data = data;
186         }
187         OpenLayers.Request.issue(config);
188         
189         t.delay_call(1, function() {
190             t.eq(got.data, config.data, "proper data sent");
191             proto.send = _send;
192         });        
193         
194     }
195     
196     function test_GET(t) {
197         t.plan(1);
198         var _issue = OpenLayers.Request.issue;
199         OpenLayers.Request.issue = function(config) {
200             t.eq(config.method, "GET", "calls issue with correct method");
201         }
202         OpenLayers.Request.GET();
203         OpenLayers.Request.issue = _issue;
204     }
205     function test_POST(t) {
206         t.plan(1);
207         var _issue = OpenLayers.Request.issue;
208         OpenLayers.Request.issue = function(config) {
209             t.eq(config.method, "POST", "calls issue with correct method");
210         }
211         OpenLayers.Request.POST();
212         OpenLayers.Request.issue = _issue;
213     }
214     function test_PUT(t) {
215         t.plan(1);
216         var _issue = OpenLayers.Request.issue;
217         OpenLayers.Request.issue = function(config) {
218             t.eq(config.method, "PUT", "calls issue with correct method");
219         }
220         OpenLayers.Request.PUT();
221         OpenLayers.Request.issue = _issue;
222     }
223     function test_DELETE(t) {
224         t.plan(1);
225         var _issue = OpenLayers.Request.issue;
226         OpenLayers.Request.issue = function(config) {
227             t.eq(config.method, "DELETE", "calls issue with correct method");
228         }
229         OpenLayers.Request.DELETE();
230         OpenLayers.Request.issue = _issue;
231     }
232     function test_HEAD(t) {
233         t.plan(1);
234         var _issue = OpenLayers.Request.issue;
235         OpenLayers.Request.issue = function(config) {
236             t.eq(config.method, "HEAD", "calls issue with correct method");
237         }
238         OpenLayers.Request.HEAD();
239         OpenLayers.Request.issue = _issue;
240     }
241     function test_OPTIONS(t) {
242         t.plan(1);
243         var _issue = OpenLayers.Request.issue;
244         OpenLayers.Request.issue = function(config) {
245             t.eq(config.method, "OPTIONS", "calls issue with correct method");
246         }
247         OpenLayers.Request.OPTIONS();
248         OpenLayers.Request.issue = _issue;
249     }
250     
251     function test_events_success(t) {
252         
253         t.plan(5);
254
255         var events = [];
256         function listener(event) {
257             events.push(event);
258         }
259         
260         // set up event listeners
261         OpenLayers.Request.events.on({
262             complete: listener,
263             success: listener,
264             failure: listener
265         });
266
267         // issue a request that succeeds
268         OpenLayers.Request.GET({
269             url: ".", params: {bar: "baz"}, async: false
270         });
271         t.eq(events.length, 2, "two events logged");
272         t.eq(events[0].type, "complete", "first event is complete");
273         t.eq(events[1].type, "success", "second event is success");
274         t.ok(events[1].config, "success listener sent config");
275         t.eq(events[1].requestUrl, ".?bar=baz", "success listener sent config.url");
276
277         // remove event listeners
278         OpenLayers.Request.events.un({
279             complete: listener,
280             success: listener,
281             failure: listener
282         });
283         
284     }
285
286     function test_events_failure(t) {
287         
288         t.plan(5);
289
290         var events = [];
291         function listener(event) {
292             events.push(event);
293         }
294         
295         // set up event listeners
296         OpenLayers.Request.events.on({
297             complete: listener,
298             success: listener,
299             failure: listener
300         });
301
302         // issue a request that succeeds
303         OpenLayers.Request.GET({
304             url: "foo", params: {bar: "baz"}, async: false
305         });
306         t.eq(events.length, 2, "two events logged");
307         t.eq(events[0].type, "complete", "first event is complete");
308         t.eq(events[1].type, "failure", "second event is failure");
309         t.ok(events[1].config, "failure listener sent config");
310         t.eq(events[1].requestUrl, "foo?bar=baz", "failure listener sent requestUrl");
311
312         // remove event listeners
313         OpenLayers.Request.events.un({
314             complete: listener,
315             success: listener,
316             failure: listener
317         });
318         
319     }
320
321     </script>
322 </head>
323 <body>
324 </body>
325 </html>