services/sync/tests/unit/test_service_getStorageInfo.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_service_getStorageInfo.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     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-common/rest.js");
     1.8 +Cu.import("resource://services-sync/constants.js");
     1.9 +Cu.import("resource://services-sync/service.js");
    1.10 +Cu.import("resource://services-sync/util.js");
    1.11 +Cu.import("resource://testing-common/services/sync/utils.js");
    1.12 +
    1.13 +let collections = {steam:  65.11328,
    1.14 +                   petrol: 82.488281,
    1.15 +                   diesel: 2.25488281};
    1.16 +
    1.17 +function run_test() {
    1.18 +  Log.repository.getLogger("Sync.Service").level = Log.Level.Trace;
    1.19 +  Log.repository.getLogger("Sync.StorageRequest").level = Log.Level.Trace;
    1.20 +  initTestLogging();
    1.21 +
    1.22 +  ensureLegacyIdentityManager();
    1.23 +  setBasicCredentials("johndoe", "ilovejane");
    1.24 +
    1.25 +  run_next_test();
    1.26 +}
    1.27 +
    1.28 +add_test(function test_success() {
    1.29 +  let handler = httpd_handler(200, "OK", JSON.stringify(collections));
    1.30 +  let server = httpd_setup({"/1.1/johndoe/info/collections": handler});
    1.31 +  Service.serverURL = server.baseURI + "/";
    1.32 +  Service.clusterURL = server.baseURI + "/";
    1.33 +
    1.34 +  let request = Service.getStorageInfo("collections", function (error, info) {
    1.35 +    do_check_eq(error, null);
    1.36 +    do_check_true(Utils.deepEquals(info, collections));
    1.37 +
    1.38 +    // Ensure that the request is sent off with the right bits.
    1.39 +    do_check_true(basic_auth_matches(handler.request,
    1.40 +                                     Service.identity.username,
    1.41 +                                     Service.identity.basicPassword));
    1.42 +    let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
    1.43 +                     " FxSync/" + WEAVE_VERSION + "." +
    1.44 +                     Services.appinfo.appBuildID + ".desktop";
    1.45 +    do_check_eq(handler.request.getHeader("User-Agent"), expectedUA);
    1.46 +
    1.47 +    server.stop(run_next_test);
    1.48 +  });
    1.49 +  do_check_true(request instanceof RESTRequest);
    1.50 +});
    1.51 +
    1.52 +add_test(function test_invalid_type() {
    1.53 +  do_check_throws(function () {
    1.54 +    Service.getStorageInfo("invalid", function (error, info) {
    1.55 +      do_throw("Shouldn't get here!");
    1.56 +    });
    1.57 +  });
    1.58 +  run_next_test();
    1.59 +});
    1.60 +
    1.61 +add_test(function test_network_error() {
    1.62 +  Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) {
    1.63 +    do_check_eq(error.result, Cr.NS_ERROR_CONNECTION_REFUSED);
    1.64 +    do_check_eq(info, null);
    1.65 +    run_next_test();
    1.66 +  });
    1.67 +});
    1.68 +
    1.69 +add_test(function test_http_error() {
    1.70 +  let handler = httpd_handler(500, "Oh noez", "Something went wrong!");
    1.71 +  let server = httpd_setup({"/1.1/johndoe/info/collections": handler});
    1.72 +  Service.serverURL = server.baseURI + "/";
    1.73 +  Service.clusterURL = server.baseURI + "/";
    1.74 +
    1.75 +  let request = Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) {
    1.76 +    do_check_eq(error.status, 500);
    1.77 +    do_check_eq(info, null);
    1.78 +    server.stop(run_next_test);
    1.79 +  });
    1.80 +});
    1.81 +
    1.82 +add_test(function test_invalid_json() {
    1.83 +  let handler = httpd_handler(200, "OK", "Invalid JSON");
    1.84 +  let server = httpd_setup({"/1.1/johndoe/info/collections": handler});
    1.85 +  Service.serverURL = server.baseURI + "/";
    1.86 +  Service.clusterURL = server.baseURI + "/";
    1.87 +
    1.88 +  let request = Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) {
    1.89 +    do_check_eq(error.name, "SyntaxError");
    1.90 +    do_check_eq(info, null);
    1.91 +    server.stop(run_next_test);
    1.92 +  });
    1.93 +});

mercurial