services/common/tests/unit/test_bagheera_client.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 "use strict";
michael@0 5
michael@0 6 Cu.import("resource://services-common/bagheeraclient.js");
michael@0 7 Cu.import("resource://services-common/rest.js");
michael@0 8 Cu.import("resource://testing-common/services-common/bagheeraserver.js");
michael@0 9 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 10 Cu.import("resource://gre/modules/Task.jsm");
michael@0 11
michael@0 12 function getClientAndServer() {
michael@0 13 let server = new BagheeraServer();
michael@0 14 server.start();
michael@0 15
michael@0 16 let client = new BagheeraClient(server.serverURI);
michael@0 17
michael@0 18 return [client, server];
michael@0 19 }
michael@0 20
michael@0 21 function run_test() {
michael@0 22 initTestLogging("Trace");
michael@0 23 run_next_test();
michael@0 24 }
michael@0 25
michael@0 26 add_test(function test_constructor() {
michael@0 27 let client = new BagheeraClient("http://localhost:1234/");
michael@0 28
michael@0 29 run_next_test();
michael@0 30 });
michael@0 31
michael@0 32 add_test(function test_post_json_transport_failure() {
michael@0 33 let client = new BagheeraClient("http://localhost:1234/");
michael@0 34
michael@0 35 client.uploadJSON("foo", "bar", {}).then(function onResult(result) {
michael@0 36 do_check_false(result.transportSuccess);
michael@0 37
michael@0 38 run_next_test();
michael@0 39 });
michael@0 40 });
michael@0 41
michael@0 42 add_test(function test_post_json_simple() {
michael@0 43 let [client, server] = getClientAndServer();
michael@0 44
michael@0 45 server.createNamespace("foo");
michael@0 46 let promise = client.uploadJSON("foo", "bar", {foo: "bar", biz: "baz"});
michael@0 47
michael@0 48 promise.then(function onSuccess(result) {
michael@0 49 do_check_true(result instanceof BagheeraClientRequestResult);
michael@0 50 do_check_true(result.request instanceof RESTRequest);
michael@0 51 do_check_true(result.transportSuccess);
michael@0 52 do_check_true(result.serverSuccess);
michael@0 53
michael@0 54 server.stop(run_next_test);
michael@0 55 }, do_check_null);
michael@0 56 });
michael@0 57
michael@0 58 add_test(function test_post_json_bad_data() {
michael@0 59 let [client, server] = getClientAndServer();
michael@0 60
michael@0 61 server.createNamespace("foo");
michael@0 62
michael@0 63 client.uploadJSON("foo", "bar", "{this is invalid json}").then(
michael@0 64 function onResult(result) {
michael@0 65 do_check_true(result.transportSuccess);
michael@0 66 do_check_false(result.serverSuccess);
michael@0 67
michael@0 68 server.stop(run_next_test);
michael@0 69 });
michael@0 70 });
michael@0 71
michael@0 72 add_task(function test_post_delete_multiple_obsolete_documents () {
michael@0 73 let [client, server] = getClientAndServer();
michael@0 74 let namespace = "foo";
michael@0 75 let documents = [
michael@0 76 [namespace, "one", "{v:1}"],
michael@0 77 [namespace, "two", "{v:2}"],
michael@0 78 [namespace, "three", "{v:3}"],
michael@0 79 [namespace, "four", "{v:4}"],
michael@0 80 ];
michael@0 81
michael@0 82 try {
michael@0 83 // create initial documents
michael@0 84 server.createNamespace(namespace);
michael@0 85 for (let [ns, id, payload] of documents) {
michael@0 86 server.setDocument(ns, id, payload);
michael@0 87 do_check_true(server.hasDocument(ns, id));
michael@0 88 }
michael@0 89
michael@0 90 // Test uploading with deleting some documents.
michael@0 91 let deleteIDs = [0, 1].map((no) => { return documents[no][1]; });
michael@0 92 let result = yield client.uploadJSON(namespace, "new-1", {foo: "bar"}, {deleteIDs: deleteIDs});
michael@0 93 do_check_true(result.transportSuccess);
michael@0 94 do_check_true(result.serverSuccess);
michael@0 95 do_check_true(server.hasDocument(namespace, "new-1"));
michael@0 96 for (let id of deleteIDs) {
michael@0 97 do_check_false(server.hasDocument(namespace, id));
michael@0 98 }
michael@0 99 // Check if the documents that were not staged for deletion are still there.
michael@0 100 for (let [,id,] of documents) {
michael@0 101 if (deleteIDs.indexOf(id) == -1) {
michael@0 102 do_check_true(server.hasDocument(namespace, id));
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 // Test upload without deleting documents.
michael@0 107 let ids = Object.keys(server.namespaces[namespace]);
michael@0 108 result = yield client.uploadJSON(namespace, "new-2", {foo: "bar"});
michael@0 109 do_check_true(result.transportSuccess);
michael@0 110 do_check_true(result.serverSuccess);
michael@0 111 do_check_true(server.hasDocument(namespace, "new-2"));
michael@0 112 // Check to see if all the original documents are still there.
michael@0 113 for (let id of ids) {
michael@0 114 do_check_true(deleteIDs.indexOf(id) !== -1 || server.hasDocument(namespace, id));
michael@0 115 }
michael@0 116 } finally {
michael@0 117 let deferred = Promise.defer();
michael@0 118 server.stop(deferred.resolve.bind(deferred));
michael@0 119 yield deferred.promise;
michael@0 120 }
michael@0 121 });
michael@0 122
michael@0 123 add_test(function test_delete_document() {
michael@0 124 let [client, server] = getClientAndServer();
michael@0 125
michael@0 126 server.createNamespace("foo");
michael@0 127 server.setDocument("foo", "bar", "{}");
michael@0 128
michael@0 129 client.deleteDocument("foo", "bar").then(function onResult(result) {
michael@0 130 do_check_true(result.transportSuccess);
michael@0 131 do_check_true(result.serverSuccess);
michael@0 132
michael@0 133 do_check_null(server.getDocument("foo", "bar"));
michael@0 134
michael@0 135 server.stop(run_next_test);
michael@0 136 });
michael@0 137 });

mercurial