mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 732152 - Implement standalone client for Storage Service 2.0; r=rnewman
This commit is contained in:
parent
8c8817fd0d
commit
51bddce270
@ -17,6 +17,7 @@ modules := \
|
||||
observers.js \
|
||||
preferences.js \
|
||||
rest.js \
|
||||
storageservice.js \
|
||||
stringbundle.js \
|
||||
tokenserverclient.js \
|
||||
utils.js \
|
||||
|
@ -8,4 +8,5 @@
|
||||
pref("services.common.log.logger.rest.request", "Debug");
|
||||
pref("services.common.log.logger.rest.response", "Debug");
|
||||
|
||||
pref("services.common.storageservice.sendVersionInfo", true);
|
||||
pref("services.common.tokenserverclient.logger.level", "Info");
|
||||
|
@ -15,7 +15,7 @@ const EXPORTED_SYMBOLS = [
|
||||
"StorageServerCallback",
|
||||
"StorageServerCollection",
|
||||
"StorageServer",
|
||||
|
||||
"storageServerForUsers",
|
||||
];
|
||||
|
||||
Cu.import("resource://testing-common/httpd.js");
|
||||
|
1795
services/common/storageservice.js
Normal file
1795
services/common/storageservice.js
Normal file
File diff suppressed because it is too large
Load Diff
@ -6,6 +6,7 @@ const modules = [
|
||||
"log4moz.js",
|
||||
"preferences.js",
|
||||
"rest.js",
|
||||
"storageservice.js",
|
||||
"stringbundle.js",
|
||||
"tokenserverclient.js",
|
||||
"utils.js",
|
||||
|
124
services/common/tests/unit/test_storageservice_bso.js
Normal file
124
services/common/tests/unit/test_storageservice_bso.js
Normal file
@ -0,0 +1,124 @@
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
Cu.import("resource://services-common/storageservice.js");
|
||||
|
||||
function run_test() {
|
||||
initTestLogging("Trace");
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
add_test(function test_bso_constructor() {
|
||||
_("Ensure created BSO instances are initialized properly.");
|
||||
|
||||
let bso = new BasicStorageObject();
|
||||
do_check_eq(bso.id, null);
|
||||
do_check_eq(bso.collection, null);
|
||||
do_check_attribute_count(bso.data, 0);
|
||||
do_check_eq(bso.payload, null);
|
||||
do_check_eq(bso.modified, null);
|
||||
do_check_eq(bso.sortindex, 0);
|
||||
do_check_eq(bso.ttl, null);
|
||||
|
||||
let bso = new BasicStorageObject("foobar");
|
||||
do_check_eq(bso.id, "foobar");
|
||||
do_check_eq(bso.collection, null);
|
||||
do_check_attribute_count(bso.data, 0);
|
||||
|
||||
let bso = new BasicStorageObject("foo", "coll");
|
||||
do_check_eq(bso.id, "foo");
|
||||
do_check_eq(bso.collection, "coll");
|
||||
do_check_attribute_count(bso.data, 0);
|
||||
|
||||
run_next_test();
|
||||
});
|
||||
|
||||
add_test(function test_bso_attributes() {
|
||||
_("Ensure attribute getters and setters work.");
|
||||
|
||||
let bso = new BasicStorageObject("foobar");
|
||||
bso.payload = "pay";
|
||||
do_check_eq(bso.payload, "pay");
|
||||
|
||||
bso.modified = 35423;
|
||||
do_check_eq(bso.modified, 35423);
|
||||
|
||||
bso.sortindex = 10;
|
||||
do_check_eq(bso.sortindex, 10);
|
||||
|
||||
bso.ttl = 60;
|
||||
do_check_eq(bso.ttl, 60);
|
||||
|
||||
run_next_test();
|
||||
});
|
||||
|
||||
add_test(function test_bso_deserialize() {
|
||||
_("Ensure that deserialize() works.");
|
||||
|
||||
_("A simple working test.");
|
||||
let json = '{"id": "foobar", "payload": "pay", "modified": 1223145532}';
|
||||
let bso = new BasicStorageObject();
|
||||
bso.deserialize(json);
|
||||
do_check_neq(bso, null);
|
||||
do_check_eq(bso.id, "foobar");
|
||||
do_check_eq(bso.payload, "pay");
|
||||
do_check_eq(bso.modified, 1223145532);
|
||||
|
||||
_("Invalid JSON.");
|
||||
let json = '{id: "foobar}';
|
||||
let bso = new BasicStorageObject();
|
||||
try {
|
||||
bso.deserialize(json);
|
||||
do_check_true(false);
|
||||
} catch (ex) {
|
||||
do_check_eq(ex.name, "SyntaxError");
|
||||
}
|
||||
|
||||
_("Invalid key in JSON.");
|
||||
let json = '{"id": "foo", "payload": "pay", "BADKEY": "irrelevant"}';
|
||||
let bso = new BasicStorageObject();
|
||||
try {
|
||||
bso.deserialize(json);
|
||||
do_check_true(false);
|
||||
} catch (ex) {
|
||||
do_check_eq(ex.name, "Error");
|
||||
do_check_eq(ex.message.indexOf("Invalid key"), 0);
|
||||
}
|
||||
|
||||
_("Loading native JS objects works.");
|
||||
let bso = new BasicStorageObject();
|
||||
bso.deserialize({id: "foo", payload: "pay"});
|
||||
do_check_neq(bso, null);
|
||||
do_check_eq(bso.id, "foo");
|
||||
do_check_eq(bso.payload, "pay");
|
||||
|
||||
_("Passing invalid type is caught.");
|
||||
let bso = new BasicStorageObject();
|
||||
try {
|
||||
bso.deserialize(["foo", "bar"]);
|
||||
do_check_true(false);
|
||||
} catch (ex) {
|
||||
do_check_eq(ex.name, "Error");
|
||||
}
|
||||
|
||||
run_next_test();
|
||||
});
|
||||
|
||||
add_test(function test_bso_toJSON() {
|
||||
_("Ensure JSON serialization works.");
|
||||
|
||||
let bso = new BasicStorageObject();
|
||||
do_check_attribute_count(bso.toJSON(), 0);
|
||||
|
||||
bso.id = "foo";
|
||||
bso.payload = "pay";
|
||||
let json = bso.toJSON();
|
||||
let original = json;
|
||||
|
||||
do_check_attribute_count(original, 2);
|
||||
do_check_eq(original.id, "foo");
|
||||
do_check_eq(original.payload, "pay");
|
||||
|
||||
run_next_test();
|
||||
});
|
939
services/common/tests/unit/test_storageservice_client.js
Normal file
939
services/common/tests/unit/test_storageservice_client.js
Normal file
@ -0,0 +1,939 @@
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
Cu.import("resource://services-common/storageservice.js");
|
||||
Cu.import("resource://testing-common/services-common/storageserver.js");
|
||||
|
||||
const BASE_URI = "http://localhost:8080/2.0";
|
||||
|
||||
function run_test() {
|
||||
initTestLogging("Trace");
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function getEmptyServer(user="765", password="password") {
|
||||
let users = {};
|
||||
users[user] = password;
|
||||
|
||||
return storageServerForUsers(users, {
|
||||
meta: {},
|
||||
clients: {},
|
||||
crypto: {},
|
||||
});
|
||||
}
|
||||
|
||||
function getClient(user="765", password="password") {
|
||||
let client = new StorageServiceClient(BASE_URI + "/" + user);
|
||||
client.addListener({
|
||||
onDispatch: function onDispatch(request) {
|
||||
let up = user + ":" + password;
|
||||
request.request.setHeader("authorization", "Basic " + btoa(up));
|
||||
}
|
||||
});
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
function getServerAndClient(user="765", password="password") {
|
||||
let server = getEmptyServer(user, password);
|
||||
let client = getClient(user, password);
|
||||
|
||||
return [server, client, user, password];
|
||||
}
|
||||
|
||||
add_test(function test_auth_failure_listener() {
|
||||
_("Ensure the onAuthFailure listener is invoked.");
|
||||
|
||||
let server = getEmptyServer();
|
||||
let client = getClient("324", "INVALID");
|
||||
client.addListener({
|
||||
onAuthFailure: function onAuthFailure(client, request) {
|
||||
_("onAuthFailure");
|
||||
server.stop(run_next_test);
|
||||
}
|
||||
});
|
||||
|
||||
let request = client.getCollectionInfo();
|
||||
request.dispatch();
|
||||
});
|
||||
|
||||
add_test(function test_duplicate_listeners() {
|
||||
_("Ensure that duplicate listeners aren't installed multiple times.");
|
||||
|
||||
let server = getEmptyServer();
|
||||
let client = getClient("1234567", "BAD_PASSWORD");
|
||||
|
||||
let invokeCount = 0;
|
||||
let listener = {
|
||||
onAuthFailure: function onAuthFailure() {
|
||||
invokeCount++;
|
||||
}
|
||||
};
|
||||
|
||||
client.addListener(listener);
|
||||
// No error expected.
|
||||
client.addListener(listener);
|
||||
|
||||
let request = client.getCollectionInfo();
|
||||
request.dispatch(function onComplete() {
|
||||
do_check_eq(invokeCount, 1);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_handler_object() {
|
||||
_("Ensure that installed handlers get their callbacks invoked.");
|
||||
|
||||
let [server, client] = getServerAndClient();
|
||||
|
||||
let onCompleteCount = 0;
|
||||
let onDispatchCount = 0;
|
||||
|
||||
let handler = {
|
||||
onComplete: function onComplete() {
|
||||
onCompleteCount++;
|
||||
|
||||
do_check_eq(onDispatchCount, 1);
|
||||
do_check_eq(onCompleteCount, 1);
|
||||
|
||||
server.stop(run_next_test);
|
||||
},
|
||||
|
||||
onDispatch: function onDispatch() {
|
||||
onDispatchCount++;
|
||||
},
|
||||
};
|
||||
let request = client.getCollectionInfo();
|
||||
request.handler = handler;
|
||||
request.dispatch();
|
||||
});
|
||||
|
||||
add_test(function test_info_collections() {
|
||||
_("Ensure requests to /info/collections work as expected.");
|
||||
|
||||
let [server, client] = getServerAndClient();
|
||||
|
||||
let request = client.getCollectionInfo();
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_eq("object", typeof req.resultObj);
|
||||
do_check_attribute_count(req.resultObj, 3);
|
||||
do_check_true("meta" in req.resultObj);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_info_collections_conditional_not_modified() {
|
||||
_("Ensure conditional getCollectionInfo requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getCollectionInfo();
|
||||
request.locallyModifiedVersion = now + 10;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_true(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_info_collections_conditional_modified() {
|
||||
_("Ensure conditional getCollectionInfo requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getCollectionInfo();
|
||||
request.locallyModifiedVersion = now - 10;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_false(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_quota() {
|
||||
_("Ensure quota requests work.");
|
||||
|
||||
let [server, client] = getServerAndClient();
|
||||
|
||||
let request = client.getQuota();
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_eq(req.resultObj.quota, 1048576);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_quota_conditional_not_modified() {
|
||||
_("Ensure conditional getQuota requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getQuota();
|
||||
request.locallyModifiedVersion = now + 10;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_true(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_quota_conditional_modified() {
|
||||
_("Ensure conditional getQuota requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getQuota();
|
||||
request.locallyModifiedVersion = now - 10;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_false(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_collection_usage() {
|
||||
_("Ensure info/collection_usage requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
abc123: new ServerBSO("abc123", "payload", Date.now())
|
||||
});
|
||||
|
||||
let request = client.getCollectionUsage();
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
let usage = req.resultObj;
|
||||
do_check_true("testcoll" in usage);
|
||||
do_check_eq(usage.testcoll, "payload".length);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_usage_conditional_not_modified() {
|
||||
_("Ensure conditional getCollectionUsage requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getCollectionUsage();
|
||||
request.locallyModifiedVersion = now + 10;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_true(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_usage_conditional_modified() {
|
||||
_("Ensure conditional getCollectionUsage requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getCollectionUsage();
|
||||
request.locallyModifiedVersion = now - 10;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_false(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_collection_counts() {
|
||||
_("Ensure info/collection_counts requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", Date.now()),
|
||||
bar: new ServerBSO("bar", "payload1", Date.now())
|
||||
});
|
||||
|
||||
let request = client.getCollectionCounts();
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
let counts = req.resultObj;
|
||||
do_check_true("testcoll" in counts);
|
||||
do_check_eq(counts.testcoll, 2);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_counts_conditional_not_modified() {
|
||||
_("Ensure conditional getCollectionCounts requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getCollectionCounts();
|
||||
request.locallyModifiedVersion = now + 10;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_true(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_counts_conditional_modified() {
|
||||
_("Ensure conditional getCollectionCounts requests work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getCollectionCounts();
|
||||
request.locallyModifiedVersion = now - 10;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_false(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_collection_simple() {
|
||||
_("Ensure basic collection retrieval works.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", Date.now()),
|
||||
bar: new ServerBSO("bar", "payload1", Date.now())
|
||||
});
|
||||
|
||||
let request = client.getCollection("testcoll");
|
||||
let bsos = [];
|
||||
request.handler = {
|
||||
onBSORecord: function onBSORecord(request, bso) {
|
||||
bsos.push(bso);
|
||||
},
|
||||
|
||||
onComplete: function onComplete(error, request) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_eq(bsos.length, 2);
|
||||
do_check_eq(bsos[0], "foo");
|
||||
do_check_eq(bsos[1], "bar");
|
||||
|
||||
server.stop(run_next_test);
|
||||
}
|
||||
};
|
||||
request.dispatch();
|
||||
});
|
||||
|
||||
add_test(function test_get_collection_conditional_not_modified() {
|
||||
_("Ensure conditional requests with no new data to getCollection work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now)
|
||||
});
|
||||
|
||||
let request = client.getCollection("testcoll");
|
||||
request.locallyModifiedVersion = now + 1;
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_true(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_get_collection_conditional_modified() {
|
||||
_("Ensure conditional requests with new data to getCollection work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now)
|
||||
});
|
||||
|
||||
let request = client.getCollection("testcoll");
|
||||
request.locallyModifiedVersion = now - 1;
|
||||
|
||||
let bsoCount = 0;
|
||||
request.handler = {
|
||||
onBSORecord: function onBSORecord() {
|
||||
bsoCount++;
|
||||
},
|
||||
|
||||
onComplete: function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_false(req.notModified);
|
||||
do_check_eq(bsoCount, 1);
|
||||
|
||||
server.stop(run_next_test);
|
||||
}
|
||||
};
|
||||
request.dispatch();
|
||||
});
|
||||
|
||||
// This is effectively a sanity test for query string generation.
|
||||
add_test(function test_get_collection_newer() {
|
||||
_("Ensure query string for newer and full work together.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
|
||||
let date0 = Date.now();
|
||||
let date1 = date0 + 500;
|
||||
|
||||
let user = server.user(username);
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", date0),
|
||||
bar: new ServerBSO("bar", "payload1", date1)
|
||||
});
|
||||
|
||||
let request = client.getCollection("testcoll");
|
||||
request.full = true;
|
||||
request.newer = date0;
|
||||
|
||||
let bsos = [];
|
||||
request.handler = {
|
||||
onBSORecord: function onBSORecord(request, bso) {
|
||||
bsos.push(bso);
|
||||
},
|
||||
|
||||
onComplete: function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_eq(bsos.length, 1);
|
||||
let bso = bsos[0];
|
||||
|
||||
do_check_eq(bso.id, "bar");
|
||||
do_check_eq(bso.payload, "payload1");
|
||||
|
||||
server.stop(run_next_test);
|
||||
}
|
||||
};
|
||||
request.dispatch();
|
||||
});
|
||||
|
||||
add_test(function test_get_bso() {
|
||||
_("Ensure that simple BSO fetches work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
|
||||
server.createCollection(username, "testcoll", {
|
||||
abc123: new ServerBSO("abc123", "payload", Date.now())
|
||||
});
|
||||
|
||||
let request = client.getBSO("testcoll", "abc123");
|
||||
request.dispatch(function(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_true(req.resultObj instanceof BasicStorageObject);
|
||||
|
||||
let bso = req.resultObj;
|
||||
do_check_eq(bso.id, "abc123");
|
||||
do_check_eq(bso.payload, "payload");
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_bso_conditional() {
|
||||
_("Ensure conditional requests for an individual BSO work.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
|
||||
let user = server.user(username);
|
||||
let now = Date.now();
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload", now)
|
||||
});
|
||||
|
||||
let request = client.getBSO("testcoll", "foo");
|
||||
request.locallyModifiedVersion = now;
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_true(req.notModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_set_bso() {
|
||||
_("Ensure simple BSO PUT works.");
|
||||
|
||||
let [server, client] = getServerAndClient();
|
||||
|
||||
let id = "mnas08h3f3r2351";
|
||||
|
||||
let bso = new BasicStorageObject(id, "testcoll");
|
||||
bso.payload = "my test payload";
|
||||
|
||||
let request = client.setBSO(bso);
|
||||
request.dispatch(function(error, req) {
|
||||
do_check_eq(error, null);
|
||||
do_check_eq(req.resultObj, null);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
add_test(function test_set_bso_conditional() {
|
||||
_("Ensure conditional setting a BSO is properly rejected.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now + 1000)
|
||||
});
|
||||
|
||||
// Should get an mtime newer than server's.
|
||||
let bso = new BasicStorageObject("foo", "testcoll");
|
||||
bso.payload = "payload1";
|
||||
|
||||
let request = client.setBSO(bso);
|
||||
request.locallyModifiedVersion = now;
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_true(error instanceof StorageServiceRequestError);
|
||||
do_check_true(error.serverModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_set_bso_argument_errors() {
|
||||
_("Ensure BSO set detects invalid arguments.");
|
||||
|
||||
let bso = new BasicStorageObject();
|
||||
let client = getClient();
|
||||
|
||||
let threw = false;
|
||||
try {
|
||||
client.setBSO(bso);
|
||||
} catch (ex) {
|
||||
threw = true;
|
||||
do_check_eq(ex.name, "Error");
|
||||
do_check_neq(ex.message.indexOf("does not have collection defined"), -1);
|
||||
} finally {
|
||||
do_check_true(threw);
|
||||
threw = false;
|
||||
}
|
||||
|
||||
bso = new BasicStorageObject("id");
|
||||
try {
|
||||
client.setBSO(bso);
|
||||
} catch (ex) {
|
||||
threw = true;
|
||||
do_check_eq(ex.name, "Error");
|
||||
do_check_neq(ex.message.indexOf("does not have collection defined"), -1);
|
||||
} finally {
|
||||
do_check_true(threw);
|
||||
threw = false;
|
||||
}
|
||||
|
||||
bso = new BasicStorageObject(null, "coll");
|
||||
try {
|
||||
client.setBSO(bso);
|
||||
} catch (ex) {
|
||||
threw = true;
|
||||
do_check_eq(ex.name, "Error");
|
||||
do_check_neq(ex.message.indexOf("does not have ID defined"), -1);
|
||||
} finally {
|
||||
do_check_true(threw);
|
||||
threw = false;
|
||||
}
|
||||
|
||||
run_next_test();
|
||||
});
|
||||
|
||||
add_test(function test_set_bsos_simple() {
|
||||
_("Ensure setBSOs with basic options works.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let bso0 = new BasicStorageObject("foo");
|
||||
bso0.payload = "payload0";
|
||||
|
||||
let bso1 = new BasicStorageObject("bar");
|
||||
bso1.payload = "payload1";
|
||||
|
||||
let request = client.setBSOs("testcollection");
|
||||
request.addBSO(bso0);
|
||||
request.addBSO(bso1);
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
let successful = req.successfulIDs;
|
||||
do_check_eq(successful.size(), 2);
|
||||
do_check_true(successful.has(bso0.id));
|
||||
do_check_true(successful.has(bso1.id));
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_set_bsos_invalid_bso() {
|
||||
_("Ensure that adding an invalid BSO throws.");
|
||||
|
||||
let client = getClient();
|
||||
let request = client.setBSOs("testcoll");
|
||||
|
||||
let threw = false;
|
||||
|
||||
// Empty argument is invalid.
|
||||
try {
|
||||
request.addBSO(null);
|
||||
} catch (ex) {
|
||||
threw = true;
|
||||
} finally {
|
||||
do_check_true(threw);
|
||||
threw = false;
|
||||
}
|
||||
|
||||
try {
|
||||
let bso = new BasicStorageObject();
|
||||
request.addBSO(bso);
|
||||
} catch (ex) {
|
||||
threw = true;
|
||||
} finally {
|
||||
do_check_true(threw);
|
||||
threw = false;
|
||||
}
|
||||
|
||||
run_next_test();
|
||||
});
|
||||
|
||||
add_test(function test_delete_bso_simple() {
|
||||
_("Ensure deletion of individual BSOs works.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
|
||||
let user = server.user(username);
|
||||
let coll = user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", Date.now()),
|
||||
bar: new ServerBSO("bar", "payload1", Date.now())
|
||||
});
|
||||
|
||||
let request = client.deleteBSO("testcoll", "foo");
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_eq(req.statusCode, 204);
|
||||
|
||||
do_check_eq(coll.count(), 1);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_bso_conditional_failed() {
|
||||
_("Ensure deletion of an individual BSO with older modification fails.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now)
|
||||
});
|
||||
|
||||
let request = client.deleteBSO("testcoll", "foo");
|
||||
request.locallyModifiedVersion = now - 10;
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_true(error instanceof StorageServiceRequestError);
|
||||
do_check_true(error.serverModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_bso_conditional_success() {
|
||||
_("Ensure deletion of an individual BSO with newer modification works.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now)
|
||||
});
|
||||
|
||||
let request = client.deleteBSO("testcoll", "foo");
|
||||
request.locallyModifiedVersion = now;
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_eq(req.statusCode, 204);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_bsos_simple() {
|
||||
_("Ensure deletion of multiple BSOs works.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let coll = user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", Date.now()),
|
||||
bar: new ServerBSO("bar", "payload1", Date.now()),
|
||||
baz: new ServerBSO("baz", "payload2", Date.now())
|
||||
});
|
||||
|
||||
let request = client.deleteBSOs("testcoll", ["foo", "baz"]);
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
do_check_eq(req.statusCode, 204);
|
||||
|
||||
do_check_eq(coll.count(), 1);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_bsos_conditional_failed() {
|
||||
_("Ensure deletion of BSOs with server modifications fails.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now)
|
||||
});
|
||||
|
||||
let request = client.deleteBSOs("testcoll", ["foo"]);
|
||||
request.locallyModifiedVersion = now - 10;
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_true(error instanceof StorageServiceRequestError);
|
||||
do_check_true(error.serverModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_bsos_conditional_success() {
|
||||
_("Ensure conditional deletion of BSOs without server modifications works.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now),
|
||||
bar: new ServerBSO("bar", "payload1", now - 10)
|
||||
});
|
||||
|
||||
let request = client.deleteBSOs("testcoll", ["bar"]);
|
||||
request.locallyModifiedVersion = now + 10;
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_eq(req.statusCode, 204);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_collection() {
|
||||
_("Ensure deleteCollection() works.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", Date.now())
|
||||
});
|
||||
|
||||
let request = client.deleteCollection("testcoll");
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_eq(user.collection("testcoll", undefined));
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_collection_conditional_failed() {
|
||||
_("Ensure conditional deletes with server modifications fail.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now)
|
||||
});
|
||||
|
||||
let request = client.deleteCollection("testcoll");
|
||||
request.locallyModifiedVersion = now - 10;
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_true(error instanceof StorageServiceRequestError);
|
||||
do_check_true(error.serverModified);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_collection_conditional_success() {
|
||||
_("Ensure conditional delete of collection works when it's supposed to.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
user.createCollection("testcoll", {
|
||||
foo: new ServerBSO("foo", "payload0", now)
|
||||
});
|
||||
|
||||
let request = client.deleteCollection("testcoll");
|
||||
request.locallyModifiedVersion = now;
|
||||
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_eq(user.collection("testcoll"), undefined);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_delete_collections() {
|
||||
_("Ensure deleteCollections() works.");
|
||||
|
||||
let [server, client, username] = getServerAndClient();
|
||||
let user = server.user(username);
|
||||
|
||||
user.createCollection("testColl", {
|
||||
foo: new ServerBSO("foo", "payload0", Date.now())
|
||||
});
|
||||
|
||||
let request = client.deleteCollections();
|
||||
request.dispatch(function onComplete(error, req) {
|
||||
do_check_null(error);
|
||||
|
||||
do_check_eq(user.collection("testcoll"), undefined);
|
||||
|
||||
server.stop(run_next_test);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_network_error_captured() {
|
||||
_("Ensure network errors are captured.");
|
||||
|
||||
// Network errors should result in .networkError being set on request.
|
||||
let client = new StorageServiceClient("http://rnewman-is-splendid.badtld/");
|
||||
|
||||
let request = client.getCollectionInfo();
|
||||
request.dispatch(function(error, req) {
|
||||
do_check_neq(error, null);
|
||||
do_check_neq(error.network, null);
|
||||
|
||||
run_next_test();
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_network_error_listener() {
|
||||
_("Ensure the onNetworkError listener is invoked on network errors.");
|
||||
|
||||
let listenerCalled = false;
|
||||
|
||||
let client = new StorageServiceClient("http://philikon-is-too.badtld/");
|
||||
client.addListener({
|
||||
onNetworkError: function(client, request) {
|
||||
listenerCalled = true;
|
||||
}
|
||||
});
|
||||
let request = client.getCollectionInfo();
|
||||
request.dispatch(function() {
|
||||
do_check_true(listenerCalled);
|
||||
run_next_test();
|
||||
});
|
||||
});
|
@ -22,6 +22,11 @@ tail =
|
||||
[test_preferences.js]
|
||||
[test_restrequest.js]
|
||||
[test_tokenauthenticatedrequest.js]
|
||||
|
||||
# Storage service APIs
|
||||
[test_storageservice_bso.js]
|
||||
[test_storageservice_client.js]
|
||||
|
||||
[test_tokenserverclient.js]
|
||||
|
||||
[test_storage_server.js]
|
||||
|
Loading…
Reference in New Issue
Block a user