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_read_bbox(t) {
111
var protocol = new OpenLayers.Protocol.HTTP();
113
// fake XHR request object
114
var request = {'status': 200};
116
var _get = OpenLayers.Request.GET;
118
var bounds = new OpenLayers.Bounds(1, 2, 3, 4);
119
var filter = new OpenLayers.Filter.Spatial({
120
type: OpenLayers.Filter.Spatial.BBOX,
125
OpenLayers.Request.GET = function(options) {
126
t.eq(options.params['bbox'].toString(), bounds.toArray().toString(),
127
'GET called with bbox filter in params');
131
var resp = protocol.read({filter: filter});
133
OpenLayers.Request.GET = _get;
136
function test_parseFeatures(t) {
139
var protocol = new OpenLayers.Protocol.HTTP();
141
// test responseXML - 2 tests
144
'documentElement': 'xml'
148
'read': function(doc) {
149
t.eq(doc.documentElement, 'xml',
150
'format.read called with correct doc');
151
return doc.documentElement;
154
var ret = protocol.parseFeatures(request);
155
t.eq(ret, 'xml', 'parseFeatures returns expected value');
157
// test responseText - 2 tests
159
'responseText': 'text'
162
'read': function(doc) {
164
'format.read called with correct doc');
168
var ret = protocol.parseFeatures(request);
169
t.eq(ret, 'text', 'parseFeatures returns expected value');
171
// test empty responseText - 1 test
176
'read': function(doc) {
177
t.fail('format.read should not be called');
180
var ret = protocol.parseFeatures(request);
181
t.eq(ret, null, 'parseFeatures returns expected value');
184
function test_create(t) {
186
var protocol = new OpenLayers.Protocol.HTTP({
188
'format': {'write': function() {}}
191
// fake XHR request object
192
var request = {'status': 200};
194
// features to pass to create
195
var features = ['feature'];
197
// options to pass to create
198
var createOptions = {
200
'headers': {'k': 'bar_header'},
201
'scope': {'hello': 'world'},
202
'callback': function() {}
207
protocol.handleCreate = function(resp, opt) {
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');
219
'handleCreate called with correct request');
224
var _post = OpenLayers.Request.POST;
226
OpenLayers.Request.POST = function(options) {
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');
243
OpenLayers.Request.POST = _post;
248
var resp = protocol.create(features, createOptions);
250
OpenLayers.Request.POST = _post;
253
function test_update(t) {
255
var protocol = new OpenLayers.Protocol.HTTP({
257
'format': {'write': function() {}}
260
// fake XHR request object
261
var request = {'status': 200};
263
// feature to pass to update
264
var feature = {'feature':'feature'};
266
// options to pass to update
267
var updateOptions = {
269
'headers': {'k': 'bar_header'},
270
'scope': {'hello': 'world'},
271
'callback': function() {}
276
protocol.handleUpdate = function(resp, opt) {
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');
288
'handleUpdate called with correct request');
293
var _put = OpenLayers.Request.PUT;
295
OpenLayers.Request.PUT = function(options) {
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');
312
OpenLayers.Request.PUT = _put;
317
var resp = protocol.update(feature, updateOptions);
319
OpenLayers.Request.PUT = _put;
322
function test_handleResponse(t) {
325
var protocol = new OpenLayers.Protocol.HTTP();
327
var options, response, request, features;
329
// test options - 2 tests
330
var scope = {'fake': 'scope'};
333
'callback': function(resp) {
335
'[no status] callback called with correct scope');
336
t.ok(resp == response,
337
'[no status] callback called with correct response');
340
response = {priv: {}};
341
protocol.handleResponse(response, options);
343
// test failure condition - 1 test
345
'callback': function(resp) {
346
t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE,
347
'[status 400] callback called with correct response code');
350
response = {priv: {status: 400}};
351
protocol.handleResponse(response, options);
353
// test success condition - 3 tests
354
features = {'fake': 'features'};
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');
363
response = {priv: {status: 200}};
364
protocol.parseFeatures = function(request) {
365
t.ok(request == response.priv,
366
'[status 200] parseFeatures called with correct request');
369
protocol.handleResponse(response, options);
375
function test_delete(t) {
377
var protocol = new OpenLayers.Protocol.HTTP({
381
// fake XHR request object
382
var request = {'status': 200};
384
// feature to pass to delete
385
var feature = {'url': 'bar_url'};
387
// options to pass to delete
388
var deleteOptions = {
390
'headers': {'k': 'bar_header'},
391
'scope': {'hello': 'world'},
392
'callback': function() {}
397
protocol.handleDelete = function(resp, opt) {
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');
409
'handleDelete called with correct request');
414
var _delete = OpenLayers.Request.DELETE;
416
OpenLayers.Request.DELETE = function(options) {
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');
433
OpenLayers.Request.DELETE = _delete;
438
var resp = protocol['delete'](feature, deleteOptions);
440
OpenLayers.Request.DELETE = _delete;
443
function test_handleDelete(t) {
446
var protocol = new OpenLayers.Protocol.HTTP();
448
var options, response, request, features;
450
// test options - 2 tests
451
var scope = {'fake': 'scope'};
454
'callback': function(resp) {
456
'callback called with correct scope');
457
t.ok(resp == response,
458
'callback called with correct response');
461
response = {priv: {}};
462
protocol.handleDelete(response, options);
464
// test failure condition - 1 test
466
'callback': function(resp) {
467
t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE,
468
'callback called with correct response code');
471
response = {priv: {status: 400}};
472
protocol.handleDelete(response, options);
474
// test success condition - 1 test
476
'callback': function(resp) {
477
t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS,
478
'callback called with correct response code');
481
response = {priv: {status: 200}};
482
protocol.handleDelete(response, options);
488
function test_commit(t) {
491
var protocol = new OpenLayers.Protocol.HTTP();
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}
505
'callback': function(resp) {
509
'callback': function(resp) {
513
'callback': function(resp) {
518
var respCreate = new OpenLayers.Protocol.Response();
519
var respUpdate = new OpenLayers.Protocol.Response();
520
var respDelete = new OpenLayers.Protocol.Response();
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);
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);
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.callback.call(options.scope, respDelete);
553
protocol.callUserCallback = function(resp, opt) {
555
'callUserCallback called with correction options map');
559
var resp = protocol.commit(features, options);
562
t.eq(count, 5, 'callUserCallback called for each request');
563
t.eq(resp.length, 5, 'commit returns array with correct length');
569
function test_callUserCallback(t) {
572
var protocol = new OpenLayers.Protocol.HTTP();
575
var scope = {'fake': 'scope'};
577
// test commit callback
580
'callback': function() {
581
t.ok(this == scope, 'callback called with correct scope');
585
resp = {'requestType': 'create', 'last': true};
586
protocol.callUserCallback(resp, options);
588
resp = {'requestType': 'create', 'last': false};
589
protocol.callUserCallback(resp, options);
591
// test create callback
595
'callback': function(r) {
596
t.ok(this == scope, 'callback called with correct scope');
597
t.ok(r == resp, 'callback called with correct response');
602
resp = {'requestType': 'create'};
603
protocol.callUserCallback(resp, options);
605
// test with both callbacks set
609
'callback': function(r) {
610
t.ok(this == scope, 'callback called with correct scope');
611
t.ok(r == resp, 'callback called with correct response');
615
'callback': function() {
616
t.ok(this == scope, 'callback called with correct scope');
620
resp = {'requestType': 'create', 'last': true};
621
protocol.callUserCallback(resp, options);
627
'callback': function(resp) {
628
t.fail('callback should not get called');
632
resp = {'requestType': 'create'};
633
protocol.callUserCallback(resp, options);
639
function test_options(t) {
642
var _R = OpenLayers.Protocol.Response;
643
OpenLayers.Protocol.Response = function() {
644
this.priv = {status: 200};
647
// test that read with no options uses protocol options - 5 tests
652
var protocol = new OpenLayers.Protocol.HTTP({
653
format: new OpenLayers.Format({read: function(){}, write: function(){}}),
657
callback: function() {
658
t.ok(true, "[read] Correct callback.");
659
t.eq(this, scope, "[read] Correct scope.");
663
var _issue = OpenLayers.Request.issue;
664
OpenLayers.Request.issue = function(config) {
665
t.eq(config.url, url, "[" + config.method + "] Correct url.");
666
t.eq(config.headers, headers, "[" + config.method + "] Correct headers.");
667
t.eq(config.params, params, "[" + config.method + "] Correct params.");
668
config.callback.call(config.scope);
671
OpenLayers.Request.issue = _issue;
673
// test that commit with no options uses protocol options - 2 tests
674
_issue = OpenLayers.Request.issue;
675
OpenLayers.Request.issue = function(config) {
676
config.callback.call(config.scope);
679
protocol.options.callback = function() {
681
t.eq(this, scope, "[commit] Correct scope.");
684
{state: OpenLayers.State.INSERT},
685
{state: OpenLayers.State.INSERT},
686
{state: OpenLayers.State.UPDATE},
687
{state: OpenLayers.State.UPDATE},
688
{state: OpenLayers.State.DELETE},
689
{state: OpenLayers.State.DELETE}
691
t.eq(called, 1, "[commit] Callback called once.");
695
OpenLayers.Protocol.Response = _R;