| | 1 | <html> |
|---|
| | 2 | <head> |
|---|
| | 3 | <script src="../../lib/OpenLayers.js"></script> |
|---|
| | 4 | <script type="text/javascript"> |
|---|
| | 5 | |
|---|
| | 6 | function test_Protocol_HTTP_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_Protocol_HTTP_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_Protocol_HTTP_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.callbackReadCreateUpdate = function(opt, resp, req) { |
|---|
| | 65 | // 4 tests |
|---|
| | 66 | t.ok(this == protocol, |
|---|
| | 67 | 'callbackReadCreateUpdate called with correct scope'); |
|---|
| | 68 | t.ok(opt == readOptions, |
|---|
| | 69 | 'callbackReadCreateUpdate called with correct options'); |
|---|
| | 70 | t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', |
|---|
| | 71 | 'callbackReadCreateUpdate called with a Response object'); |
|---|
| | 72 | t.eq(req, request, |
|---|
| | 73 | 'callbackReadCreateUpdate called with correct request'); |
|---|
| | 74 | |
|---|
| | 75 | response = resp; |
|---|
| | 76 | }; |
|---|
| | 77 | |
|---|
| | 78 | var _get = OpenLayers.Request.GET; |
|---|
| | 79 | |
|---|
| | 80 | OpenLayers.Request.GET = function(options) { |
|---|
| | 81 | // 5 tests |
|---|
| | 82 | t.eq(options.url, readOptions.url, |
|---|
| | 83 | 'GET called with correct url in options'); |
|---|
| | 84 | t.eq(options.params['k'], readOptions.params['k'], |
|---|
| | 85 | 'GET called with correct params in options'); |
|---|
| | 86 | t.eq(options.headers['k'], readOptions.headers['k'], |
|---|
| | 87 | 'GET called with correct headers in options'); |
|---|
| | 88 | t.eq(options.scope, undefined, |
|---|
| | 89 | 'GET called with correct scope in options'); |
|---|
| | 90 | t.ok(typeof options.callback == 'function', |
|---|
| | 91 | 'GET called with a callback in options'); |
|---|
| | 92 | options.callback(request); |
|---|
| | 93 | }; |
|---|
| | 94 | |
|---|
| | 95 | var resp = protocol.read(readOptions); |
|---|
| | 96 | t.ok(resp == response, |
|---|
| | 97 | 'read returns the expected response object'); |
|---|
| | 98 | |
|---|
| | 99 | // cleanup |
|---|
| | 100 | protocol.destroy(); |
|---|
| | 101 | OpenLayers.Request.GET = _get; |
|---|
| | 102 | } |
|---|
| | 103 | |
|---|
| | 104 | function test_Protocol_HTTP_readResponse(t) { |
|---|
| | 105 | t.plan(5); |
|---|
| | 106 | |
|---|
| | 107 | var protocol = new OpenLayers.Protocol.HTTP(); |
|---|
| | 108 | |
|---|
| | 109 | // test responseXML - 2 tests |
|---|
| | 110 | var request = { |
|---|
| | 111 | 'responseXML': { |
|---|
| | 112 | 'documentElement': 'xml' |
|---|
| | 113 | } |
|---|
| | 114 | }; |
|---|
| | 115 | protocol.format = { |
|---|
| | 116 | 'read': function(doc) { |
|---|
| | 117 | t.eq(doc.documentElement, 'xml', |
|---|
| | 118 | 'format.read called with correct doc'); |
|---|
| | 119 | return doc.documentElement; |
|---|
| | 120 | } |
|---|
| | 121 | }; |
|---|
| | 122 | var ret = protocol.readResponse(request); |
|---|
| | 123 | t.eq(ret, 'xml', 'readResponse returns expected value'); |
|---|
| | 124 | |
|---|
| | 125 | // test responseText - 2 tests |
|---|
| | 126 | var request = { |
|---|
| | 127 | 'responseText': 'text' |
|---|
| | 128 | }; |
|---|
| | 129 | protocol.format = { |
|---|
| | 130 | 'read': function(doc) { |
|---|
| | 131 | t.eq(doc, 'text', |
|---|
| | 132 | 'format.read called with correct doc'); |
|---|
| | 133 | return doc; |
|---|
| | 134 | } |
|---|
| | 135 | }; |
|---|
| | 136 | var ret = protocol.readResponse(request); |
|---|
| | 137 | t.eq(ret, 'text', 'readResponse returns expected value'); |
|---|
| | 138 | |
|---|
| | 139 | // test empty responseText - 1 test |
|---|
| | 140 | var request = { |
|---|
| | 141 | 'responseText': '' |
|---|
| | 142 | }; |
|---|
| | 143 | protocol.format = { |
|---|
| | 144 | 'read': function(doc) { |
|---|
| | 145 | t.fail('format.read should not be called'); |
|---|
| | 146 | } |
|---|
| | 147 | }; |
|---|
| | 148 | var ret = protocol.readResponse(request); |
|---|
| | 149 | t.eq(ret, null, 'readResponse returns expected value'); |
|---|
| | 150 | } |
|---|
| | 151 | |
|---|
| | 152 | function test_Protocol_HTTP_create(t) { |
|---|
| | 153 | t.plan(10); |
|---|
| | 154 | var protocol = new OpenLayers.Protocol.HTTP({ |
|---|
| | 155 | 'url': 'foo_url', |
|---|
| | 156 | 'format': {'write': function() {}} |
|---|
| | 157 | }); |
|---|
| | 158 | |
|---|
| | 159 | // fake XHR request object |
|---|
| | 160 | var request = {'status': 200}; |
|---|
| | 161 | |
|---|
| | 162 | // features to pass to create |
|---|
| | 163 | var features = ['feature']; |
|---|
| | 164 | |
|---|
| | 165 | // options to pass to create |
|---|
| | 166 | var createOptions = { |
|---|
| | 167 | 'url': 'bar_url', |
|---|
| | 168 | 'headers': {'k': 'bar_header'}, |
|---|
| | 169 | 'scope': {'hello': 'world'}, |
|---|
| | 170 | 'callback': function() {} |
|---|
| | 171 | }; |
|---|
| | 172 | |
|---|
| | 173 | var response; |
|---|
| | 174 | |
|---|
| | 175 | protocol.callbackCreate = function(opt, resp, req) { |
|---|
| | 176 | // 5 tests |
|---|
| | 177 | t.ok(this == protocol, |
|---|
| | 178 | 'callbackCreate called with correct scope'); |
|---|
| | 179 | t.ok(opt == createOptions, |
|---|
| | 180 | 'callbackCreate called with correct options'); |
|---|
| | 181 | t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', |
|---|
| | 182 | 'callbackCreate called with a Response object'); |
|---|
| | 183 | t.ok(resp.reqFeatures == features, |
|---|
| | 184 | 'callbackCreate called with correct requested features in response'); |
|---|
| | 185 | t.eq(req, request, |
|---|
| | 186 | 'callbackCreate called with correct request'); |
|---|
| | 187 | |
|---|
| | 188 | response = resp; |
|---|
| | 189 | }; |
|---|
| | 190 | |
|---|
| | 191 | var _post = OpenLayers.Request.POST; |
|---|
| | 192 | |
|---|
| | 193 | OpenLayers.Request.POST = function(options) { |
|---|
| | 194 | // 4 tests |
|---|
| | 195 | t.eq(options.url, createOptions.url, |
|---|
| | 196 | 'POST called with correct url in options'); |
|---|
| | 197 | t.eq(options.headers['k'], createOptions.headers['k'], |
|---|
| | 198 | 'POST called with correct headers in options'); |
|---|
| | 199 | t.eq(options.scope, undefined, |
|---|
| | 200 | 'POST called with correct scope in options'); |
|---|
| | 201 | t.ok(typeof options.callback == 'function', |
|---|
| | 202 | 'POST called with a callback in options'); |
|---|
| | 203 | // call callback |
|---|
| | 204 | options.callback(request); |
|---|
| | 205 | }; |
|---|
| | 206 | |
|---|
| | 207 | var resp = protocol.create(features, createOptions); |
|---|
| | 208 | t.ok(resp == response, |
|---|
| | 209 | 'read returns the expected response object'); |
|---|
| | 210 | |
|---|
| | 211 | // cleanup |
|---|
| | 212 | protocol.destroy(); |
|---|
| | 213 | OpenLayers.Request.POST = _post; |
|---|
| | 214 | } |
|---|
| | 215 | |
|---|
| | 216 | function test_Protocol_HTTP_update(t) { |
|---|
| | 217 | t.plan(10); |
|---|
| | 218 | var protocol = new OpenLayers.Protocol.HTTP({ |
|---|
| | 219 | 'url': 'foo_url', |
|---|
| | 220 | 'format': {'write': function() {}} |
|---|
| | 221 | }); |
|---|
| | 222 | |
|---|
| | 223 | // fake XHR request object |
|---|
| | 224 | var request = {'status': 200}; |
|---|
| | 225 | |
|---|
| | 226 | // feature to pass to update |
|---|
| | 227 | var feature = {'feature':'feature'}; |
|---|
| | 228 | |
|---|
| | 229 | // options to pass to update |
|---|
| | 230 | var updateOptions = { |
|---|
| | 231 | 'url': 'bar_url', |
|---|
| | 232 | 'headers': {'k': 'bar_header'}, |
|---|
| | 233 | 'scope': {'hello': 'world'}, |
|---|
| | 234 | 'callback': function() {} |
|---|
| | 235 | }; |
|---|
| | 236 | |
|---|
| | 237 | var response; |
|---|
| | 238 | |
|---|
| | 239 | protocol.callbackUpdate = function(opt, resp, req) { |
|---|
| | 240 | // 5 tests |
|---|
| | 241 | t.ok(this == protocol, |
|---|
| | 242 | 'callbackUpdate called with correct scope'); |
|---|
| | 243 | t.ok(opt == updateOptions, |
|---|
| | 244 | 'callbackUpdate called with correct options'); |
|---|
| | 245 | t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', |
|---|
| | 246 | 'callbackUpdate called with a Response object'); |
|---|
| | 247 | t.ok(resp.reqFeatures == feature, |
|---|
| | 248 | 'callbackUpdate called with correct requested feature in response'); |
|---|
| | 249 | t.eq(req, request, |
|---|
| | 250 | 'callbackUpdate called with correct request'); |
|---|
| | 251 | |
|---|
| | 252 | response = resp; |
|---|
| | 253 | }; |
|---|
| | 254 | |
|---|
| | 255 | var _put = OpenLayers.Request.PUT; |
|---|
| | 256 | |
|---|
| | 257 | OpenLayers.Request.PUT = function(options) { |
|---|
| | 258 | // 4 tests |
|---|
| | 259 | t.eq(options.url, updateOptions.url, |
|---|
| | 260 | 'PUT called with correct url in options'); |
|---|
| | 261 | t.eq(options.headers['k'], updateOptions.headers['k'], |
|---|
| | 262 | 'PUT called with correct headers in options'); |
|---|
| | 263 | t.eq(options.scope, undefined, |
|---|
| | 264 | 'PUT called with correct scope in options'); |
|---|
| | 265 | t.ok(typeof options.callback == 'function', |
|---|
| | 266 | 'PUT called with a callback in options'); |
|---|
| | 267 | // call callback |
|---|
| | 268 | options.callback(request); |
|---|
| | 269 | }; |
|---|
| | 270 | |
|---|
| | 271 | var resp = protocol.update(feature, updateOptions); |
|---|
| | 272 | t.ok(resp == response, |
|---|
| | 273 | 'read returns the expected response object'); |
|---|
| | 274 | |
|---|
| | 275 | // cleanup |
|---|
| | 276 | protocol.destroy(); |
|---|
| | 277 | OpenLayers.Request.PUT = _put; |
|---|
| | 278 | } |
|---|
| | 279 | function test_Protocol_HTTP_callbackReadCreateUpdate(t) { |
|---|
| | 280 | t.plan(6); |
|---|
| | 281 | |
|---|
| | 282 | var protocol = new OpenLayers.Protocol.HTTP(); |
|---|
| | 283 | |
|---|
| | 284 | var options, response, request, features; |
|---|
| | 285 | |
|---|
| | 286 | // test options - 2 tests |
|---|
| | 287 | var scope = {'fake': 'scope'}; |
|---|
| | 288 | options = { |
|---|
| | 289 | 'scope': scope, |
|---|
| | 290 | 'callback': function(resp) { |
|---|
| | 291 | t.ok(this == scope, |
|---|
| | 292 | 'callback called with correct scope'); |
|---|
| | 293 | t.ok(resp == response, |
|---|
| | 294 | 'callback called with correct response'); |
|---|
| | 295 | } |
|---|
| | 296 | }; |
|---|
| | 297 | response = {'fake': 'reponse'}; |
|---|
| | 298 | request = {'fake': 'request'}; |
|---|
| | 299 | protocol.callbackReadCreateUpdate(options, response, request); |
|---|
| | 300 | |
|---|
| | 301 | // test failure condition - 1 test |
|---|
| | 302 | options = { |
|---|
| | 303 | 'callback': function(resp) { |
|---|
| | 304 | t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE, |
|---|
| | 305 | 'callback called with correct response code'); |
|---|
| | 306 | } |
|---|
| | 307 | }; |
|---|
| | 308 | response = {'fake': 'reponse'}; |
|---|
| | 309 | request = {'status': 400}; |
|---|
| | 310 | protocol.callbackReadCreateUpdate(options, response, request); |
|---|
| | 311 | |
|---|
| | 312 | // test success condition - 3 tests |
|---|
| | 313 | features = {'fake': 'features'}; |
|---|
| | 314 | options = { |
|---|
| | 315 | 'callback': function(resp) { |
|---|
| | 316 | t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS, |
|---|
| | 317 | 'callback called with correct response code'); |
|---|
| | 318 | t.ok(resp.features, features, |
|---|
| | 319 | 'callback called with correct features in response'); |
|---|
| | 320 | } |
|---|
| | 321 | }; |
|---|
| | 322 | response = {'fake': 'reponse'}; |
|---|
| | 323 | request = {'status': 200}; |
|---|
| | 324 | protocol.readResponse = function(req) { |
|---|
| | 325 | t.ok(req == req, |
|---|
| | 326 | 'readResponse called with correct request'); |
|---|
| | 327 | return features; |
|---|
| | 328 | } |
|---|
| | 329 | protocol.callbackReadCreateUpdate(options, response, request); |
|---|
| | 330 | |
|---|
| | 331 | // cleanup |
|---|
| | 332 | protocol.destroy(); |
|---|
| | 333 | } |
|---|
| | 334 | |
|---|
| | 335 | function test_Protocol_HTTP_delete(t) { |
|---|
| | 336 | t.plan(10); |
|---|
| | 337 | var protocol = new OpenLayers.Protocol.HTTP({ |
|---|
| | 338 | 'url': 'foo_url' |
|---|
| | 339 | }); |
|---|
| | 340 | |
|---|
| | 341 | // fake XHR request object |
|---|
| | 342 | var request = {'status': 200}; |
|---|
| | 343 | |
|---|
| | 344 | // feature to pass to delete |
|---|
| | 345 | var feature = {'url': 'bar_url'}; |
|---|
| | 346 | |
|---|
| | 347 | // options to pass to delete |
|---|
| | 348 | var deleteOptions = { |
|---|
| | 349 | 'url': 'bar_url', |
|---|
| | 350 | 'headers': {'k': 'bar_header'}, |
|---|
| | 351 | 'scope': {'hello': 'world'}, |
|---|
| | 352 | 'callback': function() {} |
|---|
| | 353 | }; |
|---|
| | 354 | |
|---|
| | 355 | var response; |
|---|
| | 356 | |
|---|
| | 357 | protocol.callbackDelete = function(opt, resp, req) { |
|---|
| | 358 | // 5 tests |
|---|
| | 359 | t.ok(this == protocol, |
|---|
| | 360 | 'callbackDelete called with correct scope'); |
|---|
| | 361 | t.ok(opt == deleteOptions, |
|---|
| | 362 | 'callbackDelete called with correct options'); |
|---|
| | 363 | t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', |
|---|
| | 364 | 'callbackDelete called with a Response object'); |
|---|
| | 365 | t.ok(resp.reqFeatures == feature, |
|---|
| | 366 | 'callbackDelete called with correct requested feature in response'); |
|---|
| | 367 | t.eq(req, request, |
|---|
| | 368 | 'callbackDelete called with correct request'); |
|---|
| | 369 | |
|---|
| | 370 | response = resp; |
|---|
| | 371 | }; |
|---|
| | 372 | |
|---|
| | 373 | var _delete = OpenLayers.Request.DELETE; |
|---|
| | 374 | |
|---|
| | 375 | OpenLayers.Request.DELETE = function(options) { |
|---|
| | 376 | // 4 tests |
|---|
| | 377 | t.eq(options.url, deleteOptions.url, |
|---|
| | 378 | 'DELETE called with correct url in options'); |
|---|
| | 379 | t.eq(options.headers['k'], deleteOptions.headers['k'], |
|---|
| | 380 | 'DELETE called with correct headers in options'); |
|---|
| | 381 | t.eq(options.scope, undefined, |
|---|
| | 382 | 'DELETE called with correct scope in options'); |
|---|
| | 383 | t.ok(typeof options.callback == 'function', |
|---|
| | 384 | 'DELETE called with a callback in options'); |
|---|
| | 385 | // call callback |
|---|
| | 386 | options.callback(request); |
|---|
| | 387 | }; |
|---|
| | 388 | |
|---|
| | 389 | var resp = protocol['delete'](feature, deleteOptions); |
|---|
| | 390 | t.ok(resp == response, |
|---|
| | 391 | 'read returns the expected response object'); |
|---|
| | 392 | |
|---|
| | 393 | // cleanup |
|---|
| | 394 | protocol.destroy(); |
|---|
| | 395 | OpenLayers.Request.DELETE = _delete; |
|---|
| | 396 | } |
|---|
| | 397 | |
|---|
| | 398 | function test_Protocol_HTTP_callbackDelete(t) { |
|---|
| | 399 | t.plan(4); |
|---|
| | 400 | |
|---|
| | 401 | var protocol = new OpenLayers.Protocol.HTTP(); |
|---|
| | 402 | |
|---|
| | 403 | var options, response, request, features; |
|---|
| | 404 | |
|---|
| | 405 | // test options - 2 tests |
|---|
| | 406 | var scope = {'fake': 'scope'}; |
|---|
| | 407 | options = { |
|---|
| | 408 | 'scope': scope, |
|---|
| | 409 | 'callback': function(resp) { |
|---|
| | 410 | t.ok(this == scope, |
|---|
| | 411 | 'callback called with correct scope'); |
|---|
| | 412 | t.ok(resp == response, |
|---|
| | 413 | 'callback called with correct response'); |
|---|
| | 414 | } |
|---|
| | 415 | }; |
|---|
| | 416 | response = {'fake': 'reponse'}; |
|---|
| | 417 | request = {'fake': 'request'}; |
|---|
| | 418 | protocol.callbackDelete(options, response, request); |
|---|
| | 419 | |
|---|
| | 420 | // test failure condition - 1 test |
|---|
| | 421 | options = { |
|---|
| | 422 | 'callback': function(resp) { |
|---|
| | 423 | t.eq(resp.code, OpenLayers.Protocol.Response.FAILURE, |
|---|
| | 424 | 'callback called with correct response code'); |
|---|
| | 425 | } |
|---|
| | 426 | }; |
|---|
| | 427 | response = {'fake': 'reponse'}; |
|---|
| | 428 | request = {'status': 400}; |
|---|
| | 429 | protocol.callbackDelete(options, response, request); |
|---|
| | 430 | |
|---|
| | 431 | // test success condition - 1 test |
|---|
| | 432 | options = { |
|---|
| | 433 | 'callback': function(resp) { |
|---|
| | 434 | t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS, |
|---|
| | 435 | 'callback called with correct response code'); |
|---|
| | 436 | } |
|---|
| | 437 | }; |
|---|
| | 438 | response = {'fake': 'reponse'}; |
|---|
| | 439 | request = {'status': 200}; |
|---|
| | 440 | protocol.callbackDelete(options, response, request); |
|---|
| | 441 | |
|---|
| | 442 | // cleanup |
|---|
| | 443 | protocol.destroy(); |
|---|
| | 444 | } |
|---|
| | 445 | |
|---|
| | 446 | function test_Protocol_HTTP_commit(t) { |
|---|
| | 447 | t.plan(17); |
|---|
| | 448 | |
|---|
| | 449 | var protocol = new OpenLayers.Protocol.HTTP(); |
|---|
| | 450 | |
|---|
| | 451 | // 6 features |
|---|
| | 452 | var features = [ |
|---|
| | 453 | {'state': OpenLayers.State.INSERT}, |
|---|
| | 454 | {'state': OpenLayers.State.INSERT}, |
|---|
| | 455 | {'state': OpenLayers.State.UPDATE}, |
|---|
| | 456 | {'state': OpenLayers.State.UPDATE}, |
|---|
| | 457 | {'state': OpenLayers.State.DELETE}, |
|---|
| | 458 | {'state': OpenLayers.State.DELETE} |
|---|
| | 459 | ]; |
|---|
| | 460 | |
|---|
| | 461 | var options = { |
|---|
| | 462 | 'create': { |
|---|
| | 463 | 'callback': function(resp) { |
|---|
| | 464 | } |
|---|
| | 465 | }, |
|---|
| | 466 | 'update': { |
|---|
| | 467 | 'callback': function(resp) { |
|---|
| | 468 | } |
|---|
| | 469 | }, |
|---|
| | 470 | 'delete': { |
|---|
| | 471 | 'callback': function(resp) { |
|---|
| | 472 | } |
|---|
| | 473 | } |
|---|
| | 474 | }; |
|---|
| | 475 | |
|---|
| | 476 | var respCreate = new OpenLayers.Protocol.Response(); |
|---|
| | 477 | var respUpdate = new OpenLayers.Protocol.Response(); |
|---|
| | 478 | var respDelete = new OpenLayers.Protocol.Response(); |
|---|
| | 479 | |
|---|
| | 480 | // 2 tests |
|---|
| | 481 | protocol['create'] = function(feature, options) { |
|---|
| | 482 | t.ok(options.scope == protocol, |
|---|
| | 483 | 'create called with correct scope'); |
|---|
| | 484 | t.ok(typeof options.callback == 'function', |
|---|
| | 485 | 'create called with a callback in options'); |
|---|
| | 486 | options.callback.call(options.scope, respCreate); |
|---|
| | 487 | return respCreate; |
|---|
| | 488 | }; |
|---|
| | 489 | // 4 tests |
|---|
| | 490 | protocol['update'] = function(feature, options) { |
|---|
| | 491 | t.ok(options.scope == protocol, |
|---|
| | 492 | 'update called with correct scope'); |
|---|
| | 493 | t.ok(typeof options.callback == 'function', |
|---|
| | 494 | 'update called with a callback in options'); |
|---|
| | 495 | options.callback.call(options.scope, respUpdate); |
|---|
| | 496 | return respUpdate; |
|---|
| | 497 | }; |
|---|
| | 498 | // 4 tests |
|---|
| | 499 | protocol['delete'] = function(feature, options) { |
|---|
| | 500 | t.ok(options.scope == protocol, |
|---|
| | 501 | 'delete called with correct scope'); |
|---|
| | 502 | t.ok(typeof options.callback == 'function', |
|---|
| | 503 | 'delete called with a callback in options'); |
|---|
| | 504 | options.callback.call(options.scope, respDelete); |
|---|
| | 505 | return respDelete; |
|---|
| | 506 | }; |
|---|
| | 507 | |
|---|
| | 508 | var count = 0; |
|---|
| | 509 | |
|---|
| | 510 | // 5 tests |
|---|
| | 511 | protocol.callUserCallback = function(opt, resp) { |
|---|
| | 512 | t.ok(opt == options, |
|---|
| | 513 | 'callUserCallback called with correction options map'); |
|---|
| | 514 | count++; |
|---|
| | 515 | }; |
|---|
| | 516 | |
|---|
| | 517 | var resp = protocol.commit(features, options); |
|---|
| | 518 | |
|---|
| | 519 | // 2 tests |
|---|
| | 520 | t.eq(count, 5, 'callUserCallback called for each request'); |
|---|
| | 521 | t.eq(resp.length, 5, 'commit returns array with correct length'); |
|---|
| | 522 | |
|---|
| | 523 | // cleanup |
|---|
| | 524 | protocol.destroy(); |
|---|
| | 525 | } |
|---|
| | 526 | |
|---|
| | 527 | function test_Protocol_HTTP_callUserCallback(t) { |
|---|
| | 528 | t.plan(6); |
|---|
| | 529 | |
|---|
| | 530 | var protocol = new OpenLayers.Protocol.HTTP(); |
|---|
| | 531 | |
|---|
| | 532 | var options, resp; |
|---|
| | 533 | var scope = {'fake': 'scope'}; |
|---|
| | 534 | |
|---|
| | 535 | // test commit callback |
|---|
| | 536 | // 1 tests |
|---|
| | 537 | options = { |
|---|
| | 538 | 'callback': function() { |
|---|
| | 539 | t.ok(this == scope, 'callback called with correct scope'); |
|---|
| | 540 | }, |
|---|
| | 541 | 'scope': scope |
|---|
| | 542 | }; |
|---|
| | 543 | resp = {'requestType': 'create', 'last': true}; |
|---|
| | 544 | protocol.callUserCallback(options, resp); |
|---|
| | 545 | // 0 test |
|---|
| | 546 | resp = {'requestType': 'create', 'last': false}; |
|---|
| | 547 | protocol.callUserCallback(options, resp); |
|---|
| | 548 | |
|---|
| | 549 | // test create callback |
|---|
| | 550 | // 2 tests |
|---|
| | 551 | options = { |
|---|
| | 552 | 'create': { |
|---|
| | 553 | 'callback': function(r) { |
|---|
| | 554 | t.ok(this == scope, 'callback called with correct scope'); |
|---|
| | 555 | t.ok(r == resp, 'callback called with correct response'); |
|---|
| | 556 | }, |
|---|
| | 557 | 'scope': scope |
|---|
| | 558 | } |
|---|
| | 559 | }; |
|---|
| | 560 | resp = {'requestType': 'create'}; |
|---|
| | 561 | protocol.callUserCallback(options, resp); |
|---|
| | 562 | |
|---|
| | 563 | // test with both callbacks set |
|---|
| | 564 | // 3 tests |
|---|
| | 565 | options = { |
|---|
| | 566 | 'create': { |
|---|
| | 567 | 'callback': function(r) { |
|---|
| | 568 | t.ok(this == scope, 'callback called with correct scope'); |
|---|
| | 569 | t.ok(r == resp, 'callback called with correct response'); |
|---|
| | 570 | }, |
|---|
| | 571 | 'scope': scope |
|---|
| | 572 | }, |
|---|
| | 573 | 'callback': function() { |
|---|
| | 574 | t.ok(this == scope, 'callback called with correct scope'); |
|---|
| | 575 | }, |
|---|
| | 576 | 'scope': scope |
|---|
| | 577 | }; |
|---|
| | 578 | resp = {'requestType': 'create', 'last': true}; |
|---|
| | 579 | protocol.callUserCallback(options, resp); |
|---|
| | 580 | |
|---|
| | 581 | // no callback set |
|---|
| | 582 | // 0 test |
|---|
| | 583 | options = { |
|---|
| | 584 | 'delete': { |
|---|
| | 585 | 'callback': function(resp) { |
|---|
| | 586 | t.fail('callback should not get called'); |
|---|
| | 587 | } |
|---|
| | 588 | } |
|---|
| | 589 | }; |
|---|
| | 590 | resp = {'requestType': 'create'}; |
|---|
| | 591 | protocol.callUserCallback(options, resp); |
|---|
| | 592 | |
|---|
| | 593 | // cleanup |
|---|
| | 594 | protocol.destroy(); |
|---|
| | 595 | } |
|---|
| | 596 | </script> |
|---|
| | 597 | </head> |
|---|
| | 598 | <body> |
|---|
| | 599 | <div id="map" style="width:512px; height:256px"> </div> |
|---|
| | 600 | </body> |
|---|
| | 601 | </html> |