OpenLayers OpenLayers

root/trunk/openlayers/tests/Protocol/HTTP.html

Revision 8009, 22.9 kB (checked in by euzuro, 4 months ago)

fix broken HTTP Protocol test. Need to re-assign overridden values. This was not breaking in FF because (for whatever reason) tests are executed in backwards order. When they are executed in forwards order (IE), we have a problem. No functional change. See #1652

Line 
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5
6     function test_constructor(t) {
7         t.plan(8);
8         var a = new OpenLayers.Protocol.HTTP({
9             url: "foo"
10         });
11
12         // 4 tests
13         t.eq(a.url, "foo", "constructor sets url");
14         t.eq(a.options.url, a.url, "constructor copies url to options.url");
15         t.eq(a.params, {}, "constructor sets params");
16         t.eq(a.options.params, undefined, "constructor do not copy params to options.params");
17
18         var params = {hello: "world"};
19         var b = new OpenLayers.Protocol.HTTP({
20             url: "bar",
21             params: params
22         });
23
24         // 4 tests
25         t.eq(b.url, "bar", "constructor sets url");
26         t.eq(b.options.url, b.url, "constructor copies url to options.url");
27         t.eq(b.params, params, "constructor sets params");
28         t.eq(b.options.params, b.params, "constructor copies params to options.params");
29     }
30
31     function test_destroy(t) {
32         t.plan(3);
33         var protocol = new OpenLayers.Protocol.HTTP({
34             url: "bar",
35             params: {hello: "world"}
36         });
37         protocol.destroy();
38         t.eq(protocol.options, null, "destroy nullifies options");
39         t.eq(protocol.params, null, "destroy nullifies params");
40         t.eq(protocol.headers, null, "destroy nullifies headers");
41     }
42
43     function test_read(t) {
44         t.plan(10);
45         var protocol = new OpenLayers.Protocol.HTTP({
46             'url': 'foo_url',
47             'params': {'k': 'foo_param'}
48         });
49
50         // fake XHR request object
51         var request = {'status': 200};
52
53         // options to pass to read
54         var readOptions = {
55             'url': 'bar_url',
56             'params': {'k': 'bar_param'},
57             'headers': {'k': 'bar_header'},
58             'scope': {'hello': 'world'},
59             'callback': function() {}
60         };
61
62         var response;
63
64         protocol.handleResponse = function(resp, opt) {
65             // 4 tests
66             var req = resp.priv;
67             t.ok(this == protocol,
68                 'handleResponse called with correct scope');
69             t.ok(opt == readOptions,
70                 'handleResponse called with correct options');
71             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response',
72                 'handleResponse called with a Response object');
73             t.eq(req, request,
74                 'handleResponse called with correct request');
75
76             response = resp;
77         };
78
79         var _get = OpenLayers.Request.GET;
80
81         OpenLayers.Request.GET = function(options) {
82             // 5 tests
83             t.eq(options.url, readOptions.url,
84                 'GET called with correct url in options');
85             t.eq(options.params['k'], readOptions.params['k'],
86                 'GET called with correct params in options');
87             t.eq(options.headers['k'], readOptions.headers['k'],
88                 'GET called with correct headers in options');
89             t.eq(options.scope, undefined,
90                 'GET called with correct scope in options');
91             t.ok(typeof options.callback == 'function',
92                 'GET called with a callback in options');
93             t.delay_call(0.1, function() {
94                 options.callback(request);
95                 t.ok(resp == response,
96                     'read returns the expected response object');       
97                 // cleanup
98                 protocol.destroy();
99                 OpenLayers.Request.GET = _get;
100             });
101             return request;
102         };
103
104         var resp = protocol.read(readOptions);
105
106         OpenLayers.Request.GET = _get;
107     }
108
109     function test_read_bbox(t) {
110         t.plan(1);
111         var protocol = new OpenLayers.Protocol.HTTP();
112
113         // fake XHR request object
114         var request = {'status': 200};
115
116         var _get = OpenLayers.Request.GET;
117
118         var bounds = new OpenLayers.Bounds(1, 2, 3, 4);
119         var filter = new OpenLayers.Filter.Spatial({
120             type: OpenLayers.Filter.Spatial.BBOX,
121             value: bounds,
122             projection: "foo"
123         });
124        
125         OpenLayers.Request.GET = function(options) {
126             t.eq(options.params['bbox'].toString(), bounds.toArray().toString(),
127                 'GET called with bbox filter in params');
128             return request;
129         };
130
131         var resp = protocol.read({filter: filter});
132
133         OpenLayers.Request.GET = _get;       
134     }
135
136     function test_parseFeatures(t) {
137         t.plan(5);
138
139         var protocol = new OpenLayers.Protocol.HTTP();
140
141         // test responseXML - 2 tests
142         var request = {
143             'responseXML': {
144                 'documentElement': 'xml'
145             }
146         };
147         protocol.format = {
148             'read': function(doc) {
149                 t.eq(doc.documentElement, 'xml',
150                     'format.read called with correct doc');
151                 return doc.documentElement;
152             }
153         };
154         var ret = protocol.parseFeatures(request);
155         t.eq(ret, 'xml', 'parseFeatures returns expected value');
156
157         // test responseText - 2 tests
158         var request = {
159             'responseText': 'text'
160         };
161         protocol.format = {
162             'read': function(doc) {
163                 t.eq(doc, 'text',
164                     'format.read called with correct doc');
165                 return doc;
166             }
167         };
168         var ret = protocol.parseFeatures(request);
169         t.eq(ret, 'text', 'parseFeatures returns expected value');
170
171         // test empty responseText - 1 test
172         var request = {
173             'responseText': ''
174         };
175         protocol.format = {
176             'read': function(doc) {
177                 t.fail('format.read should not be called');
178             }
179         };
180         var ret = protocol.parseFeatures(request);
181         t.eq(ret, null, 'parseFeatures returns expected value');
182     }
183
184     function test_create(t) {
185         t.plan(10);
186         var protocol = new OpenLayers.Protocol.HTTP({
187             'url': 'foo_url',
188             'format': {'write': function() {}}
189         });
190
191         // fake XHR request object
192         var request = {'status': 200};
193
194         // features to pass to create
195         var features = ['feature'];
196
197         // options to pass to create
198         var createOptions = {
199             'url': 'bar_url',
200             'headers': {'k': 'bar_header'},
201             'scope': {'hello': 'world'},
202             'callback': function() {}
203         };
204
205         var response;
206
207         protocol.handleCreate = function(resp, opt) {
208             // 5 tests
209             var req = resp.priv;
210             t.ok(this == protocol,
211                 'handleCreate called with correct scope');
212             t.ok(opt == createOptions,
213                 'handleCreate called with correct options');
214             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response',
215                 'handleCreate called with a Response object');
216             t.ok(resp.reqFeatures == features,
217                 'handleCreate called with correct requested features in response');
218             t.eq(req, request,
219                 'handleCreate called with correct request');
220
221             response = resp;
222         };
223
224         var _post = OpenLayers.Request.POST;
225
226         OpenLayers.Request.POST = function(options) {
227             // 4 tests
228             t.eq(options.url, createOptions.url,
229                 'POST called with correct url in options');
230             t.eq(options.headers['k'], createOptions.headers['k'],
231                 'POST called with correct headers in options');
232             t.eq(options.scope, undefined,
233                 'POST called with correct scope in options');
234             t.ok(typeof options.callback == 'function',
235                 'POST called with a callback in options');
236             // call callback - delayed because this function has to return first
237             t.delay_call(0.1, function() {
238                 options.callback(request);
239                 t.ok(resp == response,
240                     'create returns the expected response object');
241                 // cleanup
242                 protocol.destroy();
243                 OpenLayers.Request.POST = _post;
244             });
245             return request;
246         };
247
248         var resp = protocol.create(features, createOptions);
249        
250         OpenLayers.Request.POST = _post;
251     }
252
253     function test_update(t) {
254         t.plan(10);
255         var protocol = new OpenLayers.Protocol.HTTP({
256             'url': 'foo_url',
257             'format': {'write': function() {}}
258         });
259
260         // fake XHR request object
261         var request = {'status': 200};
262
263         // feature to pass to update
264         var feature = {'feature':'feature'};
265
266         // options to pass to update
267         var updateOptions = {
268             'url': 'bar_url',
269             'headers': {'k': 'bar_header'},
270             'scope': {'hello': 'world'},
271             'callback': function() {}
272         };
273
274         var response;
275
276         protocol.handleUpdate = function(resp, opt) {
277             var req = resp.priv;
278             // 5 tests
279             t.ok(this == protocol,
280                 'handleUpdate called with correct scope');
281             t.ok(opt == updateOptions,
282                 'handleUpdate called with correct options');
283             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response',
284                 'handleUpdate called with a Response object');
285             t.ok(resp.reqFeatures == feature,
286                 'handleUpdate called with correct requested feature in response');
287             t.eq(req, request,
288                 'handleUpdate called with correct request');
289
290             response = resp;
291         };
292
293         var _put = OpenLayers.Request.PUT;
294
295         OpenLayers.Request.PUT = function(options) {
296             // 4 tests
297             t.eq(options.url, updateOptions.url,
298                 'PUT called with correct url in options');
299             t.eq(options.headers['k'], updateOptions.headers['k'],
300                 'PUT called with correct headers in options');
301             t.eq(options.scope, undefined,
302                 'PUT called with correct scope in options');
303             t.ok(typeof options.callback == 'function',
304                 'PUT called with a callback in options');
305             // call callback - delayed because this function has to return first
306             t.delay_call(0.1, function() {
307                 options.callback(request);
308                 t.ok(resp == response,
309                     'update returns the expected response object');
310                 // cleanup
311                 protocol.destroy();
312                 OpenLayers.Request.PUT = _put;
313             });
314             return request;
315         };
316
317         var resp = protocol.update(feature, updateOptions);
318        
319         OpenLayers.Request.PUT = _put;
320     }
321
322     function test_handleResponse(t) {
323         t.plan(6);
324
325         var protocol = new OpenLayers.Protocol.HTTP();
326
327         var options, response, request, features;
328
329         // test options - 2 tests
330         var scope = {'fake': 'scope'};
331         options = {
332             'scope': scope,
333             'callback': function(resp) {
334                 t.ok(this == scope,
335                     '[no status] callback called with correct scope');
336                 t.ok(resp == response,
337                     '[no status] callback called with correct response');
338             }
339         };
340         response = {priv: {}};
341         protocol.handleResponse(response, options);
342        
343         // test failure condition - 1 test
344         options = {
345             'callback': function(resp) {
346                 t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE,
347                     '[status 400] callback called with correct response code');
348             }
349         };
350         response = {priv: {status: 400}};
351         protocol.handleResponse(response, options);
352
353         // test success condition - 3 tests
354         features = {'fake': 'features'};
355         options = {
356             'callback': function(resp) {
357                 t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS,
358                     '[status 200] callback called with correct response code');
359                 t.eq(resp.features, features,
360                     '[status 200] callback called with correct features in response');
361             }
362         };
363         response = {priv: {status: 200}};
364         protocol.parseFeatures = function(request) {
365             t.ok(request == response.priv,
366                 '[status 200] parseFeatures called with correct request');
367             return features;
368         }
369         protocol.handleResponse(response, options);
370
371         // cleanup
372         protocol.destroy();
373     }
374
375     function test_delete(t) {
376         t.plan(10);
377         var protocol = new OpenLayers.Protocol.HTTP({
378             'url': 'foo_url'
379         });
380
381         // fake XHR request object
382         var request = {'status': 200};
383
384         // feature to pass to delete
385         var feature = {'url': 'bar_url'};
386
387         // options to pass to delete
388         var deleteOptions = {
389             'url': 'bar_url',
390             'headers': {'k': 'bar_header'},
391             'scope': {'hello': 'world'},
392             'callback': function() {}
393         };
394
395         var response;
396
397         protocol.handleDelete = function(resp, opt) {
398             // 5 tests
399             var req = resp.priv;
400             t.ok(this == protocol,
401                 'handleDelete called with correct scope');
402             t.ok(opt == deleteOptions,
403                 'handleDelete called with correct options');
404             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response',
405                 'handleDelete called with a Response object');
406             t.ok(resp.reqFeatures == feature,
407                 'handleDelete called with correct requested feature in response');
408             t.eq(req, request,
409                 'handleDelete called with correct request');
410
411             response = resp;
412         };
413
414         var _delete = OpenLayers.Request.DELETE;
415
416         OpenLayers.Request.DELETE = function(options) {
417             // 4 tests
418             t.eq(options.url, deleteOptions.url,
419                 'DELETE called with correct url in options');
420             t.eq(options.headers['k'], deleteOptions.headers['k'],
421                 'DELETE called with correct headers in options');
422             t.eq(options.scope, undefined,
423                 'DELETE called with correct scope in options');
424             t.ok(typeof options.callback == 'function',
425                 'DELETE called with a callback in options');
426             // call callback - delayed because this function has to return first
427             t.delay_call(0.1, function() {
428                 options.callback(request);
429                 t.ok(resp == response,
430                     'read returns the expected response object');
431                 // cleanup
432                 protocol.destroy();
433                 OpenLayers.Request.DELETE = _delete;
434             });
435             return request;
436         };
437
438         var resp = protocol['delete'](feature, deleteOptions);
439
440         OpenLayers.Request.DELETE = _delete;
441     }
442
443     function test_handleDelete(t) {
444         t.plan(4);
445
446         var protocol = new OpenLayers.Protocol.HTTP();
447
448         var options, response, request, features;
449
450         // test options - 2 tests
451         var scope = {'fake': 'scope'};
452         options = {
453             'scope': scope,
454             'callback': function(resp) {
455                 t.ok(this == scope,
456                     'callback called with correct scope');
457                 t.ok(resp == response,
458                     'callback called with correct response');
459             }
460         };
461         response = {priv: {}};
462         protocol.handleDelete(response, options);
463        
464         // test failure condition - 1 test
465         options = {
466             'callback': function(resp) {
467                 t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE,
468                     'callback called with correct response code');
469             }
470         };
471         response = {priv: {status: 400}};
472         protocol.handleDelete(response, options);
473
474         // test success condition - 1 test
475         options = {
476             'callback': function(resp) {
477                 t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS,
478                     'callback called with correct response code');
479             }
480         };
481         response = {priv: {status: 200}};
482         protocol.handleDelete(response, options);
483
484         // cleanup
485         protocol.destroy();
486     }
487
488     function test_commit(t) {
489         t.plan(17);
490
491         var protocol = new OpenLayers.Protocol.HTTP();
492
493         // 6 features
494         var features = [
495             {'state': OpenLayers.State.INSERT},
496             {'state': OpenLayers.State.INSERT},
497             {'state': OpenLayers.State.UPDATE},
498             {'state': OpenLayers.State.UPDATE},
499             {'state': OpenLayers.State.DELETE},
500             {'state': OpenLayers.State.DELETE}
501         ];
502
503         var options = {
504             'create': {
505                 'callback': function(resp) {
506                 }
507             },
508             'update': {
509                 'callback': function(resp) {
510                 }
511             },
512             'delete': {
513                 'callback': function(resp) {
514                 }
515             }
516         };
517
518         var respCreate = new OpenLayers.Protocol.Response();
519         var respUpdate = new OpenLayers.Protocol.Response();
520         var respDelete = new OpenLayers.Protocol.Response();
521
522         // 2 tests
523         protocol['create'] = function(feature, options) {
524             t.ok(options.scope == protocol,
525                 'create called with correct scope');
526             t.ok(typeof options.callback == 'function',
527                 'create called with a callback in options');
528             options.callback.call(options.scope, respCreate);
529             return respCreate;
530         };
531         // 4 tests
532         protocol['update'] = function(feature, options) {
533             t.ok(options.scope == protocol,
534                 'update called with correct scope');
535             t.ok(typeof options.callback == 'function',
536                 'update called with a callback in options');
537             options.callback.call(options.scope, respUpdate);
538             return respUpdate;
539         };
540         // 4 tests
541         protocol['delete'] = function(feature, options) {
542             t.ok(options.scope == protocol,
543                 'delete called with correct scope');
544             t.ok(typeof options.callback == 'function',
545                 'delete called with a callback in options');
546             options.