3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_constructor(t) {
8 var a = new OpenLayers.Protocol.HTTP({
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");
18 var params = {hello: "world"};
19 var b = new OpenLayers.Protocol.HTTP({
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");
31 function test_destroy(t) {
33 var protocol = new OpenLayers.Protocol.HTTP({
35 params: {hello: "world"}
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");
43 function test_read(t) {
45 var protocol = new OpenLayers.Protocol.HTTP({
47 'params': {'k': 'foo_param'}
50 // fake XHR request object
51 var request = {'status': 200};
53 // options to pass to read
56 'params': {'k': 'bar_param'},
57 'headers': {'k': 'bar_header'},
58 'scope': {'hello': 'world'},
59 'callback': function() {}
64 protocol.handleResponse = function(resp, opt) {
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');
74 'handleResponse called with correct request');
79 var _get = OpenLayers.Request.GET;
81 OpenLayers.Request.GET = function(options) {
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');
99 OpenLayers.Request.GET = _get;
104 var resp = protocol.read(readOptions);
106 OpenLayers.Request.GET = _get;
109 function test_readWithPOST(t) {
111 var protocol = new OpenLayers.Protocol.HTTP({
113 'params': {'k': 'foo_param'}
116 // fake XHR request object
117 var request = {'status': 200};
119 // options to pass to read
122 'params': {'k': 'bar_param'},
123 'scope': {'hello': 'world'},
124 'callback': function() {},
130 protocol.handleResponse = function(resp, opt) {
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');
140 'handleResponse called with correct request');
145 var _post = OpenLayers.Request.POST;
147 OpenLayers.Request.POST = function(options) {
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');
165 OpenLayers.Request.POST = _post;
170 var resp = protocol.read(readOptions);
172 OpenLayers.Request.POST = _post;
175 function test_read_method(t) {
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'; }
183 var protocol = new OpenLayers.Protocol.HTTP({});
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');
190 var protocol = new OpenLayers.Protocol.HTTP({readWithPOST: true});
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');
197 OpenLayers.Request.POST = _post;
198 OpenLayers.Request.GET = _get;
201 function test_read_bbox(t) {
203 var protocol = new OpenLayers.Protocol.HTTP();
205 // fake XHR request object
206 var request = {'status': 200};
208 var _get = OpenLayers.Request.GET;
210 var bounds = new OpenLayers.Bounds(1, 2, 3, 4);
211 var filter = new OpenLayers.Filter.Spatial({
212 type: OpenLayers.Filter.Spatial.BBOX,
217 OpenLayers.Request.GET = function(options) {
218 t.eq(options.params['bbox'].toString(), bounds.toArray().toString(),
219 'GET called with bbox filter in params');
223 var resp = protocol.read({filter: filter});
225 OpenLayers.Request.GET = _get;
228 function test_parseFeatures(t) {
231 var protocol = new OpenLayers.Protocol.HTTP();
233 // test responseXML - 2 tests
236 'documentElement': 'xml'
240 'read': function(doc) {
241 t.eq(doc.documentElement, 'xml',
242 'format.read called with correct doc');
243 return doc.documentElement;
246 var ret = protocol.parseFeatures(request);
247 t.eq(ret, 'xml', 'parseFeatures returns expected value');
249 // test responseText - 2 tests
251 'responseText': 'text'
254 'read': function(doc) {
256 'format.read called with correct doc');
260 var ret = protocol.parseFeatures(request);
261 t.eq(ret, 'text', 'parseFeatures returns expected value');
263 // test empty responseText - 1 test
268 'read': function(doc) {
269 t.fail('format.read should not be called');
272 var ret = protocol.parseFeatures(request);
273 t.eq(ret, null, 'parseFeatures returns expected value');
276 function test_create(t) {
278 var protocol = new OpenLayers.Protocol.HTTP({
280 'format': {'write': function() {}}
283 // fake XHR request object
284 var request = {'status': 200};
286 // features to pass to create
287 var features = ['feature'];
289 // options to pass to create
290 var createOptions = {
292 'headers': {'k': 'bar_header'},
293 'scope': {'hello': 'world'},
294 'callback': function() {}
299 protocol.handleCreate = function(resp, opt) {
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');
311 'handleCreate called with correct request');
316 var _post = OpenLayers.Request.POST;
318 OpenLayers.Request.POST = function(options) {
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');
335 OpenLayers.Request.POST = _post;
340 var resp = protocol.create(features, createOptions);
342 OpenLayers.Request.POST = _post;
345 function test_update(t) {
347 var protocol = new OpenLayers.Protocol.HTTP({
349 'format': {'write': function() {}}
352 // fake XHR request object
353 var request = {'status': 200};
355 // feature to pass to update
356 var feature = {'feature':'feature'};
358 // options to pass to update
359 var updateOptions = {
361 'headers': {'k': 'bar_header'},
362 'scope': {'hello': 'world'},
363 'callback': function() {}
368 protocol.handleUpdate = function(resp, opt) {
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');
380 'handleUpdate called with correct request');
385 var _put = OpenLayers.Request.PUT;
387 OpenLayers.Request.PUT = function(options) {
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');
404 OpenLayers.Request.PUT = _put;
409 var resp = protocol.update(feature, updateOptions);
411 OpenLayers.Request.PUT = _put;
414 function test_handleResponse(t) {
417 var protocol = new OpenLayers.Protocol.HTTP();
419 var options, response, request, features;
421 // test options - 2 tests
422 var scope = {'fake': 'scope'};
425 'callback': function(resp) {
427 '[no status] callback called with correct scope');
428 t.ok(resp == response,
429 '[no status] callback called with correct response');
432 response = {priv: {}};
433 protocol.handleResponse(response, options);
435 // test failure condition - 1 test
437 'callback': function(resp) {
438 t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE,
439 '[status 400] callback called with correct response code');
442 response = {priv: {status: 400}};
443 protocol.handleResponse(response, options);
445 // test success condition - 3 tests
446 features = {'fake': 'features'};
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');
455 response = {priv: {status: 200}};
456 protocol.parseFeatures = function(request) {
457 t.ok(request == response.priv,
458 '[status 200] parseFeatures called with correct request');
461 protocol.handleResponse(response, options);
467 function test_delete(t) {
469 var protocol = new OpenLayers.Protocol.HTTP({
473 // fake XHR request object
474 var request = {'status': 200};
476 // feature to pass to delete
477 var feature = {'url': 'bar_url'};
479 // options to pass to delete
480 var deleteOptions = {
482 'headers': {'k': 'bar_header'},
483 'scope': {'hello': 'world'},
484 'callback': function() {}
489 protocol.handleDelete = function(resp, opt) {
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');
501 'handleDelete called with correct request');
506 var _delete = OpenLayers.Request.DELETE;
508 OpenLayers.Request.DELETE = function(options) {
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');
525 OpenLayers.Request.DELETE = _delete;
530 var resp = protocol['delete'](feature, deleteOptions);
532 OpenLayers.Request.DELETE = _delete;
535 function test_handleDelete(t) {
538 var protocol = new OpenLayers.Protocol.HTTP();
540 var options, response, request, features;
542 // test options - 2 tests
543 var scope = {'fake': 'scope'};
546 'callback': function(resp) {
548 'callback called with correct scope');
549 t.ok(resp == response,
550 'callback called with correct response');
553 response = {priv: {}};
554 protocol.handleDelete(response, options);
556 // test failure condition - 1 test
558 'callback': function(resp) {
559 t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE,
560 'callback called with correct response code');
563 response = {priv: {status: 400}};
564 protocol.handleDelete(response, options);
566 // test success condition - 1 test
568 'callback': function(resp) {
569 t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS,
570 'callback called with correct response code');
573 response = {priv: {status: 200}};
574 protocol.handleDelete(response, options);
580 function test_commit(t) {
583 var protocol = new OpenLayers.Protocol.HTTP();
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}
597 'callback': function(resp) {
601 'callback': function(resp) {
605 'callback': function(resp) {
610 var respCreate = new OpenLayers.Protocol.Response();
611 var respUpdate = new OpenLayers.Protocol.Response();
612 var respDelete = new OpenLayers.Protocol.Response();
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);
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);
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);
645 protocol.callUserCallback = function(resp, opt) {
647 'callUserCallback called with correction options map');
651 var resp = protocol.commit(features, options);
654 t.eq(count, 5, 'callUserCallback called for each request');
655 t.eq(resp.length, 5, 'commit returns array with correct length');
661 function test_callUserCallback(t) {
664 var protocol = new OpenLayers.Protocol.HTTP();
666 var scope = {'fake': 'scope'};
668 // test commit callback
672 callback: function() {
678 var resp = {requestType: 'foo'};
679 protocol.callUserCallback(resp, options);
680 t.ok(log.scope, scope, 'correct callback called with correct scope');
684 function test_options(t) {
689 // test that read with no options uses protocol options - 5 tests
694 var protocol = new OpenLayers.Protocol.HTTP({
695 format: new OpenLayers.Format({
702 callback: function(resp) {
703 log1.callbackCalled = true;
704 log1.callbackScope = this;
705 log1.request = resp && resp.priv;
706 log1.requestType = resp && resp.requestType;
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");
720 // test that commit with no options uses protocol options - 2 tests
721 var log2 = {called: 0};
722 protocol.options.callback = function() {
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"}
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.");