Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | Cu.import("resource://services-common/rest.js"); |
michael@0 | 5 | Cu.import("resource://services-sync/constants.js"); |
michael@0 | 6 | Cu.import("resource://services-sync/service.js"); |
michael@0 | 7 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 8 | Cu.import("resource://testing-common/services/sync/utils.js"); |
michael@0 | 9 | |
michael@0 | 10 | let collections = {steam: 65.11328, |
michael@0 | 11 | petrol: 82.488281, |
michael@0 | 12 | diesel: 2.25488281}; |
michael@0 | 13 | |
michael@0 | 14 | function run_test() { |
michael@0 | 15 | Log.repository.getLogger("Sync.Service").level = Log.Level.Trace; |
michael@0 | 16 | Log.repository.getLogger("Sync.StorageRequest").level = Log.Level.Trace; |
michael@0 | 17 | initTestLogging(); |
michael@0 | 18 | |
michael@0 | 19 | ensureLegacyIdentityManager(); |
michael@0 | 20 | setBasicCredentials("johndoe", "ilovejane"); |
michael@0 | 21 | |
michael@0 | 22 | run_next_test(); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | add_test(function test_success() { |
michael@0 | 26 | let handler = httpd_handler(200, "OK", JSON.stringify(collections)); |
michael@0 | 27 | let server = httpd_setup({"/1.1/johndoe/info/collections": handler}); |
michael@0 | 28 | Service.serverURL = server.baseURI + "/"; |
michael@0 | 29 | Service.clusterURL = server.baseURI + "/"; |
michael@0 | 30 | |
michael@0 | 31 | let request = Service.getStorageInfo("collections", function (error, info) { |
michael@0 | 32 | do_check_eq(error, null); |
michael@0 | 33 | do_check_true(Utils.deepEquals(info, collections)); |
michael@0 | 34 | |
michael@0 | 35 | // Ensure that the request is sent off with the right bits. |
michael@0 | 36 | do_check_true(basic_auth_matches(handler.request, |
michael@0 | 37 | Service.identity.username, |
michael@0 | 38 | Service.identity.basicPassword)); |
michael@0 | 39 | let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version + |
michael@0 | 40 | " FxSync/" + WEAVE_VERSION + "." + |
michael@0 | 41 | Services.appinfo.appBuildID + ".desktop"; |
michael@0 | 42 | do_check_eq(handler.request.getHeader("User-Agent"), expectedUA); |
michael@0 | 43 | |
michael@0 | 44 | server.stop(run_next_test); |
michael@0 | 45 | }); |
michael@0 | 46 | do_check_true(request instanceof RESTRequest); |
michael@0 | 47 | }); |
michael@0 | 48 | |
michael@0 | 49 | add_test(function test_invalid_type() { |
michael@0 | 50 | do_check_throws(function () { |
michael@0 | 51 | Service.getStorageInfo("invalid", function (error, info) { |
michael@0 | 52 | do_throw("Shouldn't get here!"); |
michael@0 | 53 | }); |
michael@0 | 54 | }); |
michael@0 | 55 | run_next_test(); |
michael@0 | 56 | }); |
michael@0 | 57 | |
michael@0 | 58 | add_test(function test_network_error() { |
michael@0 | 59 | Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) { |
michael@0 | 60 | do_check_eq(error.result, Cr.NS_ERROR_CONNECTION_REFUSED); |
michael@0 | 61 | do_check_eq(info, null); |
michael@0 | 62 | run_next_test(); |
michael@0 | 63 | }); |
michael@0 | 64 | }); |
michael@0 | 65 | |
michael@0 | 66 | add_test(function test_http_error() { |
michael@0 | 67 | let handler = httpd_handler(500, "Oh noez", "Something went wrong!"); |
michael@0 | 68 | let server = httpd_setup({"/1.1/johndoe/info/collections": handler}); |
michael@0 | 69 | Service.serverURL = server.baseURI + "/"; |
michael@0 | 70 | Service.clusterURL = server.baseURI + "/"; |
michael@0 | 71 | |
michael@0 | 72 | let request = Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) { |
michael@0 | 73 | do_check_eq(error.status, 500); |
michael@0 | 74 | do_check_eq(info, null); |
michael@0 | 75 | server.stop(run_next_test); |
michael@0 | 76 | }); |
michael@0 | 77 | }); |
michael@0 | 78 | |
michael@0 | 79 | add_test(function test_invalid_json() { |
michael@0 | 80 | let handler = httpd_handler(200, "OK", "Invalid JSON"); |
michael@0 | 81 | let server = httpd_setup({"/1.1/johndoe/info/collections": handler}); |
michael@0 | 82 | Service.serverURL = server.baseURI + "/"; |
michael@0 | 83 | Service.clusterURL = server.baseURI + "/"; |
michael@0 | 84 | |
michael@0 | 85 | let request = Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) { |
michael@0 | 86 | do_check_eq(error.name, "SyntaxError"); |
michael@0 | 87 | do_check_eq(info, null); |
michael@0 | 88 | server.stop(run_next_test); |
michael@0 | 89 | }); |
michael@0 | 90 | }); |