1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_service_cluster.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://services-sync/service.js"); 1.8 +Cu.import("resource://services-sync/util.js"); 1.9 +Cu.import("resource://testing-common/services/sync/utils.js"); 1.10 + 1.11 +function do_check_throws(func) { 1.12 + var raised = false; 1.13 + try { 1.14 + func(); 1.15 + } catch (ex) { 1.16 + raised = true; 1.17 + } 1.18 + do_check_true(raised); 1.19 +} 1.20 + 1.21 +add_test(function test_findCluster() { 1.22 + _("Test Service._findCluster()"); 1.23 + let server; 1.24 + ensureLegacyIdentityManager(); 1.25 + try { 1.26 + _("_findCluster() throws on network errors (e.g. connection refused)."); 1.27 + do_check_throws(function() { 1.28 + Service.serverURL = "http://dummy:9000/"; 1.29 + Service.identity.account = "johndoe"; 1.30 + Service._clusterManager._findCluster(); 1.31 + }); 1.32 + 1.33 + server = httpd_setup({ 1.34 + "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://weave.user.node/"), 1.35 + "/user/1.0/jimdoe/node/weave": httpd_handler(200, "OK", "null"), 1.36 + "/user/1.0/janedoe/node/weave": httpd_handler(404, "Not Found", "Not Found"), 1.37 + "/user/1.0/juliadoe/node/weave": httpd_handler(400, "Bad Request", "Bad Request"), 1.38 + "/user/1.0/joedoe/node/weave": httpd_handler(500, "Server Error", "Server Error") 1.39 + }); 1.40 + 1.41 + Service.serverURL = server.baseURI; 1.42 + Service.identity.account = "johndoe"; 1.43 + 1.44 + _("_findCluster() returns the user's cluster node"); 1.45 + let cluster = Service._clusterManager._findCluster(); 1.46 + do_check_eq(cluster, "http://weave.user.node/"); 1.47 + 1.48 + _("A 'null' response is converted to null."); 1.49 + Service.identity.account = "jimdoe"; 1.50 + cluster = Service._clusterManager._findCluster(); 1.51 + do_check_eq(cluster, null); 1.52 + 1.53 + _("If a 404 is encountered, the server URL is taken as the cluster URL"); 1.54 + Service.identity.account = "janedoe"; 1.55 + cluster = Service._clusterManager._findCluster(); 1.56 + do_check_eq(cluster, Service.serverURL); 1.57 + 1.58 + _("A 400 response will throw an error."); 1.59 + Service.identity.account = "juliadoe"; 1.60 + do_check_throws(function() { 1.61 + Service._clusterManager._findCluster(); 1.62 + }); 1.63 + 1.64 + _("Any other server response (e.g. 500) will throw an error."); 1.65 + Service.identity.account = "joedoe"; 1.66 + do_check_throws(function() { 1.67 + Service._clusterManager._findCluster(); 1.68 + }); 1.69 + 1.70 + } finally { 1.71 + Svc.Prefs.resetBranch(""); 1.72 + if (server) { 1.73 + server.stop(run_next_test); 1.74 + } 1.75 + } 1.76 +}); 1.77 + 1.78 +add_test(function test_setCluster() { 1.79 + _("Test Service._setCluster()"); 1.80 + let server = httpd_setup({ 1.81 + "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://weave.user.node/"), 1.82 + "/user/1.0/jimdoe/node/weave": httpd_handler(200, "OK", "null") 1.83 + }); 1.84 + try { 1.85 + Service.serverURL = server.baseURI; 1.86 + Service.identity.account = "johndoe"; 1.87 + 1.88 + _("Check initial state."); 1.89 + do_check_eq(Service.clusterURL, ""); 1.90 + 1.91 + _("Set the cluster URL."); 1.92 + do_check_true(Service._clusterManager.setCluster()); 1.93 + do_check_eq(Service.clusterURL, "http://weave.user.node/"); 1.94 + 1.95 + _("Setting it again won't make a difference if it's the same one."); 1.96 + do_check_false(Service._clusterManager.setCluster()); 1.97 + do_check_eq(Service.clusterURL, "http://weave.user.node/"); 1.98 + 1.99 + _("A 'null' response won't make a difference either."); 1.100 + Service.identity.account = "jimdoe"; 1.101 + do_check_false(Service._clusterManager.setCluster()); 1.102 + do_check_eq(Service.clusterURL, "http://weave.user.node/"); 1.103 + 1.104 + } finally { 1.105 + Svc.Prefs.resetBranch(""); 1.106 + server.stop(run_next_test); 1.107 + } 1.108 +}); 1.109 + 1.110 +function run_test() { 1.111 + initTestLogging(); 1.112 + run_next_test(); 1.113 +}