|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://services-sync/service.js"); |
|
5 Cu.import("resource://services-sync/util.js"); |
|
6 Cu.import("resource://testing-common/services/sync/utils.js"); |
|
7 |
|
8 function do_check_throws(func) { |
|
9 var raised = false; |
|
10 try { |
|
11 func(); |
|
12 } catch (ex) { |
|
13 raised = true; |
|
14 } |
|
15 do_check_true(raised); |
|
16 } |
|
17 |
|
18 add_test(function test_findCluster() { |
|
19 _("Test Service._findCluster()"); |
|
20 let server; |
|
21 ensureLegacyIdentityManager(); |
|
22 try { |
|
23 _("_findCluster() throws on network errors (e.g. connection refused)."); |
|
24 do_check_throws(function() { |
|
25 Service.serverURL = "http://dummy:9000/"; |
|
26 Service.identity.account = "johndoe"; |
|
27 Service._clusterManager._findCluster(); |
|
28 }); |
|
29 |
|
30 server = httpd_setup({ |
|
31 "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://weave.user.node/"), |
|
32 "/user/1.0/jimdoe/node/weave": httpd_handler(200, "OK", "null"), |
|
33 "/user/1.0/janedoe/node/weave": httpd_handler(404, "Not Found", "Not Found"), |
|
34 "/user/1.0/juliadoe/node/weave": httpd_handler(400, "Bad Request", "Bad Request"), |
|
35 "/user/1.0/joedoe/node/weave": httpd_handler(500, "Server Error", "Server Error") |
|
36 }); |
|
37 |
|
38 Service.serverURL = server.baseURI; |
|
39 Service.identity.account = "johndoe"; |
|
40 |
|
41 _("_findCluster() returns the user's cluster node"); |
|
42 let cluster = Service._clusterManager._findCluster(); |
|
43 do_check_eq(cluster, "http://weave.user.node/"); |
|
44 |
|
45 _("A 'null' response is converted to null."); |
|
46 Service.identity.account = "jimdoe"; |
|
47 cluster = Service._clusterManager._findCluster(); |
|
48 do_check_eq(cluster, null); |
|
49 |
|
50 _("If a 404 is encountered, the server URL is taken as the cluster URL"); |
|
51 Service.identity.account = "janedoe"; |
|
52 cluster = Service._clusterManager._findCluster(); |
|
53 do_check_eq(cluster, Service.serverURL); |
|
54 |
|
55 _("A 400 response will throw an error."); |
|
56 Service.identity.account = "juliadoe"; |
|
57 do_check_throws(function() { |
|
58 Service._clusterManager._findCluster(); |
|
59 }); |
|
60 |
|
61 _("Any other server response (e.g. 500) will throw an error."); |
|
62 Service.identity.account = "joedoe"; |
|
63 do_check_throws(function() { |
|
64 Service._clusterManager._findCluster(); |
|
65 }); |
|
66 |
|
67 } finally { |
|
68 Svc.Prefs.resetBranch(""); |
|
69 if (server) { |
|
70 server.stop(run_next_test); |
|
71 } |
|
72 } |
|
73 }); |
|
74 |
|
75 add_test(function test_setCluster() { |
|
76 _("Test Service._setCluster()"); |
|
77 let server = httpd_setup({ |
|
78 "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://weave.user.node/"), |
|
79 "/user/1.0/jimdoe/node/weave": httpd_handler(200, "OK", "null") |
|
80 }); |
|
81 try { |
|
82 Service.serverURL = server.baseURI; |
|
83 Service.identity.account = "johndoe"; |
|
84 |
|
85 _("Check initial state."); |
|
86 do_check_eq(Service.clusterURL, ""); |
|
87 |
|
88 _("Set the cluster URL."); |
|
89 do_check_true(Service._clusterManager.setCluster()); |
|
90 do_check_eq(Service.clusterURL, "http://weave.user.node/"); |
|
91 |
|
92 _("Setting it again won't make a difference if it's the same one."); |
|
93 do_check_false(Service._clusterManager.setCluster()); |
|
94 do_check_eq(Service.clusterURL, "http://weave.user.node/"); |
|
95 |
|
96 _("A 'null' response won't make a difference either."); |
|
97 Service.identity.account = "jimdoe"; |
|
98 do_check_false(Service._clusterManager.setCluster()); |
|
99 do_check_eq(Service.clusterURL, "http://weave.user.node/"); |
|
100 |
|
101 } finally { |
|
102 Svc.Prefs.resetBranch(""); |
|
103 server.stop(run_next_test); |
|
104 } |
|
105 }); |
|
106 |
|
107 function run_test() { |
|
108 initTestLogging(); |
|
109 run_next_test(); |
|
110 } |