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