]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Protocol/HTTP.html
initial commit
[syp.git] / openlayers / tests / Protocol / HTTP.html
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_readWithPOST(t) {
110         t.plan(10);
111         var protocol = new OpenLayers.Protocol.HTTP({
112             'url': 'foo_url',
113             'params': {'k': 'foo_param'}
114         });
115
116         // fake XHR request object
117         var request = {'status': 200};
118
119         // options to pass to read
120         var readOptions = {
121             'url': 'bar_url',
122             'params': {'k': 'bar_param'},
123             'scope': {'hello': 'world'},
124             'callback': function() {},
125             'readWithPOST': true
126         };
127
128         var response;
129
130         protocol.handleResponse = function(resp, opt) {
131             // 4 tests
132             var req = resp.priv;
133             t.ok(this == protocol,
134                 'handleResponse called with correct scope');
135             t.ok(opt == readOptions,
136                 'handleResponse called with correct options');
137             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response',
138                 'handleResponse called with a Response object');
139             t.eq(req, request,
140                 'handleResponse called with correct request');
141
142             response = resp;
143         };
144
145         var _post = OpenLayers.Request.POST;
146
147         OpenLayers.Request.POST = function(options) {
148             // 5 tests
149             t.eq(options.url, readOptions.url,
150                 'GET with POST called with correct url in options');
151             t.eq(options.data, OpenLayers.Util.getParameterString(readOptions.params),
152                 'GET with POST called with correct params encoded in options');
153             t.eq(options.headers, {"Content-Type": "application/x-www-form-urlencoded"},
154                 'GET with POST called with correct headers (application/x-www-form-urlencoded)');
155             t.eq(options.scope, undefined,
156                 'GET with POST called with correct scope in options');
157             t.ok(typeof options.callback == 'function',
158                 'GET with POST called with a callback in options');
159             t.delay_call(0.1, function() {
160                 options.callback(request);
161                 t.ok(resp == response,
162                     'read returns the expected response object');        
163                 // cleanup
164                 protocol.destroy();
165                 OpenLayers.Request.POST = _post;
166             });
167             return request;
168         };
169
170         var resp = protocol.read(readOptions);
171
172         OpenLayers.Request.POST = _post;
173     }
174
175     function test_read_method(t) {
176         t.plan(4);
177
178         var _post = OpenLayers.Request.POST;
179         OpenLayers.Request.POST = function(options) { return 'post'; }
180         var _get = OpenLayers.Request.GET;
181         OpenLayers.Request.GET = function(options) { return 'get'; }
182
183         var protocol = new OpenLayers.Protocol.HTTP({});
184
185         t.eq(protocol.read({}).priv, 'get',
186             'readWithPOST is false by default');
187         t.eq(protocol.read({readWithPOST: true}).priv, 'post',
188             'readWithPOST can be set in read options');
189
190         var protocol = new OpenLayers.Protocol.HTTP({readWithPOST: true});
191
192         t.eq(protocol.read({}).priv, 'post',
193             'readWithPOST can be set in constructor');
194         t.eq(protocol.read({readWithPOST: false}).priv, 'get',
195             'readWithPOST can be overridden in read options');
196
197         OpenLayers.Request.POST = _post;
198         OpenLayers.Request.GET = _get;
199     }
200
201     function test_read_bbox(t) {
202         t.plan(1);
203         var protocol = new OpenLayers.Protocol.HTTP();
204
205         // fake XHR request object
206         var request = {'status': 200};
207
208         var _get = OpenLayers.Request.GET;
209
210         var bounds = new OpenLayers.Bounds(1, 2, 3, 4);
211         var filter = new OpenLayers.Filter.Spatial({
212             type: OpenLayers.Filter.Spatial.BBOX,
213             value: bounds,
214             projection: "foo"
215         });
216         
217         OpenLayers.Request.GET = function(options) {
218             t.eq(options.params['bbox'].toString(), bounds.toArray().toString(),
219                 'GET called with bbox filter in params');
220             return request;
221         };
222
223         var resp = protocol.read({filter: filter});
224
225         OpenLayers.Request.GET = _get;        
226     }
227
228     function test_parseFeatures(t) {
229         t.plan(5);
230
231         var protocol = new OpenLayers.Protocol.HTTP();
232
233         // test responseXML - 2 tests
234         var request = {
235             'responseXML': {
236                 'documentElement': 'xml'
237             }
238         };
239         protocol.format = {
240             'read': function(doc) {
241                 t.eq(doc.documentElement, 'xml',
242                     'format.read called with correct doc');
243                 return doc.documentElement;
244             }
245         };
246         var ret = protocol.parseFeatures(request);
247         t.eq(ret, 'xml', 'parseFeatures returns expected value');
248
249         // test responseText - 2 tests
250         var request = {
251             'responseText': 'text'
252         };
253         protocol.format = {
254             'read': function(doc) {
255                 t.eq(doc, 'text',
256                     'format.read called with correct doc');
257                 return doc;
258             }
259         };
260         var ret = protocol.parseFeatures(request);
261         t.eq(ret, 'text', 'parseFeatures returns expected value');
262
263         // test empty responseText - 1 test
264         var request = {
265             'responseText': ''
266         };
267         protocol.format = {
268             'read': function(doc) {
269                 t.fail('format.read should not be called');
270             }
271         };
272         var ret = protocol.parseFeatures(request);
273         t.eq(ret, null, 'parseFeatures returns expected value');
274     }
275
276     function test_create(t) {
277         t.plan(10);
278         var protocol = new OpenLayers.Protocol.HTTP({
279             'url': 'foo_url',
280             'format': {'write': function() {}}
281         });
282
283         // fake XHR request object
284         var request = {'status': 200};
285
286         // features to pass to create
287         var features = ['feature'];
288
289         // options to pass to create
290         var createOptions = {
291             'url': 'bar_url',
292             'headers': {'k': 'bar_header'},
293             'scope': {'hello': 'world'},
294             'callback': function() {}
295         };
296
297         var response;
298
299         protocol.handleCreate = function(resp, opt) {
300             // 5 tests
301             var req = resp.priv;
302             t.ok(this == protocol,
303                 'handleCreate called with correct scope');
304             t.ok(opt == createOptions,
305                 'handleCreate called with correct options');
306             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response',
307                 'handleCreate called with a Response object');
308             t.ok(resp.reqFeatures == features,
309                 'handleCreate called with correct requested features in response');
310             t.eq(req, request,
311                 'handleCreate called with correct request');
312
313             response = resp;
314         };
315
316         var _post = OpenLayers.Request.POST;
317
318         OpenLayers.Request.POST = function(options) {
319             // 4 tests
320             t.eq(options.url, createOptions.url,
321                 'POST called with correct url in options');
322             t.eq(options.headers['k'], createOptions.headers['k'],
323                 'POST called with correct headers in options');
324             t.eq(options.scope, undefined,
325                 'POST called with correct scope in options');
326             t.ok(typeof options.callback == 'function',
327                 'POST called with a callback in options');
328             // call callback - delayed because this function has to return first
329             t.delay_call(0.1, function() {
330                 options.callback(request);
331                 t.ok(resp == response,
332                     'create returns the expected response object');
333                 // cleanup
334                 protocol.destroy();
335                 OpenLayers.Request.POST = _post;
336             });
337             return request;
338         };
339
340         var resp = protocol.create(features, createOptions);
341         
342         OpenLayers.Request.POST = _post;
343     }
344
345     function test_update(t) {
346         t.plan(10);
347         var protocol = new OpenLayers.Protocol.HTTP({
348             'url': 'foo_url',
349             'format': {'write': function() {}}
350         });
351
352         // fake XHR request object
353         var request = {'status': 200};
354
355         // feature to pass to update
356         var feature = {'feature':'feature'};
357
358         // options to pass to update
359         var updateOptions = {
360             'url': 'bar_url',
361             'headers': {'k': 'bar_header'},
362             'scope': {'hello': 'world'},
363             'callback': function() {}
364         };
365
366         var response;
367
368         protocol.handleUpdate = function(resp, opt) {
369             var req = resp.priv;
370             // 5 tests
371             t.ok(this == protocol,
372                 'handleUpdate called with correct scope');
373             t.ok(opt == updateOptions,
374                 'handleUpdate called with correct options');
375             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response',
376                 'handleUpdate called with a Response object');
377             t.ok(resp.reqFeatures == feature,
378                 'handleUpdate called with correct requested feature in response');
379             t.eq(req, request,
380                 'handleUpdate called with correct request');
381
382             response = resp;
383         };
384
385         var _put = OpenLayers.Request.PUT;
386
387         OpenLayers.Request.PUT = function(options) {
388             // 4 tests
389             t.eq(options.url, updateOptions.url,
390                 'PUT called with correct url in options');
391             t.eq(options.headers['k'], updateOptions.headers['k'],
392                 'PUT called with correct headers in options');
393             t.eq(options.scope, undefined,
394                 'PUT called with correct scope in options');
395             t.ok(typeof options.callback == 'function',
396                 'PUT called with a callback in options');
397             // call callback - delayed because this function has to return first
398             t.delay_call(0.1, function() {
399                 options.callback(request);
400                 t.ok(resp == response,
401                     'update returns the expected response object');
402                 // cleanup
403                 protocol.destroy();
404                 OpenLayers.Request.PUT = _put;
405             });
406             return request;
407         };
408
409         var resp = protocol.update(feature, updateOptions);
410         
411         OpenLayers.Request.PUT = _put;
412     }
413
414     function test_handleResponse(t) {
415         t.plan(6);
416
417         var protocol = new OpenLayers.Protocol.HTTP();
418
419         var options, response, request, features;
420
421         // test options - 2 tests
422         var scope = {'fake': 'scope'};
423         options = {
424             'scope': scope,
425             'callback': function(resp) {
426                 t.ok(this == scope,
427                     '[no status] callback called with correct scope');
428                 t.ok(resp == response,
429                     '[no status] callback called with correct response');
430             }
431         };
432         response = {priv: {}};
433         protocol.handleResponse(response, options);
434         
435         // test failure condition - 1 test
436         options = {
437             'callback': function(resp) {
438                 t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE,
439                     '[status 400] callback called with correct response code');
440             }
441         };
442         response = {priv: {status: 400}};
443         protocol.handleResponse(response, options);
444
445         // test success condition - 3 tests
446         features = {'fake': 'features'};
447         options = {
448             'callback': function(resp) {
449                 t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS,
450                     '[status 200] callback called with correct response code');
451                 t.eq(resp.features, features,
452                     '[status 200] callback called with correct features in response');
453             }
454         };
455         response = {priv: {status: 200}};
456         protocol.parseFeatures = function(request) {
457             t.ok(request == response.priv,
458                 '[status 200] parseFeatures called with correct request');
459             return features;
460         }
461         protocol.handleResponse(response, options);
462
463         // cleanup
464         protocol.destroy();
465     }
466
467     function test_delete(t) {
468         t.plan(10);
469         var protocol = new OpenLayers.Protocol.HTTP({
470             'url': 'foo_url'
471         });
472
473         // fake XHR request object
474         var request = {'status': 200};
475
476         // feature to pass to delete
477         var feature = {'url': 'bar_url'};
478
479         // options to pass to delete
480         var deleteOptions = {
481             'url': 'bar_url',
482             'headers': {'k': 'bar_header'},
483             'scope': {'hello': 'world'},
484             'callback': function() {}
485         };
486
487         var response;
488
489         protocol.handleDelete = function(resp, opt) {
490             // 5 tests
491             var req = resp.priv;
492             t.ok(this == protocol,
493                 'handleDelete called with correct scope');
494             t.ok(opt == deleteOptions,
495                 'handleDelete called with correct options');
496             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response',
497                 'handleDelete called with a Response object');
498             t.ok(resp.reqFeatures == feature,
499                 'handleDelete called with correct requested feature in response');
500             t.eq(req, request,
501                 'handleDelete called with correct request');
502
503             response = resp;
504         };
505
506         var _delete = OpenLayers.Request.DELETE;
507
508         OpenLayers.Request.DELETE = function(options) {
509             // 4 tests
510             t.eq(options.url, deleteOptions.url,
511                 'DELETE called with correct url in options');
512             t.eq(options.headers['k'], deleteOptions.headers['k'],
513                 'DELETE called with correct headers in options');
514             t.eq(options.scope, undefined,
515                 'DELETE called with correct scope in options');
516             t.ok(typeof options.callback == 'function',
517                 'DELETE called with a callback in options');
518             // call callback - delayed because this function has to return first
519             t.delay_call(0.1, function() {
520                 options.callback(request);
521                 t.ok(resp == response,
522                     'read returns the expected response object');
523                 // cleanup
524                 protocol.destroy();
525                 OpenLayers.Request.DELETE = _delete;
526             });
527             return request;
528         };
529
530         var resp = protocol['delete'](feature, deleteOptions);
531
532         OpenLayers.Request.DELETE = _delete;
533     }
534
535     function test_handleDelete(t) {
536         t.plan(4);
537
538         var protocol = new OpenLayers.Protocol.HTTP();
539
540         var options, response, request, features;
541
542         // test options - 2 tests
543         var scope = {'fake': 'scope'};
544         options = {
545             'scope': scope,
546             'callback': function(resp) {
547                 t.ok(this == scope,
548                     'callback called with correct scope');
549                 t.ok(resp == response,
550                     'callback called with correct response');
551             }
552         };
553         response = {priv: {}};
554         protocol.handleDelete(response, options);
555         
556         // test failure condition - 1 test
557         options = {
558             'callback': function(resp) {
559                 t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE,
560                     'callback called with correct response code');
561             }
562         };
563         response = {priv: {status: 400}};
564         protocol.handleDelete(response, options);
565
566         // test success condition - 1 test
567         options = {
568             'callback': function(resp) {
569                 t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS,
570                     'callback called with correct response code');
571             }
572         };
573         response = {priv: {status: 200}};
574         protocol.handleDelete(response, options);
575
576         // cleanup
577         protocol.destroy();
578     }
579
580     function test_commit(t) {
581         t.plan(17);
582
583         var protocol = new OpenLayers.Protocol.HTTP();
584
585         // 6 features
586         var features = [
587             {'state': OpenLayers.State.INSERT},
588             {'state': OpenLayers.State.INSERT},
589             {'state': OpenLayers.State.UPDATE},
590             {'state': OpenLayers.State.UPDATE},
591             {'state': OpenLayers.State.DELETE},
592             {'state': OpenLayers.State.DELETE}
593         ];
594
595         var options = {
596             'create': {
597                 'callback': function(resp) {
598                 }
599             },
600             'update': {
601                 'callback': function(resp) {
602                 }
603             },
604             'delete': {
605                 'callback': function(resp) {
606                 }
607             }
608         };
609
610         var respCreate = new OpenLayers.Protocol.Response();
611         var respUpdate = new OpenLayers.Protocol.Response();
612         var respDelete = new OpenLayers.Protocol.Response();
613
614         // 2 tests
615         protocol['create'] = function(feature, options) {
616             t.ok(options.scope == protocol,
617                 'create called with correct scope');
618             t.ok(typeof options.callback == 'function',
619                 'create called with a callback in options');
620             options.callback.call(options.scope, respCreate);
621             return respCreate;
622         };
623         // 4 tests
624         protocol['update'] = function(feature, options) {
625             t.ok(options.scope == protocol,
626                 'update called with correct scope');
627             t.ok(typeof options.callback == 'function',
628                 'update called with a callback in options');
629             options.callback.call(options.scope, respUpdate);
630             return respUpdate;
631         };
632         // 4 tests
633         protocol['delete'] = function(feature, options) {
634             t.ok(options.scope == protocol,
635                 'delete called with correct scope');
636             t.ok(typeof options.callback == 'function',
637                 'delete called with a callback in options');
638             options.callback.call(options.scope, respDelete);
639             return respDelete;
640         };
641
642         var count = 0;
643
644         // 5 tests
645         protocol.callUserCallback = function(resp, opt) {
646             t.ok(opt == options,
647                 'callUserCallback called with correction options map');
648             count++;
649         };
650
651         var resp = protocol.commit(features, options);
652
653         // 2 tests
654         t.eq(count, 5, 'callUserCallback called for each request');
655         t.eq(resp.length, 5, 'commit returns array with correct length');
656
657         // cleanup
658         protocol.destroy();
659     }
660
661     function test_callUserCallback(t) {
662         t.plan(1);
663
664         var protocol = new OpenLayers.Protocol.HTTP();
665
666         var scope = {'fake': 'scope'};
667
668         // test commit callback
669         var log = {};
670         var options = {
671             foo: {
672                 callback: function() {
673                     log.scope = this;
674                 },
675                 scope: scope
676             }
677         };
678         var resp = {requestType: 'foo'};
679         protocol.callUserCallback(resp, options);
680         t.ok(log.scope, scope, 'correct callback called with correct scope');
681
682     }
683
684     function test_options(t) {
685         t.plan(6);
686         
687         var log1 = {};
688
689         // test that read with no options uses protocol options - 5 tests
690         var url = ".";
691         var headers = {};
692         var params = {};
693         var scope = {};
694         var protocol = new OpenLayers.Protocol.HTTP({
695             format: new OpenLayers.Format({
696                 read: function() {},
697                 write: function() {}
698             }),
699             url: url,
700             headers: headers,
701             params: params,
702             callback: function(resp) {
703                 log1.callbackCalled = true;
704                 log1.callbackScope = this;
705                 log1.request = resp && resp.priv;
706                 log1.requestType = resp && resp.requestType;
707             },
708             scope: scope
709         });        
710         protocol.read();
711         
712         t.delay_call(0.5, function() {
713             t.eq(log1.callbackCalled, true, "[read] callback called");
714             t.eq(log1.callbackScope, scope, "[read] correct scope");
715             t.ok(log1.request instanceof OpenLayers.Request.XMLHttpRequest, "[read] correct priv type");
716             t.eq(log1.requestType, "read", "[read] correct request type");
717         });
718         
719
720         // test that commit with no options uses protocol options - 2 tests
721         var log2 = {called: 0};
722         protocol.options.callback = function() {
723             log2.called++;
724             log2.scope = this;
725         };
726         protocol.commit([
727             {state: OpenLayers.State.INSERT},
728             {state: OpenLayers.State.INSERT},
729             {state: OpenLayers.State.UPDATE, url: "./1"},
730             {state: OpenLayers.State.UPDATE, url: "./2"},
731             {state: OpenLayers.State.DELETE, url: "./3"},
732             {state: OpenLayers.State.DELETE, url: "./4"}
733         ]);
734         t.delay_call(0.5, function() {
735             t.eq(log2.called, 1, "[commit] Callback called once.");
736             t.eq(log2.scope, scope, "[commit] Correct scope.");
737         });
738
739     }
740
741
742   </script>
743 </head>
744 <body>
745 </body>
746 </html>