Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Cu.import("resource://services-common/storageservice.js"); |
michael@0 | 5 | Cu.import("resource://testing-common/services-common/storageserver.js"); |
michael@0 | 6 | |
michael@0 | 7 | function run_test() { |
michael@0 | 8 | initTestLogging("Trace"); |
michael@0 | 9 | |
michael@0 | 10 | run_next_test(); |
michael@0 | 11 | } |
michael@0 | 12 | |
michael@0 | 13 | function getRandomUser() { |
michael@0 | 14 | return "" + (Math.floor(Math.random() * 100000) + 1); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | function getEmptyServer(user=getRandomUser(), password="password") { |
michael@0 | 18 | let users = {}; |
michael@0 | 19 | users[user] = password; |
michael@0 | 20 | |
michael@0 | 21 | return storageServerForUsers(users, { |
michael@0 | 22 | meta: {}, |
michael@0 | 23 | clients: {}, |
michael@0 | 24 | crypto: {}, |
michael@0 | 25 | }); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | function getClient(server, user=getRandomUser(), password="password") { |
michael@0 | 29 | let identity = server.server.identity; |
michael@0 | 30 | let url = identity.primaryScheme + "://" + identity.primaryHost + ":" + |
michael@0 | 31 | identity.primaryPort + "/2.0/" + user; |
michael@0 | 32 | let client = new StorageServiceClient(url); |
michael@0 | 33 | client.addListener({ |
michael@0 | 34 | onDispatch: function onDispatch(request) { |
michael@0 | 35 | let up = user + ":" + password; |
michael@0 | 36 | request.request.setHeader("authorization", "Basic " + btoa(up)); |
michael@0 | 37 | } |
michael@0 | 38 | }); |
michael@0 | 39 | |
michael@0 | 40 | return client; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | function getServerAndClient(user=getRandomUser(), password="password") { |
michael@0 | 44 | let server = getEmptyServer(user, password); |
michael@0 | 45 | let client = getClient(server, user, password); |
michael@0 | 46 | |
michael@0 | 47 | return [server, client, user, password]; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | add_test(function test_auth_failure_listener() { |
michael@0 | 51 | _("Ensure the onAuthFailure listener is invoked."); |
michael@0 | 52 | |
michael@0 | 53 | let server = getEmptyServer(); |
michael@0 | 54 | let client = getClient(server, "324", "INVALID"); |
michael@0 | 55 | client.addListener({ |
michael@0 | 56 | onAuthFailure: function onAuthFailure(client, request) { |
michael@0 | 57 | _("onAuthFailure"); |
michael@0 | 58 | server.stop(run_next_test); |
michael@0 | 59 | } |
michael@0 | 60 | }); |
michael@0 | 61 | |
michael@0 | 62 | let request = client.getCollectionInfo(); |
michael@0 | 63 | request.dispatch(); |
michael@0 | 64 | }); |
michael@0 | 65 | |
michael@0 | 66 | add_test(function test_duplicate_listeners() { |
michael@0 | 67 | _("Ensure that duplicate listeners aren't installed multiple times."); |
michael@0 | 68 | |
michael@0 | 69 | let server = getEmptyServer(); |
michael@0 | 70 | let client = getClient(server, "1234567", "BAD_PASSWORD"); |
michael@0 | 71 | |
michael@0 | 72 | let invokeCount = 0; |
michael@0 | 73 | let listener = { |
michael@0 | 74 | onAuthFailure: function onAuthFailure() { |
michael@0 | 75 | invokeCount++; |
michael@0 | 76 | } |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | client.addListener(listener); |
michael@0 | 80 | // No error expected. |
michael@0 | 81 | client.addListener(listener); |
michael@0 | 82 | |
michael@0 | 83 | let request = client.getCollectionInfo(); |
michael@0 | 84 | request.dispatch(function onComplete() { |
michael@0 | 85 | do_check_eq(invokeCount, 1); |
michael@0 | 86 | |
michael@0 | 87 | server.stop(run_next_test); |
michael@0 | 88 | }); |
michael@0 | 89 | }); |
michael@0 | 90 | |
michael@0 | 91 | add_test(function test_handler_object() { |
michael@0 | 92 | _("Ensure that installed handlers get their callbacks invoked."); |
michael@0 | 93 | |
michael@0 | 94 | let [server, client] = getServerAndClient(); |
michael@0 | 95 | |
michael@0 | 96 | let onCompleteCount = 0; |
michael@0 | 97 | let onDispatchCount = 0; |
michael@0 | 98 | |
michael@0 | 99 | let handler = { |
michael@0 | 100 | onComplete: function onComplete() { |
michael@0 | 101 | onCompleteCount++; |
michael@0 | 102 | |
michael@0 | 103 | do_check_eq(onDispatchCount, 1); |
michael@0 | 104 | do_check_eq(onCompleteCount, 1); |
michael@0 | 105 | |
michael@0 | 106 | server.stop(run_next_test); |
michael@0 | 107 | }, |
michael@0 | 108 | |
michael@0 | 109 | onDispatch: function onDispatch() { |
michael@0 | 110 | onDispatchCount++; |
michael@0 | 111 | }, |
michael@0 | 112 | }; |
michael@0 | 113 | let request = client.getCollectionInfo(); |
michael@0 | 114 | request.handler = handler; |
michael@0 | 115 | request.dispatch(); |
michael@0 | 116 | }); |
michael@0 | 117 | |
michael@0 | 118 | add_test(function test_info_collections() { |
michael@0 | 119 | _("Ensure requests to /info/collections work as expected."); |
michael@0 | 120 | |
michael@0 | 121 | let [server, client] = getServerAndClient(); |
michael@0 | 122 | |
michael@0 | 123 | let request = client.getCollectionInfo(); |
michael@0 | 124 | request.dispatch(function onComplete(error, req) { |
michael@0 | 125 | do_check_null(error); |
michael@0 | 126 | do_check_eq("object", typeof req.resultObj); |
michael@0 | 127 | do_check_attribute_count(req.resultObj, 3); |
michael@0 | 128 | do_check_true("meta" in req.resultObj); |
michael@0 | 129 | |
michael@0 | 130 | server.stop(run_next_test); |
michael@0 | 131 | }); |
michael@0 | 132 | }); |
michael@0 | 133 | |
michael@0 | 134 | add_test(function test_info_collections_conditional_not_modified() { |
michael@0 | 135 | _("Ensure conditional getCollectionInfo requests work."); |
michael@0 | 136 | |
michael@0 | 137 | let [server, client, username] = getServerAndClient(); |
michael@0 | 138 | let user = server.user(username); |
michael@0 | 139 | |
michael@0 | 140 | let now = Date.now(); |
michael@0 | 141 | |
michael@0 | 142 | user.createCollection("testcoll", { |
michael@0 | 143 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 144 | }); |
michael@0 | 145 | |
michael@0 | 146 | let request = client.getCollectionInfo(); |
michael@0 | 147 | request.locallyModifiedVersion = now + 10; |
michael@0 | 148 | request.dispatch(function onComplete(error, req) { |
michael@0 | 149 | do_check_null(error); |
michael@0 | 150 | do_check_true(req.notModified); |
michael@0 | 151 | |
michael@0 | 152 | server.stop(run_next_test); |
michael@0 | 153 | }); |
michael@0 | 154 | }); |
michael@0 | 155 | |
michael@0 | 156 | add_test(function test_info_collections_conditional_modified() { |
michael@0 | 157 | _("Ensure conditional getCollectionInfo requests work."); |
michael@0 | 158 | |
michael@0 | 159 | let [server, client, username] = getServerAndClient(); |
michael@0 | 160 | let user = server.user(username); |
michael@0 | 161 | |
michael@0 | 162 | let now = Date.now(); |
michael@0 | 163 | |
michael@0 | 164 | user.createCollection("testcoll", { |
michael@0 | 165 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 166 | }); |
michael@0 | 167 | |
michael@0 | 168 | let request = client.getCollectionInfo(); |
michael@0 | 169 | request.locallyModifiedVersion = now - 10; |
michael@0 | 170 | request.dispatch(function onComplete(error, req) { |
michael@0 | 171 | do_check_null(error); |
michael@0 | 172 | do_check_false(req.notModified); |
michael@0 | 173 | |
michael@0 | 174 | server.stop(run_next_test); |
michael@0 | 175 | }); |
michael@0 | 176 | }); |
michael@0 | 177 | |
michael@0 | 178 | add_test(function test_get_quota() { |
michael@0 | 179 | _("Ensure quota requests work."); |
michael@0 | 180 | |
michael@0 | 181 | let [server, client] = getServerAndClient(); |
michael@0 | 182 | |
michael@0 | 183 | let request = client.getQuota(); |
michael@0 | 184 | request.dispatch(function onComplete(error, req) { |
michael@0 | 185 | do_check_null(error); |
michael@0 | 186 | |
michael@0 | 187 | do_check_eq(req.resultObj.quota, 1048576); |
michael@0 | 188 | |
michael@0 | 189 | server.stop(run_next_test); |
michael@0 | 190 | }); |
michael@0 | 191 | }); |
michael@0 | 192 | |
michael@0 | 193 | add_test(function test_get_quota_conditional_not_modified() { |
michael@0 | 194 | _("Ensure conditional getQuota requests work."); |
michael@0 | 195 | |
michael@0 | 196 | let [server, client, username] = getServerAndClient(); |
michael@0 | 197 | let user = server.user(username); |
michael@0 | 198 | |
michael@0 | 199 | let now = Date.now(); |
michael@0 | 200 | |
michael@0 | 201 | user.createCollection("testcoll", { |
michael@0 | 202 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 203 | }); |
michael@0 | 204 | |
michael@0 | 205 | let request = client.getQuota(); |
michael@0 | 206 | request.locallyModifiedVersion = now + 10; |
michael@0 | 207 | request.dispatch(function onComplete(error, req) { |
michael@0 | 208 | do_check_null(error); |
michael@0 | 209 | do_check_true(req.notModified); |
michael@0 | 210 | |
michael@0 | 211 | server.stop(run_next_test); |
michael@0 | 212 | }); |
michael@0 | 213 | }); |
michael@0 | 214 | |
michael@0 | 215 | add_test(function test_get_quota_conditional_modified() { |
michael@0 | 216 | _("Ensure conditional getQuota requests work."); |
michael@0 | 217 | |
michael@0 | 218 | let [server, client, username] = getServerAndClient(); |
michael@0 | 219 | let user = server.user(username); |
michael@0 | 220 | |
michael@0 | 221 | let now = Date.now(); |
michael@0 | 222 | |
michael@0 | 223 | user.createCollection("testcoll", { |
michael@0 | 224 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 225 | }); |
michael@0 | 226 | |
michael@0 | 227 | let request = client.getQuota(); |
michael@0 | 228 | request.locallyModifiedVersion = now - 10; |
michael@0 | 229 | request.dispatch(function onComplete(error, req) { |
michael@0 | 230 | do_check_null(error); |
michael@0 | 231 | do_check_false(req.notModified); |
michael@0 | 232 | |
michael@0 | 233 | server.stop(run_next_test); |
michael@0 | 234 | }); |
michael@0 | 235 | }); |
michael@0 | 236 | |
michael@0 | 237 | add_test(function test_get_collection_usage() { |
michael@0 | 238 | _("Ensure info/collection_usage requests work."); |
michael@0 | 239 | |
michael@0 | 240 | let [server, client, username] = getServerAndClient(); |
michael@0 | 241 | let user = server.user(username); |
michael@0 | 242 | |
michael@0 | 243 | user.createCollection("testcoll", { |
michael@0 | 244 | abc123: new ServerBSO("abc123", "payload", Date.now()) |
michael@0 | 245 | }); |
michael@0 | 246 | |
michael@0 | 247 | let request = client.getCollectionUsage(); |
michael@0 | 248 | request.dispatch(function onComplete(error, req) { |
michael@0 | 249 | do_check_null(error); |
michael@0 | 250 | |
michael@0 | 251 | let usage = req.resultObj; |
michael@0 | 252 | do_check_true("testcoll" in usage); |
michael@0 | 253 | do_check_eq(usage.testcoll, "payload".length); |
michael@0 | 254 | |
michael@0 | 255 | server.stop(run_next_test); |
michael@0 | 256 | }); |
michael@0 | 257 | }); |
michael@0 | 258 | |
michael@0 | 259 | add_test(function test_get_usage_conditional_not_modified() { |
michael@0 | 260 | _("Ensure conditional getCollectionUsage requests work."); |
michael@0 | 261 | |
michael@0 | 262 | let [server, client, username] = getServerAndClient(); |
michael@0 | 263 | let user = server.user(username); |
michael@0 | 264 | |
michael@0 | 265 | let now = Date.now(); |
michael@0 | 266 | |
michael@0 | 267 | user.createCollection("testcoll", { |
michael@0 | 268 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 269 | }); |
michael@0 | 270 | |
michael@0 | 271 | let request = client.getCollectionUsage(); |
michael@0 | 272 | request.locallyModifiedVersion = now + 10; |
michael@0 | 273 | request.dispatch(function onComplete(error, req) { |
michael@0 | 274 | do_check_null(error); |
michael@0 | 275 | do_check_true(req.notModified); |
michael@0 | 276 | |
michael@0 | 277 | server.stop(run_next_test); |
michael@0 | 278 | }); |
michael@0 | 279 | }); |
michael@0 | 280 | |
michael@0 | 281 | add_test(function test_get_usage_conditional_modified() { |
michael@0 | 282 | _("Ensure conditional getCollectionUsage requests work."); |
michael@0 | 283 | |
michael@0 | 284 | let [server, client, username] = getServerAndClient(); |
michael@0 | 285 | let user = server.user(username); |
michael@0 | 286 | |
michael@0 | 287 | let now = Date.now(); |
michael@0 | 288 | |
michael@0 | 289 | user.createCollection("testcoll", { |
michael@0 | 290 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 291 | }); |
michael@0 | 292 | |
michael@0 | 293 | let request = client.getCollectionUsage(); |
michael@0 | 294 | request.locallyModifiedVersion = now - 10; |
michael@0 | 295 | request.dispatch(function onComplete(error, req) { |
michael@0 | 296 | do_check_null(error); |
michael@0 | 297 | do_check_false(req.notModified); |
michael@0 | 298 | |
michael@0 | 299 | server.stop(run_next_test); |
michael@0 | 300 | }); |
michael@0 | 301 | }); |
michael@0 | 302 | |
michael@0 | 303 | add_test(function test_get_collection_counts() { |
michael@0 | 304 | _("Ensure info/collection_counts requests work."); |
michael@0 | 305 | |
michael@0 | 306 | let [server, client, username] = getServerAndClient(); |
michael@0 | 307 | let user = server.user(username); |
michael@0 | 308 | |
michael@0 | 309 | user.createCollection("testcoll", { |
michael@0 | 310 | foo: new ServerBSO("foo", "payload0", Date.now()), |
michael@0 | 311 | bar: new ServerBSO("bar", "payload1", Date.now()) |
michael@0 | 312 | }); |
michael@0 | 313 | |
michael@0 | 314 | let request = client.getCollectionCounts(); |
michael@0 | 315 | request.dispatch(function onComplete(error, req) { |
michael@0 | 316 | do_check_null(error); |
michael@0 | 317 | |
michael@0 | 318 | let counts = req.resultObj; |
michael@0 | 319 | do_check_true("testcoll" in counts); |
michael@0 | 320 | do_check_eq(counts.testcoll, 2); |
michael@0 | 321 | |
michael@0 | 322 | server.stop(run_next_test); |
michael@0 | 323 | }); |
michael@0 | 324 | }); |
michael@0 | 325 | |
michael@0 | 326 | add_test(function test_get_counts_conditional_not_modified() { |
michael@0 | 327 | _("Ensure conditional getCollectionCounts requests work."); |
michael@0 | 328 | |
michael@0 | 329 | let [server, client, username] = getServerAndClient(); |
michael@0 | 330 | let user = server.user(username); |
michael@0 | 331 | |
michael@0 | 332 | let now = Date.now(); |
michael@0 | 333 | |
michael@0 | 334 | user.createCollection("testcoll", { |
michael@0 | 335 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 336 | }); |
michael@0 | 337 | |
michael@0 | 338 | let request = client.getCollectionCounts(); |
michael@0 | 339 | request.locallyModifiedVersion = now + 10; |
michael@0 | 340 | request.dispatch(function onComplete(error, req) { |
michael@0 | 341 | do_check_null(error); |
michael@0 | 342 | do_check_true(req.notModified); |
michael@0 | 343 | |
michael@0 | 344 | server.stop(run_next_test); |
michael@0 | 345 | }); |
michael@0 | 346 | }); |
michael@0 | 347 | |
michael@0 | 348 | add_test(function test_get_counts_conditional_modified() { |
michael@0 | 349 | _("Ensure conditional getCollectionCounts requests work."); |
michael@0 | 350 | |
michael@0 | 351 | let [server, client, username] = getServerAndClient(); |
michael@0 | 352 | let user = server.user(username); |
michael@0 | 353 | |
michael@0 | 354 | let now = Date.now(); |
michael@0 | 355 | |
michael@0 | 356 | user.createCollection("testcoll", { |
michael@0 | 357 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 358 | }); |
michael@0 | 359 | |
michael@0 | 360 | let request = client.getCollectionCounts(); |
michael@0 | 361 | request.locallyModifiedVersion = now - 10; |
michael@0 | 362 | request.dispatch(function onComplete(error, req) { |
michael@0 | 363 | do_check_null(error); |
michael@0 | 364 | do_check_false(req.notModified); |
michael@0 | 365 | |
michael@0 | 366 | server.stop(run_next_test); |
michael@0 | 367 | }); |
michael@0 | 368 | }); |
michael@0 | 369 | |
michael@0 | 370 | add_test(function test_get_collection_simple() { |
michael@0 | 371 | _("Ensure basic collection retrieval works."); |
michael@0 | 372 | |
michael@0 | 373 | let [server, client, username] = getServerAndClient(); |
michael@0 | 374 | let user = server.user(username); |
michael@0 | 375 | |
michael@0 | 376 | user.createCollection("testcoll", { |
michael@0 | 377 | foo: new ServerBSO("foo", "payload0", Date.now()), |
michael@0 | 378 | bar: new ServerBSO("bar", "payload1", Date.now()) |
michael@0 | 379 | }); |
michael@0 | 380 | |
michael@0 | 381 | let request = client.getCollection("testcoll"); |
michael@0 | 382 | let bsos = []; |
michael@0 | 383 | request.handler = { |
michael@0 | 384 | onBSORecord: function onBSORecord(request, bso) { |
michael@0 | 385 | bsos.push(bso); |
michael@0 | 386 | }, |
michael@0 | 387 | |
michael@0 | 388 | onComplete: function onComplete(error, request) { |
michael@0 | 389 | do_check_null(error); |
michael@0 | 390 | |
michael@0 | 391 | do_check_eq(bsos.length, 2); |
michael@0 | 392 | do_check_eq(bsos[0], "foo"); |
michael@0 | 393 | do_check_eq(bsos[1], "bar"); |
michael@0 | 394 | |
michael@0 | 395 | server.stop(run_next_test); |
michael@0 | 396 | } |
michael@0 | 397 | }; |
michael@0 | 398 | request.dispatch(); |
michael@0 | 399 | }); |
michael@0 | 400 | |
michael@0 | 401 | add_test(function test_get_collection_conditional_not_modified() { |
michael@0 | 402 | _("Ensure conditional requests with no new data to getCollection work."); |
michael@0 | 403 | |
michael@0 | 404 | let [server, client, username] = getServerAndClient(); |
michael@0 | 405 | let user = server.user(username); |
michael@0 | 406 | |
michael@0 | 407 | let now = Date.now(); |
michael@0 | 408 | |
michael@0 | 409 | user.createCollection("testcoll", { |
michael@0 | 410 | foo: new ServerBSO("foo", "payload0", now) |
michael@0 | 411 | }); |
michael@0 | 412 | |
michael@0 | 413 | let request = client.getCollection("testcoll"); |
michael@0 | 414 | request.locallyModifiedVersion = now + 1; |
michael@0 | 415 | |
michael@0 | 416 | request.dispatch(function onComplete(error, req) { |
michael@0 | 417 | do_check_null(error); |
michael@0 | 418 | |
michael@0 | 419 | do_check_true(req.notModified); |
michael@0 | 420 | |
michael@0 | 421 | server.stop(run_next_test); |
michael@0 | 422 | }); |
michael@0 | 423 | }); |
michael@0 | 424 | |
michael@0 | 425 | add_test(function test_get_collection_conditional_modified() { |
michael@0 | 426 | _("Ensure conditional requests with new data to getCollection work."); |
michael@0 | 427 | |
michael@0 | 428 | let [server, client, username] = getServerAndClient(); |
michael@0 | 429 | let user = server.user(username); |
michael@0 | 430 | |
michael@0 | 431 | let now = Date.now(); |
michael@0 | 432 | |
michael@0 | 433 | user.createCollection("testcoll", { |
michael@0 | 434 | foo: new ServerBSO("foo", "payload0", now) |
michael@0 | 435 | }); |
michael@0 | 436 | |
michael@0 | 437 | let request = client.getCollection("testcoll"); |
michael@0 | 438 | request.locallyModifiedVersion = now - 1; |
michael@0 | 439 | |
michael@0 | 440 | let bsoCount = 0; |
michael@0 | 441 | request.handler = { |
michael@0 | 442 | onBSORecord: function onBSORecord() { |
michael@0 | 443 | bsoCount++; |
michael@0 | 444 | }, |
michael@0 | 445 | |
michael@0 | 446 | onComplete: function onComplete(error, req) { |
michael@0 | 447 | do_check_null(error); |
michael@0 | 448 | |
michael@0 | 449 | do_check_false(req.notModified); |
michael@0 | 450 | do_check_eq(bsoCount, 1); |
michael@0 | 451 | |
michael@0 | 452 | server.stop(run_next_test); |
michael@0 | 453 | } |
michael@0 | 454 | }; |
michael@0 | 455 | request.dispatch(); |
michael@0 | 456 | }); |
michael@0 | 457 | |
michael@0 | 458 | // This is effectively a sanity test for query string generation. |
michael@0 | 459 | add_test(function test_get_collection_newer() { |
michael@0 | 460 | _("Ensure query string for newer and full work together."); |
michael@0 | 461 | |
michael@0 | 462 | let [server, client, username] = getServerAndClient(); |
michael@0 | 463 | |
michael@0 | 464 | let date0 = Date.now(); |
michael@0 | 465 | let date1 = date0 + 500; |
michael@0 | 466 | |
michael@0 | 467 | let user = server.user(username); |
michael@0 | 468 | user.createCollection("testcoll", { |
michael@0 | 469 | foo: new ServerBSO("foo", "payload0", date0), |
michael@0 | 470 | bar: new ServerBSO("bar", "payload1", date1) |
michael@0 | 471 | }); |
michael@0 | 472 | |
michael@0 | 473 | let request = client.getCollection("testcoll"); |
michael@0 | 474 | request.full = true; |
michael@0 | 475 | request.newer = date0; |
michael@0 | 476 | |
michael@0 | 477 | let bsos = []; |
michael@0 | 478 | request.handler = { |
michael@0 | 479 | onBSORecord: function onBSORecord(request, bso) { |
michael@0 | 480 | bsos.push(bso); |
michael@0 | 481 | }, |
michael@0 | 482 | |
michael@0 | 483 | onComplete: function onComplete(error, req) { |
michael@0 | 484 | do_check_null(error); |
michael@0 | 485 | |
michael@0 | 486 | do_check_eq(bsos.length, 1); |
michael@0 | 487 | let bso = bsos[0]; |
michael@0 | 488 | |
michael@0 | 489 | do_check_eq(bso.id, "bar"); |
michael@0 | 490 | do_check_eq(bso.payload, "payload1"); |
michael@0 | 491 | |
michael@0 | 492 | server.stop(run_next_test); |
michael@0 | 493 | } |
michael@0 | 494 | }; |
michael@0 | 495 | request.dispatch(); |
michael@0 | 496 | }); |
michael@0 | 497 | |
michael@0 | 498 | add_test(function test_get_bso() { |
michael@0 | 499 | _("Ensure that simple BSO fetches work."); |
michael@0 | 500 | |
michael@0 | 501 | let [server, client, username] = getServerAndClient(); |
michael@0 | 502 | |
michael@0 | 503 | server.createCollection(username, "testcoll", { |
michael@0 | 504 | abc123: new ServerBSO("abc123", "payload", Date.now()) |
michael@0 | 505 | }); |
michael@0 | 506 | |
michael@0 | 507 | let request = client.getBSO("testcoll", "abc123"); |
michael@0 | 508 | request.dispatch(function(error, req) { |
michael@0 | 509 | do_check_null(error); |
michael@0 | 510 | do_check_true(req.resultObj instanceof BasicStorageObject); |
michael@0 | 511 | |
michael@0 | 512 | let bso = req.resultObj; |
michael@0 | 513 | do_check_eq(bso.id, "abc123"); |
michael@0 | 514 | do_check_eq(bso.payload, "payload"); |
michael@0 | 515 | |
michael@0 | 516 | server.stop(run_next_test); |
michael@0 | 517 | }); |
michael@0 | 518 | }); |
michael@0 | 519 | |
michael@0 | 520 | add_test(function test_bso_conditional() { |
michael@0 | 521 | _("Ensure conditional requests for an individual BSO work."); |
michael@0 | 522 | |
michael@0 | 523 | let [server, client, username] = getServerAndClient(); |
michael@0 | 524 | |
michael@0 | 525 | let user = server.user(username); |
michael@0 | 526 | let now = Date.now(); |
michael@0 | 527 | user.createCollection("testcoll", { |
michael@0 | 528 | foo: new ServerBSO("foo", "payload", now) |
michael@0 | 529 | }); |
michael@0 | 530 | |
michael@0 | 531 | let request = client.getBSO("testcoll", "foo"); |
michael@0 | 532 | request.locallyModifiedVersion = now; |
michael@0 | 533 | |
michael@0 | 534 | request.dispatch(function onComplete(error, req) { |
michael@0 | 535 | do_check_null(error); |
michael@0 | 536 | do_check_true(req.notModified); |
michael@0 | 537 | |
michael@0 | 538 | server.stop(run_next_test); |
michael@0 | 539 | }); |
michael@0 | 540 | }); |
michael@0 | 541 | |
michael@0 | 542 | add_test(function test_set_bso() { |
michael@0 | 543 | _("Ensure simple BSO PUT works."); |
michael@0 | 544 | |
michael@0 | 545 | let [server, client] = getServerAndClient(); |
michael@0 | 546 | |
michael@0 | 547 | let id = "mnas08h3f3r2351"; |
michael@0 | 548 | |
michael@0 | 549 | let bso = new BasicStorageObject(id, "testcoll"); |
michael@0 | 550 | bso.payload = "my test payload"; |
michael@0 | 551 | |
michael@0 | 552 | let request = client.setBSO(bso); |
michael@0 | 553 | request.dispatch(function(error, req) { |
michael@0 | 554 | do_check_eq(error, null); |
michael@0 | 555 | do_check_eq(req.resultObj, null); |
michael@0 | 556 | |
michael@0 | 557 | server.stop(run_next_test); |
michael@0 | 558 | }); |
michael@0 | 559 | }); |
michael@0 | 560 | |
michael@0 | 561 | |
michael@0 | 562 | add_test(function test_set_bso_conditional() { |
michael@0 | 563 | _("Ensure conditional setting a BSO is properly rejected."); |
michael@0 | 564 | |
michael@0 | 565 | let [server, client, username] = getServerAndClient(); |
michael@0 | 566 | let user = server.user(username); |
michael@0 | 567 | |
michael@0 | 568 | let now = Date.now(); |
michael@0 | 569 | user.createCollection("testcoll", { |
michael@0 | 570 | foo: new ServerBSO("foo", "payload0", now + 1000) |
michael@0 | 571 | }); |
michael@0 | 572 | |
michael@0 | 573 | // Should get an mtime newer than server's. |
michael@0 | 574 | let bso = new BasicStorageObject("foo", "testcoll"); |
michael@0 | 575 | bso.payload = "payload1"; |
michael@0 | 576 | |
michael@0 | 577 | let request = client.setBSO(bso); |
michael@0 | 578 | request.locallyModifiedVersion = now; |
michael@0 | 579 | request.dispatch(function onComplete(error, req) { |
michael@0 | 580 | do_check_true(error instanceof StorageServiceRequestError); |
michael@0 | 581 | do_check_true(error.serverModified); |
michael@0 | 582 | |
michael@0 | 583 | server.stop(run_next_test); |
michael@0 | 584 | }); |
michael@0 | 585 | }); |
michael@0 | 586 | |
michael@0 | 587 | add_test(function test_set_bso_argument_errors() { |
michael@0 | 588 | _("Ensure BSO set detects invalid arguments."); |
michael@0 | 589 | |
michael@0 | 590 | let server = getEmptyServer(); |
michael@0 | 591 | let bso = new BasicStorageObject(); |
michael@0 | 592 | let client = getClient(server); |
michael@0 | 593 | |
michael@0 | 594 | let threw = false; |
michael@0 | 595 | try { |
michael@0 | 596 | client.setBSO(bso); |
michael@0 | 597 | } catch (ex) { |
michael@0 | 598 | threw = true; |
michael@0 | 599 | do_check_eq(ex.name, "Error"); |
michael@0 | 600 | do_check_neq(ex.message.indexOf("does not have collection defined"), -1); |
michael@0 | 601 | } finally { |
michael@0 | 602 | do_check_true(threw); |
michael@0 | 603 | threw = false; |
michael@0 | 604 | } |
michael@0 | 605 | |
michael@0 | 606 | bso = new BasicStorageObject("id"); |
michael@0 | 607 | try { |
michael@0 | 608 | client.setBSO(bso); |
michael@0 | 609 | } catch (ex) { |
michael@0 | 610 | threw = true; |
michael@0 | 611 | do_check_eq(ex.name, "Error"); |
michael@0 | 612 | do_check_neq(ex.message.indexOf("does not have collection defined"), -1); |
michael@0 | 613 | } finally { |
michael@0 | 614 | do_check_true(threw); |
michael@0 | 615 | threw = false; |
michael@0 | 616 | } |
michael@0 | 617 | |
michael@0 | 618 | bso = new BasicStorageObject(null, "coll"); |
michael@0 | 619 | try { |
michael@0 | 620 | client.setBSO(bso); |
michael@0 | 621 | } catch (ex) { |
michael@0 | 622 | threw = true; |
michael@0 | 623 | do_check_eq(ex.name, "Error"); |
michael@0 | 624 | do_check_neq(ex.message.indexOf("does not have ID defined"), -1); |
michael@0 | 625 | } finally { |
michael@0 | 626 | do_check_true(threw); |
michael@0 | 627 | threw = false; |
michael@0 | 628 | } |
michael@0 | 629 | |
michael@0 | 630 | server.stop(run_next_test); |
michael@0 | 631 | }); |
michael@0 | 632 | |
michael@0 | 633 | add_test(function test_set_bsos_simple() { |
michael@0 | 634 | _("Ensure setBSOs with basic options works."); |
michael@0 | 635 | |
michael@0 | 636 | let [server, client, username] = getServerAndClient(); |
michael@0 | 637 | let user = server.user(username); |
michael@0 | 638 | |
michael@0 | 639 | let bso0 = new BasicStorageObject("foo"); |
michael@0 | 640 | bso0.payload = "payload0"; |
michael@0 | 641 | |
michael@0 | 642 | let bso1 = new BasicStorageObject("bar"); |
michael@0 | 643 | bso1.payload = "payload1"; |
michael@0 | 644 | |
michael@0 | 645 | let request = client.setBSOs("testcollection"); |
michael@0 | 646 | request.addBSO(bso0); |
michael@0 | 647 | request.addBSO(bso1); |
michael@0 | 648 | |
michael@0 | 649 | request.dispatch(function onComplete(error, req) { |
michael@0 | 650 | do_check_null(error); |
michael@0 | 651 | |
michael@0 | 652 | let successful = req.successfulIDs; |
michael@0 | 653 | do_check_eq(successful.length, 2); |
michael@0 | 654 | do_check_eq(successful.indexOf(bso0.id), 0); |
michael@0 | 655 | do_check_true(successful.indexOf(bso1.id), 1); |
michael@0 | 656 | |
michael@0 | 657 | server.stop(run_next_test); |
michael@0 | 658 | }); |
michael@0 | 659 | }); |
michael@0 | 660 | |
michael@0 | 661 | add_test(function test_set_bsos_invalid_bso() { |
michael@0 | 662 | _("Ensure that adding an invalid BSO throws."); |
michael@0 | 663 | |
michael@0 | 664 | let server = getEmptyServer(); |
michael@0 | 665 | let client = getClient(server); |
michael@0 | 666 | let request = client.setBSOs("testcoll"); |
michael@0 | 667 | |
michael@0 | 668 | let threw = false; |
michael@0 | 669 | |
michael@0 | 670 | // Empty argument is invalid. |
michael@0 | 671 | try { |
michael@0 | 672 | request.addBSO(null); |
michael@0 | 673 | } catch (ex) { |
michael@0 | 674 | threw = true; |
michael@0 | 675 | } finally { |
michael@0 | 676 | do_check_true(threw); |
michael@0 | 677 | threw = false; |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | try { |
michael@0 | 681 | let bso = new BasicStorageObject(); |
michael@0 | 682 | request.addBSO(bso); |
michael@0 | 683 | } catch (ex) { |
michael@0 | 684 | threw = true; |
michael@0 | 685 | } finally { |
michael@0 | 686 | do_check_true(threw); |
michael@0 | 687 | threw = false; |
michael@0 | 688 | } |
michael@0 | 689 | |
michael@0 | 690 | server.stop(run_next_test); |
michael@0 | 691 | }); |
michael@0 | 692 | |
michael@0 | 693 | add_test(function test_set_bsos_newline() { |
michael@0 | 694 | _("Ensure that newlines in BSO payloads are formatted properly."); |
michael@0 | 695 | |
michael@0 | 696 | let [server, client, username] = getServerAndClient(); |
michael@0 | 697 | let user = server.user(username); |
michael@0 | 698 | |
michael@0 | 699 | let request = client.setBSOs("testcoll"); |
michael@0 | 700 | |
michael@0 | 701 | let bso0 = new BasicStorageObject("bso0"); |
michael@0 | 702 | bso0.payload = "hello\nworld"; |
michael@0 | 703 | request.addBSO(bso0); |
michael@0 | 704 | |
michael@0 | 705 | let bso1 = new BasicStorageObject("bso1"); |
michael@0 | 706 | bso1.payload = "foobar"; |
michael@0 | 707 | request.addBSO(bso1); |
michael@0 | 708 | |
michael@0 | 709 | request.dispatch(function onComplete(error, request) { |
michael@0 | 710 | do_check_null(error); |
michael@0 | 711 | do_check_eq(request.successfulIDs.length, 2); |
michael@0 | 712 | |
michael@0 | 713 | let coll = user.collection("testcoll"); |
michael@0 | 714 | do_check_eq(coll.bso("bso0").payload, bso0.payload); |
michael@0 | 715 | do_check_eq(coll.bso("bso1").payload, bso1.payload); |
michael@0 | 716 | |
michael@0 | 717 | server.stop(run_next_test); |
michael@0 | 718 | }); |
michael@0 | 719 | }); |
michael@0 | 720 | |
michael@0 | 721 | add_test(function test_delete_bso_simple() { |
michael@0 | 722 | _("Ensure deletion of individual BSOs works."); |
michael@0 | 723 | |
michael@0 | 724 | let [server, client, username] = getServerAndClient(); |
michael@0 | 725 | |
michael@0 | 726 | let user = server.user(username); |
michael@0 | 727 | let coll = user.createCollection("testcoll", { |
michael@0 | 728 | foo: new ServerBSO("foo", "payload0", Date.now()), |
michael@0 | 729 | bar: new ServerBSO("bar", "payload1", Date.now()) |
michael@0 | 730 | }); |
michael@0 | 731 | |
michael@0 | 732 | let request = client.deleteBSO("testcoll", "foo"); |
michael@0 | 733 | request.dispatch(function onComplete(error, req) { |
michael@0 | 734 | do_check_null(error); |
michael@0 | 735 | do_check_eq(req.statusCode, 204); |
michael@0 | 736 | |
michael@0 | 737 | do_check_eq(coll.count(), 1); |
michael@0 | 738 | |
michael@0 | 739 | server.stop(run_next_test); |
michael@0 | 740 | }); |
michael@0 | 741 | }); |
michael@0 | 742 | |
michael@0 | 743 | add_test(function test_delete_bso_conditional_failed() { |
michael@0 | 744 | _("Ensure deletion of an individual BSO with older modification fails."); |
michael@0 | 745 | |
michael@0 | 746 | let [server, client, username] = getServerAndClient(); |
michael@0 | 747 | let user = server.user(username); |
michael@0 | 748 | |
michael@0 | 749 | let now = Date.now(); |
michael@0 | 750 | user.createCollection("testcoll", { |
michael@0 | 751 | foo: new ServerBSO("foo", "payload0", now) |
michael@0 | 752 | }); |
michael@0 | 753 | |
michael@0 | 754 | let request = client.deleteBSO("testcoll", "foo"); |
michael@0 | 755 | request.locallyModifiedVersion = now - 10; |
michael@0 | 756 | |
michael@0 | 757 | request.dispatch(function onComplete(error, req) { |
michael@0 | 758 | do_check_true(error instanceof StorageServiceRequestError); |
michael@0 | 759 | do_check_true(error.serverModified); |
michael@0 | 760 | |
michael@0 | 761 | server.stop(run_next_test); |
michael@0 | 762 | }); |
michael@0 | 763 | }); |
michael@0 | 764 | |
michael@0 | 765 | add_test(function test_delete_bso_conditional_success() { |
michael@0 | 766 | _("Ensure deletion of an individual BSO with newer modification works."); |
michael@0 | 767 | |
michael@0 | 768 | let [server, client, username] = getServerAndClient(); |
michael@0 | 769 | let user = server.user(username); |
michael@0 | 770 | |
michael@0 | 771 | let now = Date.now(); |
michael@0 | 772 | user.createCollection("testcoll", { |
michael@0 | 773 | foo: new ServerBSO("foo", "payload0", now) |
michael@0 | 774 | }); |
michael@0 | 775 | |
michael@0 | 776 | let request = client.deleteBSO("testcoll", "foo"); |
michael@0 | 777 | request.locallyModifiedVersion = now; |
michael@0 | 778 | |
michael@0 | 779 | request.dispatch(function onComplete(error, req) { |
michael@0 | 780 | do_check_null(error); |
michael@0 | 781 | do_check_eq(req.statusCode, 204); |
michael@0 | 782 | |
michael@0 | 783 | server.stop(run_next_test); |
michael@0 | 784 | }); |
michael@0 | 785 | }); |
michael@0 | 786 | |
michael@0 | 787 | add_test(function test_delete_bsos_simple() { |
michael@0 | 788 | _("Ensure deletion of multiple BSOs works."); |
michael@0 | 789 | |
michael@0 | 790 | let [server, client, username] = getServerAndClient(); |
michael@0 | 791 | let user = server.user(username); |
michael@0 | 792 | |
michael@0 | 793 | let coll = user.createCollection("testcoll", { |
michael@0 | 794 | foo: new ServerBSO("foo", "payload0", Date.now()), |
michael@0 | 795 | bar: new ServerBSO("bar", "payload1", Date.now()), |
michael@0 | 796 | baz: new ServerBSO("baz", "payload2", Date.now()) |
michael@0 | 797 | }); |
michael@0 | 798 | |
michael@0 | 799 | let request = client.deleteBSOs("testcoll", ["foo", "baz"]); |
michael@0 | 800 | request.dispatch(function onComplete(error, req) { |
michael@0 | 801 | do_check_null(error); |
michael@0 | 802 | do_check_eq(req.statusCode, 204); |
michael@0 | 803 | |
michael@0 | 804 | do_check_eq(coll.count(), 1); |
michael@0 | 805 | |
michael@0 | 806 | server.stop(run_next_test); |
michael@0 | 807 | }); |
michael@0 | 808 | }); |
michael@0 | 809 | |
michael@0 | 810 | add_test(function test_delete_bsos_conditional_failed() { |
michael@0 | 811 | _("Ensure deletion of BSOs with server modifications fails."); |
michael@0 | 812 | |
michael@0 | 813 | let [server, client, username] = getServerAndClient(); |
michael@0 | 814 | let user = server.user(username); |
michael@0 | 815 | |
michael@0 | 816 | let now = Date.now(); |
michael@0 | 817 | let coll = user.createCollection("testcoll", { |
michael@0 | 818 | foo: new ServerBSO("foo", "payload0", now) |
michael@0 | 819 | }); |
michael@0 | 820 | |
michael@0 | 821 | let request = client.deleteBSOs("testcoll", ["foo"]); |
michael@0 | 822 | request.locallyModifiedVersion = coll.timestamp - 1; |
michael@0 | 823 | |
michael@0 | 824 | request.dispatch(function onComplete(error, req) { |
michael@0 | 825 | do_check_true(error instanceof StorageServiceRequestError); |
michael@0 | 826 | do_check_true(error.serverModified); |
michael@0 | 827 | |
michael@0 | 828 | server.stop(run_next_test); |
michael@0 | 829 | }); |
michael@0 | 830 | }); |
michael@0 | 831 | |
michael@0 | 832 | add_test(function test_delete_bsos_conditional_success() { |
michael@0 | 833 | _("Ensure conditional deletion of BSOs without server modifications works."); |
michael@0 | 834 | |
michael@0 | 835 | let [server, client, username] = getServerAndClient(); |
michael@0 | 836 | let user = server.user(username); |
michael@0 | 837 | |
michael@0 | 838 | let now = Date.now(); |
michael@0 | 839 | let coll = user.createCollection("testcoll", { |
michael@0 | 840 | foo: new ServerBSO("foo", "payload0", now), |
michael@0 | 841 | bar: new ServerBSO("bar", "payload1", now - 10) |
michael@0 | 842 | }); |
michael@0 | 843 | |
michael@0 | 844 | let request = client.deleteBSOs("testcoll", ["bar"]); |
michael@0 | 845 | request.locallyModifiedVersion = coll.timestamp; |
michael@0 | 846 | |
michael@0 | 847 | request.dispatch(function onComplete(error, req) { |
michael@0 | 848 | do_check_null(error); |
michael@0 | 849 | |
michael@0 | 850 | do_check_eq(req.statusCode, 204); |
michael@0 | 851 | |
michael@0 | 852 | server.stop(run_next_test); |
michael@0 | 853 | }); |
michael@0 | 854 | }); |
michael@0 | 855 | |
michael@0 | 856 | add_test(function test_delete_collection() { |
michael@0 | 857 | _("Ensure deleteCollection() works."); |
michael@0 | 858 | |
michael@0 | 859 | let [server, client, username] = getServerAndClient(); |
michael@0 | 860 | let user = server.user(username); |
michael@0 | 861 | |
michael@0 | 862 | user.createCollection("testcoll", { |
michael@0 | 863 | foo: new ServerBSO("foo", "payload0", Date.now()) |
michael@0 | 864 | }); |
michael@0 | 865 | |
michael@0 | 866 | let request = client.deleteCollection("testcoll"); |
michael@0 | 867 | request.dispatch(function onComplete(error, req) { |
michael@0 | 868 | do_check_null(error); |
michael@0 | 869 | |
michael@0 | 870 | do_check_eq(user.collection("testcoll", undefined)); |
michael@0 | 871 | |
michael@0 | 872 | server.stop(run_next_test); |
michael@0 | 873 | }); |
michael@0 | 874 | }); |
michael@0 | 875 | |
michael@0 | 876 | add_test(function test_delete_collection_conditional_failed() { |
michael@0 | 877 | _("Ensure conditional deletes with server modifications fail."); |
michael@0 | 878 | |
michael@0 | 879 | let [server, client, username] = getServerAndClient(); |
michael@0 | 880 | let user = server.user(username); |
michael@0 | 881 | |
michael@0 | 882 | let now = Date.now(); |
michael@0 | 883 | |
michael@0 | 884 | let coll = user.createCollection("testcoll", { |
michael@0 | 885 | foo: new ServerBSO("foo", "payload0", now) |
michael@0 | 886 | }); |
michael@0 | 887 | |
michael@0 | 888 | let request = client.deleteCollection("testcoll"); |
michael@0 | 889 | request.locallyModifiedVersion = coll.timestamp - 1; |
michael@0 | 890 | |
michael@0 | 891 | request.dispatch(function onComplete(error, req) { |
michael@0 | 892 | do_check_true(error instanceof StorageServiceRequestError); |
michael@0 | 893 | do_check_true(error.serverModified); |
michael@0 | 894 | |
michael@0 | 895 | server.stop(run_next_test); |
michael@0 | 896 | }); |
michael@0 | 897 | }); |
michael@0 | 898 | |
michael@0 | 899 | add_test(function test_delete_collection_conditional_success() { |
michael@0 | 900 | _("Ensure conditional delete of collection works when it's supposed to."); |
michael@0 | 901 | |
michael@0 | 902 | let [server, client, username] = getServerAndClient(); |
michael@0 | 903 | let user = server.user(username); |
michael@0 | 904 | |
michael@0 | 905 | let now = Date.now(); |
michael@0 | 906 | |
michael@0 | 907 | let coll = user.createCollection("testcoll", { |
michael@0 | 908 | foo: new ServerBSO("foo", "payload0", now) |
michael@0 | 909 | }); |
michael@0 | 910 | |
michael@0 | 911 | let request = client.deleteCollection("testcoll"); |
michael@0 | 912 | request.locallyModifiedVersion = coll.timestamp; |
michael@0 | 913 | |
michael@0 | 914 | request.dispatch(function onComplete(error, req) { |
michael@0 | 915 | do_check_null(error); |
michael@0 | 916 | |
michael@0 | 917 | do_check_eq(user.collection("testcoll"), undefined); |
michael@0 | 918 | |
michael@0 | 919 | server.stop(run_next_test); |
michael@0 | 920 | }); |
michael@0 | 921 | }); |
michael@0 | 922 | |
michael@0 | 923 | add_test(function test_delete_collections() { |
michael@0 | 924 | _("Ensure deleteCollections() works."); |
michael@0 | 925 | |
michael@0 | 926 | let [server, client, username] = getServerAndClient(); |
michael@0 | 927 | let user = server.user(username); |
michael@0 | 928 | |
michael@0 | 929 | user.createCollection("testColl", { |
michael@0 | 930 | foo: new ServerBSO("foo", "payload0", Date.now()) |
michael@0 | 931 | }); |
michael@0 | 932 | |
michael@0 | 933 | let request = client.deleteCollections(); |
michael@0 | 934 | request.dispatch(function onComplete(error, req) { |
michael@0 | 935 | do_check_null(error); |
michael@0 | 936 | |
michael@0 | 937 | do_check_eq(user.collection("testcoll"), undefined); |
michael@0 | 938 | |
michael@0 | 939 | server.stop(run_next_test); |
michael@0 | 940 | }); |
michael@0 | 941 | }); |
michael@0 | 942 | |
michael@0 | 943 | add_test(function test_network_error_captured() { |
michael@0 | 944 | _("Ensure network errors are captured."); |
michael@0 | 945 | |
michael@0 | 946 | // Network errors should result in .networkError being set on request. |
michael@0 | 947 | let client = new StorageServiceClient("http://rnewman-is-splendid.badtld/"); |
michael@0 | 948 | |
michael@0 | 949 | let request = client.getCollectionInfo(); |
michael@0 | 950 | request.dispatch(function(error, req) { |
michael@0 | 951 | do_check_neq(error, null); |
michael@0 | 952 | do_check_neq(error.network, null); |
michael@0 | 953 | |
michael@0 | 954 | run_next_test(); |
michael@0 | 955 | }); |
michael@0 | 956 | }); |
michael@0 | 957 | |
michael@0 | 958 | add_test(function test_network_error_listener() { |
michael@0 | 959 | _("Ensure the onNetworkError listener is invoked on network errors."); |
michael@0 | 960 | |
michael@0 | 961 | let listenerCalled = false; |
michael@0 | 962 | |
michael@0 | 963 | let client = new StorageServiceClient("http://philikon-is-too.badtld/"); |
michael@0 | 964 | client.addListener({ |
michael@0 | 965 | onNetworkError: function(client, request) { |
michael@0 | 966 | listenerCalled = true; |
michael@0 | 967 | } |
michael@0 | 968 | }); |
michael@0 | 969 | let request = client.getCollectionInfo(); |
michael@0 | 970 | request.dispatch(function() { |
michael@0 | 971 | do_check_true(listenerCalled); |
michael@0 | 972 | run_next_test(); |
michael@0 | 973 | }); |
michael@0 | 974 | }); |
michael@0 | 975 | |
michael@0 | 976 | add_test(function test_batching_set_too_large() { |
michael@0 | 977 | _("Ensure we throw when attempting to add a BSO that is too large to fit."); |
michael@0 | 978 | |
michael@0 | 979 | let [server, client, username] = getServerAndClient(); |
michael@0 | 980 | |
michael@0 | 981 | let request = client.setBSOsBatching("testcoll"); |
michael@0 | 982 | let payload = ""; |
michael@0 | 983 | |
michael@0 | 984 | // The actual length of the payload is a little less. But, this ensures we |
michael@0 | 985 | // exceed it. |
michael@0 | 986 | for (let i = 0; i < client.REQUEST_SIZE_LIMIT; i++) { |
michael@0 | 987 | payload += i; |
michael@0 | 988 | } |
michael@0 | 989 | |
michael@0 | 990 | let bso = new BasicStorageObject("bso"); |
michael@0 | 991 | bso.payload = payload; |
michael@0 | 992 | do_check_throws(function add() { request.addBSO(bso); }); |
michael@0 | 993 | |
michael@0 | 994 | server.stop(run_next_test); |
michael@0 | 995 | }); |
michael@0 | 996 | |
michael@0 | 997 | add_test(function test_batching_set_basic() { |
michael@0 | 998 | _("Ensure batching set works with single requests."); |
michael@0 | 999 | |
michael@0 | 1000 | let [server, client, username] = getServerAndClient(); |
michael@0 | 1001 | |
michael@0 | 1002 | let request = client.setBSOsBatching("testcoll"); |
michael@0 | 1003 | for (let i = 0; i < 10; i++) { |
michael@0 | 1004 | let bso = new BasicStorageObject("bso" + i); |
michael@0 | 1005 | bso.payload = "payload" + i; |
michael@0 | 1006 | request.addBSO(bso); |
michael@0 | 1007 | } |
michael@0 | 1008 | |
michael@0 | 1009 | request.finish(function onFinish(request) { |
michael@0 | 1010 | do_check_eq(request.successfulIDs.length, 10); |
michael@0 | 1011 | |
michael@0 | 1012 | let collection = server.user(username).collection("testcoll"); |
michael@0 | 1013 | do_check_eq(collection.timestamp, request.serverModifiedVersion); |
michael@0 | 1014 | |
michael@0 | 1015 | server.stop(run_next_test); |
michael@0 | 1016 | }); |
michael@0 | 1017 | }); |
michael@0 | 1018 | |
michael@0 | 1019 | add_test(function test_batching_set_batch_count() { |
michael@0 | 1020 | _("Ensure multiple outgoing request batching works when count is exceeded."); |
michael@0 | 1021 | |
michael@0 | 1022 | let [server, client, username] = getServerAndClient(); |
michael@0 | 1023 | let requestCount = 0; |
michael@0 | 1024 | server.callback.onRequest = function onRequest() { |
michael@0 | 1025 | requestCount++; |
michael@0 | 1026 | } |
michael@0 | 1027 | |
michael@0 | 1028 | let request = client.setBSOsBatching("testcoll"); |
michael@0 | 1029 | for (let i = 1; i <= 300; i++) { |
michael@0 | 1030 | let bso = new BasicStorageObject("bso" + i); |
michael@0 | 1031 | bso.payload = "XXXXXXX"; |
michael@0 | 1032 | request.addBSO(bso); |
michael@0 | 1033 | } |
michael@0 | 1034 | |
michael@0 | 1035 | request.finish(function onFinish(request) { |
michael@0 | 1036 | do_check_eq(request.successfulIDs.length, 300); |
michael@0 | 1037 | do_check_eq(requestCount, 3); |
michael@0 | 1038 | |
michael@0 | 1039 | let collection = server.user(username).collection("testcoll"); |
michael@0 | 1040 | do_check_eq(collection.timestamp, request.serverModifiedVersion); |
michael@0 | 1041 | |
michael@0 | 1042 | server.stop(run_next_test); |
michael@0 | 1043 | }); |
michael@0 | 1044 | }); |
michael@0 | 1045 | |
michael@0 | 1046 | add_test(function test_batching_set_batch_size() { |
michael@0 | 1047 | _("Ensure outgoing requests batch when size is exceeded."); |
michael@0 | 1048 | |
michael@0 | 1049 | let [server, client, username] = getServerAndClient(); |
michael@0 | 1050 | let requestCount = 0; |
michael@0 | 1051 | server.callback.onRequest = function onRequest() { |
michael@0 | 1052 | requestCount++; |
michael@0 | 1053 | }; |
michael@0 | 1054 | |
michael@0 | 1055 | let limit = client.REQUEST_SIZE_LIMIT; |
michael@0 | 1056 | |
michael@0 | 1057 | let request = client.setBSOsBatching("testcoll"); |
michael@0 | 1058 | |
michael@0 | 1059 | // JavaScript: Y U NO EASY REPETITION FUNCTIONALITY? |
michael@0 | 1060 | let data = []; |
michael@0 | 1061 | for (let i = (limit / 2) - 100; i; i -= 1) { |
michael@0 | 1062 | data.push("X"); |
michael@0 | 1063 | } |
michael@0 | 1064 | |
michael@0 | 1065 | let payload = data.join(""); |
michael@0 | 1066 | |
michael@0 | 1067 | for (let i = 0; i < 4; i++) { |
michael@0 | 1068 | let bso = new BasicStorageObject("bso" + i); |
michael@0 | 1069 | bso.payload = payload; |
michael@0 | 1070 | request.addBSO(bso); |
michael@0 | 1071 | } |
michael@0 | 1072 | |
michael@0 | 1073 | request.finish(function onFinish(request) { |
michael@0 | 1074 | do_check_eq(request.successfulIDs.length, 4); |
michael@0 | 1075 | do_check_eq(requestCount, 2); |
michael@0 | 1076 | |
michael@0 | 1077 | let collection = server.user(username).collection("testcoll"); |
michael@0 | 1078 | do_check_eq(collection.timestamp, request.serverModifiedVersion); |
michael@0 | 1079 | |
michael@0 | 1080 | server.stop(run_next_test); |
michael@0 | 1081 | }); |
michael@0 | 1082 | }); |
michael@0 | 1083 | |
michael@0 | 1084 | add_test(function test_batching_set_flush() { |
michael@0 | 1085 | _("Ensure flushing batch sets works."); |
michael@0 | 1086 | |
michael@0 | 1087 | let [server, client, username] = getServerAndClient(); |
michael@0 | 1088 | |
michael@0 | 1089 | let requestCount = 0; |
michael@0 | 1090 | server.callback.onRequest = function onRequest() { |
michael@0 | 1091 | requestCount++; |
michael@0 | 1092 | } |
michael@0 | 1093 | |
michael@0 | 1094 | let request = client.setBSOsBatching("testcoll"); |
michael@0 | 1095 | for (let i = 1; i < 101; i++) { |
michael@0 | 1096 | let bso = new BasicStorageObject("bso" + i); |
michael@0 | 1097 | bso.payload = "foo"; |
michael@0 | 1098 | request.addBSO(bso); |
michael@0 | 1099 | |
michael@0 | 1100 | if (i % 10 == 0) { |
michael@0 | 1101 | request.flush(); |
michael@0 | 1102 | } |
michael@0 | 1103 | } |
michael@0 | 1104 | |
michael@0 | 1105 | request.finish(function onFinish(request) { |
michael@0 | 1106 | do_check_eq(request.successfulIDs.length, 100); |
michael@0 | 1107 | do_check_eq(requestCount, 10); |
michael@0 | 1108 | |
michael@0 | 1109 | let collection = server.user(username).collection("testcoll"); |
michael@0 | 1110 | do_check_eq(collection.timestamp, request.serverModifiedVersion); |
michael@0 | 1111 | |
michael@0 | 1112 | server.stop(run_next_test); |
michael@0 | 1113 | }); |
michael@0 | 1114 | }); |
michael@0 | 1115 | |
michael@0 | 1116 | add_test(function test_batching_set_conditional_success() { |
michael@0 | 1117 | _("Ensure conditional requests for batched sets work properly."); |
michael@0 | 1118 | |
michael@0 | 1119 | let [server, client, username] = getServerAndClient(); |
michael@0 | 1120 | |
michael@0 | 1121 | let collection = server.user(username).createCollection("testcoll"); |
michael@0 | 1122 | |
michael@0 | 1123 | let lastServerVersion = Date.now(); |
michael@0 | 1124 | collection.insertBSO(new ServerBSO("foo", "bar", lastServerVersion)); |
michael@0 | 1125 | collection.timestamp = lastServerVersion; |
michael@0 | 1126 | do_check_eq(collection.timestamp, lastServerVersion); |
michael@0 | 1127 | |
michael@0 | 1128 | let requestCount = 0; |
michael@0 | 1129 | server.callback.onRequest = function onRequest() { |
michael@0 | 1130 | requestCount++; |
michael@0 | 1131 | } |
michael@0 | 1132 | |
michael@0 | 1133 | let request = client.setBSOsBatching("testcoll"); |
michael@0 | 1134 | request.locallyModifiedVersion = collection.timestamp; |
michael@0 | 1135 | |
michael@0 | 1136 | for (let i = 1; i < 251; i++) { |
michael@0 | 1137 | let bso = new BasicStorageObject("bso" + i); |
michael@0 | 1138 | bso.payload = "foo" + i; |
michael@0 | 1139 | request.addBSO(bso); |
michael@0 | 1140 | } |
michael@0 | 1141 | |
michael@0 | 1142 | request.finish(function onFinish(request) { |
michael@0 | 1143 | do_check_eq(requestCount, 3); |
michael@0 | 1144 | |
michael@0 | 1145 | do_check_eq(collection.timestamp, request.serverModifiedVersion); |
michael@0 | 1146 | do_check_eq(collection.timestamp, request.locallyModifiedVersion); |
michael@0 | 1147 | |
michael@0 | 1148 | server.stop(run_next_test); |
michael@0 | 1149 | }); |
michael@0 | 1150 | }); |
michael@0 | 1151 | |
michael@0 | 1152 | add_test(function test_batching_set_initial_failure() { |
michael@0 | 1153 | _("Ensure that an initial request failure setting BSOs is handled properly."); |
michael@0 | 1154 | |
michael@0 | 1155 | let [server, client, username] = getServerAndClient(); |
michael@0 | 1156 | |
michael@0 | 1157 | let collection = server.user(username).createCollection("testcoll"); |
michael@0 | 1158 | collection.timestamp = Date.now(); |
michael@0 | 1159 | |
michael@0 | 1160 | let requestCount = 0; |
michael@0 | 1161 | server.callback.onRequest = function onRequest() { |
michael@0 | 1162 | requestCount++; |
michael@0 | 1163 | } |
michael@0 | 1164 | |
michael@0 | 1165 | let request = client.setBSOsBatching("testcoll"); |
michael@0 | 1166 | request.locallyModifiedVersion = collection.timestamp - 1; |
michael@0 | 1167 | |
michael@0 | 1168 | for (let i = 1; i < 250; i++) { |
michael@0 | 1169 | let bso = new BasicStorageObject("bso" + i); |
michael@0 | 1170 | bso.payload = "foo" + i; |
michael@0 | 1171 | request.addBSO(bso); |
michael@0 | 1172 | } |
michael@0 | 1173 | |
michael@0 | 1174 | request.finish(function onFinish(request) { |
michael@0 | 1175 | do_check_eq(requestCount, 1); |
michael@0 | 1176 | |
michael@0 | 1177 | do_check_eq(request.successfulIDs.length, 0); |
michael@0 | 1178 | do_check_eq(Object.keys(request.failures).length, 0); |
michael@0 | 1179 | |
michael@0 | 1180 | server.stop(run_next_test); |
michael@0 | 1181 | }); |
michael@0 | 1182 | }); |
michael@0 | 1183 | |
michael@0 | 1184 | add_test(function test_batching_set_subsequent_failure() { |
michael@0 | 1185 | _("Ensure a non-initial failure during batching set is handled properly."); |
michael@0 | 1186 | |
michael@0 | 1187 | let [server, client, username] = getServerAndClient(); |
michael@0 | 1188 | let collection = server.user(username).createCollection("testcoll"); |
michael@0 | 1189 | collection.timestamp = Date.now(); |
michael@0 | 1190 | |
michael@0 | 1191 | let requestCount = 0; |
michael@0 | 1192 | server.callback.onRequest = function onRequest() { |
michael@0 | 1193 | requestCount++; |
michael@0 | 1194 | |
michael@0 | 1195 | if (requestCount == 1) { |
michael@0 | 1196 | return; |
michael@0 | 1197 | } |
michael@0 | 1198 | |
michael@0 | 1199 | collection.timestamp++; |
michael@0 | 1200 | } |
michael@0 | 1201 | |
michael@0 | 1202 | let request = client.setBSOsBatching("testcoll"); |
michael@0 | 1203 | request.locallyModifiedVersion = collection.timestamp; |
michael@0 | 1204 | |
michael@0 | 1205 | for (let i = 0; i < 250; i++) { |
michael@0 | 1206 | let bso = new BasicStorageObject("bso" + i); |
michael@0 | 1207 | bso.payload = "foo" + i; |
michael@0 | 1208 | request.addBSO(bso); |
michael@0 | 1209 | } |
michael@0 | 1210 | |
michael@0 | 1211 | request.finish(function onFinish(request) { |
michael@0 | 1212 | do_check_eq(requestCount, 2); |
michael@0 | 1213 | do_check_eq(request.successfulIDs.length, 100); |
michael@0 | 1214 | do_check_eq(Object.keys(request.failures).length, 0); |
michael@0 | 1215 | |
michael@0 | 1216 | server.stop(run_next_test); |
michael@0 | 1217 | }); |
michael@0 | 1218 | }); |
michael@0 | 1219 | |
michael@0 | 1220 | function getBatchedDeleteData(collection="testcoll") { |
michael@0 | 1221 | let [server, client, username] = getServerAndClient(); |
michael@0 | 1222 | |
michael@0 | 1223 | let serverBSOs = {}; |
michael@0 | 1224 | for (let i = 1000; i; i -= 1) { |
michael@0 | 1225 | serverBSOs["bso" + i] = new ServerBSO("bso" + i, "payload" + i); |
michael@0 | 1226 | } |
michael@0 | 1227 | |
michael@0 | 1228 | let user = server.user(username); |
michael@0 | 1229 | user.createCollection(collection, serverBSOs); |
michael@0 | 1230 | |
michael@0 | 1231 | return [server, client, username, collection]; |
michael@0 | 1232 | } |
michael@0 | 1233 | |
michael@0 | 1234 | add_test(function test_batched_delete_single() { |
michael@0 | 1235 | _("Ensure batched delete with single request works."); |
michael@0 | 1236 | |
michael@0 | 1237 | let [server, client, username, collection] = getBatchedDeleteData(); |
michael@0 | 1238 | |
michael@0 | 1239 | let requestCount = 0; |
michael@0 | 1240 | server.callback.onRequest = function onRequest() { |
michael@0 | 1241 | requestCount += 1; |
michael@0 | 1242 | } |
michael@0 | 1243 | |
michael@0 | 1244 | let request = client.deleteBSOsBatching(collection); |
michael@0 | 1245 | for (let i = 1; i < 51; i += 1) { |
michael@0 | 1246 | request.addID("bso" + i); |
michael@0 | 1247 | } |
michael@0 | 1248 | |
michael@0 | 1249 | request.finish(function onFinish(request) { |
michael@0 | 1250 | do_check_eq(requestCount, 1); |
michael@0 | 1251 | do_check_eq(request.errors.length, 0); |
michael@0 | 1252 | |
michael@0 | 1253 | let coll = server.user(username).collection(collection); |
michael@0 | 1254 | do_check_eq(coll.count(), 950); |
michael@0 | 1255 | |
michael@0 | 1256 | do_check_eq(request.serverModifiedVersion, coll.timestamp); |
michael@0 | 1257 | |
michael@0 | 1258 | server.stop(run_next_test); |
michael@0 | 1259 | }); |
michael@0 | 1260 | }); |
michael@0 | 1261 | |
michael@0 | 1262 | add_test(function test_batched_delete_multiple() { |
michael@0 | 1263 | _("Ensure batched delete splits requests properly."); |
michael@0 | 1264 | |
michael@0 | 1265 | let [server, client, username, collection] = getBatchedDeleteData(); |
michael@0 | 1266 | |
michael@0 | 1267 | let requestCount = 0; |
michael@0 | 1268 | server.callback.onRequest = function onRequest() { |
michael@0 | 1269 | requestCount += 1; |
michael@0 | 1270 | } |
michael@0 | 1271 | |
michael@0 | 1272 | let request = client.deleteBSOsBatching(collection); |
michael@0 | 1273 | for (let i = 1; i < 251; i += 1) { |
michael@0 | 1274 | request.addID("bso" + i); |
michael@0 | 1275 | } |
michael@0 | 1276 | |
michael@0 | 1277 | request.finish(function onFinish(request) { |
michael@0 | 1278 | do_check_eq(requestCount, 3); |
michael@0 | 1279 | do_check_eq(request.errors.length, 0); |
michael@0 | 1280 | |
michael@0 | 1281 | let coll = server.user(username).collection(collection); |
michael@0 | 1282 | do_check_eq(coll.count(), 750); |
michael@0 | 1283 | |
michael@0 | 1284 | do_check_eq(request.serverModifiedVersion, coll.timestamp); |
michael@0 | 1285 | |
michael@0 | 1286 | server.stop(run_next_test); |
michael@0 | 1287 | }); |
michael@0 | 1288 | }); |
michael@0 | 1289 | |
michael@0 | 1290 | add_test(function test_batched_delete_conditional_success() { |
michael@0 | 1291 | _("Ensure conditional batched delete all work."); |
michael@0 | 1292 | |
michael@0 | 1293 | let [server, client, username, collection] = getBatchedDeleteData(); |
michael@0 | 1294 | |
michael@0 | 1295 | let requestCount = 0; |
michael@0 | 1296 | server.callback.onRequest = function onRequest() { |
michael@0 | 1297 | requestCount++; |
michael@0 | 1298 | } |
michael@0 | 1299 | |
michael@0 | 1300 | let serverCollection = server.user(username).collection(collection); |
michael@0 | 1301 | let initialTimestamp = serverCollection.timestamp; |
michael@0 | 1302 | |
michael@0 | 1303 | let request = client.deleteBSOsBatching(collection); |
michael@0 | 1304 | request.locallyModifiedVersion = initialTimestamp; |
michael@0 | 1305 | |
michael@0 | 1306 | for (let i = 1; i < 251; i += 1) { |
michael@0 | 1307 | request.addID("bso" + 1); |
michael@0 | 1308 | } |
michael@0 | 1309 | |
michael@0 | 1310 | request.finish(function onFinish(request) { |
michael@0 | 1311 | do_check_eq(requestCount, 3); |
michael@0 | 1312 | do_check_eq(request.errors.length, 0); |
michael@0 | 1313 | |
michael@0 | 1314 | do_check_true(request.locallyModifiedVersion > initialTimestamp); |
michael@0 | 1315 | |
michael@0 | 1316 | server.stop(run_next_test); |
michael@0 | 1317 | }); |
michael@0 | 1318 | }); |
michael@0 | 1319 | |
michael@0 | 1320 | add_test(function test_batched_delete_conditional_initial_failure() { |
michael@0 | 1321 | _("Ensure conditional batched delete failure on initial request works."); |
michael@0 | 1322 | |
michael@0 | 1323 | // The client needs to issue multiple requests but the first one was |
michael@0 | 1324 | // rejected. The client should only issue that initial request. |
michael@0 | 1325 | let [server, client, username, collection] = getBatchedDeleteData(); |
michael@0 | 1326 | |
michael@0 | 1327 | let requestCount = 0; |
michael@0 | 1328 | server.callback.onRequest = function onRequest() { |
michael@0 | 1329 | requestCount++; |
michael@0 | 1330 | } |
michael@0 | 1331 | |
michael@0 | 1332 | let serverCollection = server.user(username).collection(collection); |
michael@0 | 1333 | let request = client.deleteBSOsBatching(collection); |
michael@0 | 1334 | request.locallyModifiedVersion = serverCollection.timestamp - 1; |
michael@0 | 1335 | |
michael@0 | 1336 | for (let i = 1; i < 251; i += 1) { |
michael@0 | 1337 | request.addID("bso" + i); |
michael@0 | 1338 | } |
michael@0 | 1339 | |
michael@0 | 1340 | request.finish(function onFinish(request) { |
michael@0 | 1341 | do_check_eq(requestCount, 1); |
michael@0 | 1342 | do_check_eq(request.errors.length, 1); |
michael@0 | 1343 | |
michael@0 | 1344 | server.stop(run_next_test); |
michael@0 | 1345 | }); |
michael@0 | 1346 | }); |
michael@0 | 1347 | |
michael@0 | 1348 | add_test(function test_batched_delete_conditional_subsequent_failure() { |
michael@0 | 1349 | _("Ensure conditional batched delete failure on non-initial request."); |
michael@0 | 1350 | |
michael@0 | 1351 | let [server, client, username, collection] = getBatchedDeleteData(); |
michael@0 | 1352 | |
michael@0 | 1353 | let serverCollection = server.user(username).collection(collection); |
michael@0 | 1354 | |
michael@0 | 1355 | let requestCount = 0; |
michael@0 | 1356 | server.callback.onRequest = function onRequest() { |
michael@0 | 1357 | requestCount++; |
michael@0 | 1358 | |
michael@0 | 1359 | if (requestCount <= 1) { |
michael@0 | 1360 | return; |
michael@0 | 1361 | } |
michael@0 | 1362 | |
michael@0 | 1363 | // Advance collection's timestamp on subsequent requests so request is |
michael@0 | 1364 | // rejected. |
michael@0 | 1365 | serverCollection.timestamp++; |
michael@0 | 1366 | } |
michael@0 | 1367 | |
michael@0 | 1368 | let request = client.deleteBSOsBatching(collection); |
michael@0 | 1369 | request.locallyModifiedVersion = serverCollection.timestamp; |
michael@0 | 1370 | |
michael@0 | 1371 | for (let i = 1; i < 251; i += 1) { |
michael@0 | 1372 | request.addID("bso" + i); |
michael@0 | 1373 | } |
michael@0 | 1374 | |
michael@0 | 1375 | request.finish(function onFinish(request) { |
michael@0 | 1376 | do_check_eq(requestCount, 2); |
michael@0 | 1377 | do_check_eq(request.errors.length, 1); |
michael@0 | 1378 | |
michael@0 | 1379 | server.stop(run_next_test); |
michael@0 | 1380 | }); |
michael@0 | 1381 | }); |