diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /services/sync/tests/unit/test_syncstoragerequest.js | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'services/sync/tests/unit/test_syncstoragerequest.js')
-rw-r--r-- | services/sync/tests/unit/test_syncstoragerequest.js | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/services/sync/tests/unit/test_syncstoragerequest.js b/services/sync/tests/unit/test_syncstoragerequest.js new file mode 100644 index 000000000..14e5daade --- /dev/null +++ b/services/sync/tests/unit/test_syncstoragerequest.js @@ -0,0 +1,220 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +Cu.import("resource://gre/modules/Log.jsm"); +Cu.import("resource://services-sync/constants.js"); +Cu.import("resource://services-sync/rest.js"); +Cu.import("resource://services-sync/service.js"); +Cu.import("resource://services-sync/util.js"); +Cu.import("resource://testing-common/services/sync/utils.js"); + +var httpProtocolHandler = Cc["@mozilla.org/network/protocol;1?name=http"] + .getService(Ci.nsIHttpProtocolHandler); + +function run_test() { + Log.repository.getLogger("Sync.RESTRequest").level = Log.Level.Trace; + initTestLogging(); + + ensureLegacyIdentityManager(); + + run_next_test(); +} + +add_test(function test_user_agent_desktop() { + let handler = httpd_handler(200, "OK"); + let server = httpd_setup({"/resource": handler}); + + let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version + + " (" + httpProtocolHandler.oscpu + ")" + + " FxSync/" + WEAVE_VERSION + "." + + Services.appinfo.appBuildID + ".desktop"; + + let request = new SyncStorageRequest(server.baseURI + "/resource"); + request.onComplete = function onComplete(error) { + do_check_eq(error, null); + do_check_eq(this.response.status, 200); + do_check_eq(handler.request.getHeader("User-Agent"), expectedUA); + server.stop(run_next_test); + }; + do_check_eq(request.get(), request); +}); + +add_test(function test_user_agent_mobile() { + let handler = httpd_handler(200, "OK"); + let server = httpd_setup({"/resource": handler}); + + Svc.Prefs.set("client.type", "mobile"); + let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version + + " (" + httpProtocolHandler.oscpu + ")" + + " FxSync/" + WEAVE_VERSION + "." + + Services.appinfo.appBuildID + ".mobile"; + + let request = new SyncStorageRequest(server.baseURI + "/resource"); + request.get(function (error) { + do_check_eq(error, null); + do_check_eq(this.response.status, 200); + do_check_eq(handler.request.getHeader("User-Agent"), expectedUA); + Svc.Prefs.resetBranch(""); + server.stop(run_next_test); + }); +}); + +add_test(function test_auth() { + let handler = httpd_handler(200, "OK"); + let server = httpd_setup({"/resource": handler}); + + setBasicCredentials("johndoe", "ilovejane", "XXXXXXXXX"); + + let request = Service.getStorageRequest(server.baseURI + "/resource"); + request.get(function (error) { + do_check_eq(error, null); + do_check_eq(this.response.status, 200); + do_check_true(basic_auth_matches(handler.request, "johndoe", "ilovejane")); + + Svc.Prefs.reset(""); + + server.stop(run_next_test); + }); +}); + +/** + * The X-Weave-Timestamp header updates SyncStorageRequest.serverTime. + */ +add_test(function test_weave_timestamp() { + const TIMESTAMP = 1274380461; + function handler(request, response) { + response.setHeader("X-Weave-Timestamp", "" + TIMESTAMP, false); + response.setStatusLine(request.httpVersion, 200, "OK"); + } + let server = httpd_setup({"/resource": handler}); + + do_check_eq(SyncStorageRequest.serverTime, undefined); + let request = new SyncStorageRequest(server.baseURI + "/resource"); + request.get(function (error) { + do_check_eq(error, null); + do_check_eq(this.response.status, 200); + do_check_eq(SyncStorageRequest.serverTime, TIMESTAMP); + delete SyncStorageRequest.serverTime; + server.stop(run_next_test); + }); +}); + +/** + * The X-Weave-Backoff header notifies an observer. + */ +add_test(function test_weave_backoff() { + function handler(request, response) { + response.setHeader("X-Weave-Backoff", '600', false); + response.setStatusLine(request.httpVersion, 200, "OK"); + } + let server = httpd_setup({"/resource": handler}); + + let backoffInterval; + Svc.Obs.add("weave:service:backoff:interval", function onBackoff(subject) { + Svc.Obs.remove("weave:service:backoff:interval", onBackoff); + backoffInterval = subject; + }); + + let request = new SyncStorageRequest(server.baseURI + "/resource"); + request.get(function (error) { + do_check_eq(error, null); + do_check_eq(this.response.status, 200); + do_check_eq(backoffInterval, 600); + server.stop(run_next_test); + }); +}); + +/** + * X-Weave-Quota-Remaining header notifies observer on successful requests. + */ +add_test(function test_weave_quota_notice() { + function handler(request, response) { + response.setHeader("X-Weave-Quota-Remaining", '1048576', false); + response.setStatusLine(request.httpVersion, 200, "OK"); + } + let server = httpd_setup({"/resource": handler}); + + let quotaValue; + Svc.Obs.add("weave:service:quota:remaining", function onQuota(subject) { + Svc.Obs.remove("weave:service:quota:remaining", onQuota); + quotaValue = subject; + }); + + let request = new SyncStorageRequest(server.baseURI + "/resource"); + request.get(function (error) { + do_check_eq(error, null); + do_check_eq(this.response.status, 200); + do_check_eq(quotaValue, 1048576); + server.stop(run_next_test); + }); +}); + +/** + * X-Weave-Quota-Remaining header doesn't notify observer on failed requests. + */ +add_test(function test_weave_quota_error() { + function handler(request, response) { + response.setHeader("X-Weave-Quota-Remaining", '1048576', false); + response.setStatusLine(request.httpVersion, 400, "Bad Request"); + } + let server = httpd_setup({"/resource": handler}); + + let quotaValue; + function onQuota(subject) { + quotaValue = subject; + } + Svc.Obs.add("weave:service:quota:remaining", onQuota); + + let request = new SyncStorageRequest(server.baseURI + "/resource"); + request.get(function (error) { + do_check_eq(error, null); + do_check_eq(this.response.status, 400); + do_check_eq(quotaValue, undefined); + Svc.Obs.remove("weave:service:quota:remaining", onQuota); + server.stop(run_next_test); + }); +}); + +add_test(function test_abort() { + function handler(request, response) { + response.setHeader("X-Weave-Timestamp", "" + TIMESTAMP, false); + response.setHeader("X-Weave-Quota-Remaining", '1048576', false); + response.setHeader("X-Weave-Backoff", '600', false); + response.setStatusLine(request.httpVersion, 200, "OK"); + } + let server = httpd_setup({"/resource": handler}); + + let request = new SyncStorageRequest(server.baseURI + "/resource"); + + // Aborting a request that hasn't been sent yet is pointless and will throw. + do_check_throws(function () { + request.abort(); + }); + + function throwy() { + do_throw("Shouldn't have gotten here!"); + } + + Svc.Obs.add("weave:service:backoff:interval", throwy); + Svc.Obs.add("weave:service:quota:remaining", throwy); + request.onProgress = request.onComplete = throwy; + + request.get(); + request.abort(); + do_check_eq(request.status, request.ABORTED); + + // Aborting an already aborted request is pointless and will throw. + do_check_throws(function () { + request.abort(); + }); + + Utils.nextTick(function () { + // Verify that we didn't try to process any of the values. + do_check_eq(SyncStorageRequest.serverTime, undefined); + + Svc.Obs.remove("weave:service:backoff:interval", throwy); + Svc.Obs.remove("weave:service:quota:remaining", throwy); + + server.stop(run_next_test); + }); +}); |