Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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://gre/modules/Log.jsm"); |
michael@0 | 5 | Cu.import("resource://services-sync/constants.js"); |
michael@0 | 6 | Cu.import("resource://services-sync/rest.js"); |
michael@0 | 7 | Cu.import("resource://services-sync/service.js"); |
michael@0 | 8 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 9 | Cu.import("resource://testing-common/services/sync/utils.js"); |
michael@0 | 10 | |
michael@0 | 11 | function run_test() { |
michael@0 | 12 | Log.repository.getLogger("Sync.RESTRequest").level = Log.Level.Trace; |
michael@0 | 13 | initTestLogging(); |
michael@0 | 14 | |
michael@0 | 15 | ensureLegacyIdentityManager(); |
michael@0 | 16 | |
michael@0 | 17 | run_next_test(); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | add_test(function test_user_agent_desktop() { |
michael@0 | 21 | let handler = httpd_handler(200, "OK"); |
michael@0 | 22 | let server = httpd_setup({"/resource": handler}); |
michael@0 | 23 | |
michael@0 | 24 | let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version + |
michael@0 | 25 | " FxSync/" + WEAVE_VERSION + "." + |
michael@0 | 26 | Services.appinfo.appBuildID + ".desktop"; |
michael@0 | 27 | |
michael@0 | 28 | let request = new SyncStorageRequest(server.baseURI + "/resource"); |
michael@0 | 29 | request.onComplete = function onComplete(error) { |
michael@0 | 30 | do_check_eq(error, null); |
michael@0 | 31 | do_check_eq(this.response.status, 200); |
michael@0 | 32 | do_check_eq(handler.request.getHeader("User-Agent"), expectedUA); |
michael@0 | 33 | server.stop(run_next_test); |
michael@0 | 34 | }; |
michael@0 | 35 | do_check_eq(request.get(), request); |
michael@0 | 36 | }); |
michael@0 | 37 | |
michael@0 | 38 | add_test(function test_user_agent_mobile() { |
michael@0 | 39 | let handler = httpd_handler(200, "OK"); |
michael@0 | 40 | let server = httpd_setup({"/resource": handler}); |
michael@0 | 41 | |
michael@0 | 42 | Svc.Prefs.set("client.type", "mobile"); |
michael@0 | 43 | let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version + |
michael@0 | 44 | " FxSync/" + WEAVE_VERSION + "." + |
michael@0 | 45 | Services.appinfo.appBuildID + ".mobile"; |
michael@0 | 46 | |
michael@0 | 47 | let request = new SyncStorageRequest(server.baseURI + "/resource"); |
michael@0 | 48 | request.get(function (error) { |
michael@0 | 49 | do_check_eq(error, null); |
michael@0 | 50 | do_check_eq(this.response.status, 200); |
michael@0 | 51 | do_check_eq(handler.request.getHeader("User-Agent"), expectedUA); |
michael@0 | 52 | Svc.Prefs.resetBranch(""); |
michael@0 | 53 | server.stop(run_next_test); |
michael@0 | 54 | }); |
michael@0 | 55 | }); |
michael@0 | 56 | |
michael@0 | 57 | add_test(function test_auth() { |
michael@0 | 58 | let handler = httpd_handler(200, "OK"); |
michael@0 | 59 | let server = httpd_setup({"/resource": handler}); |
michael@0 | 60 | |
michael@0 | 61 | setBasicCredentials("johndoe", "ilovejane", "XXXXXXXXX"); |
michael@0 | 62 | |
michael@0 | 63 | let request = Service.getStorageRequest(server.baseURI + "/resource"); |
michael@0 | 64 | request.get(function (error) { |
michael@0 | 65 | do_check_eq(error, null); |
michael@0 | 66 | do_check_eq(this.response.status, 200); |
michael@0 | 67 | do_check_true(basic_auth_matches(handler.request, "johndoe", "ilovejane")); |
michael@0 | 68 | |
michael@0 | 69 | Svc.Prefs.reset(""); |
michael@0 | 70 | |
michael@0 | 71 | server.stop(run_next_test); |
michael@0 | 72 | }); |
michael@0 | 73 | }); |
michael@0 | 74 | |
michael@0 | 75 | /** |
michael@0 | 76 | * The X-Weave-Timestamp header updates SyncStorageRequest.serverTime. |
michael@0 | 77 | */ |
michael@0 | 78 | add_test(function test_weave_timestamp() { |
michael@0 | 79 | const TIMESTAMP = 1274380461; |
michael@0 | 80 | function handler(request, response) { |
michael@0 | 81 | response.setHeader("X-Weave-Timestamp", "" + TIMESTAMP, false); |
michael@0 | 82 | response.setStatusLine(request.httpVersion, 200, "OK"); |
michael@0 | 83 | } |
michael@0 | 84 | let server = httpd_setup({"/resource": handler}); |
michael@0 | 85 | |
michael@0 | 86 | do_check_eq(SyncStorageRequest.serverTime, undefined); |
michael@0 | 87 | let request = new SyncStorageRequest(server.baseURI + "/resource"); |
michael@0 | 88 | request.get(function (error) { |
michael@0 | 89 | do_check_eq(error, null); |
michael@0 | 90 | do_check_eq(this.response.status, 200); |
michael@0 | 91 | do_check_eq(SyncStorageRequest.serverTime, TIMESTAMP); |
michael@0 | 92 | delete SyncStorageRequest.serverTime; |
michael@0 | 93 | server.stop(run_next_test); |
michael@0 | 94 | }); |
michael@0 | 95 | }); |
michael@0 | 96 | |
michael@0 | 97 | /** |
michael@0 | 98 | * The X-Weave-Backoff header notifies an observer. |
michael@0 | 99 | */ |
michael@0 | 100 | add_test(function test_weave_backoff() { |
michael@0 | 101 | function handler(request, response) { |
michael@0 | 102 | response.setHeader("X-Weave-Backoff", '600', false); |
michael@0 | 103 | response.setStatusLine(request.httpVersion, 200, "OK"); |
michael@0 | 104 | } |
michael@0 | 105 | let server = httpd_setup({"/resource": handler}); |
michael@0 | 106 | |
michael@0 | 107 | let backoffInterval; |
michael@0 | 108 | Svc.Obs.add("weave:service:backoff:interval", function onBackoff(subject) { |
michael@0 | 109 | Svc.Obs.remove("weave:service:backoff:interval", onBackoff); |
michael@0 | 110 | backoffInterval = subject; |
michael@0 | 111 | }); |
michael@0 | 112 | |
michael@0 | 113 | let request = new SyncStorageRequest(server.baseURI + "/resource"); |
michael@0 | 114 | request.get(function (error) { |
michael@0 | 115 | do_check_eq(error, null); |
michael@0 | 116 | do_check_eq(this.response.status, 200); |
michael@0 | 117 | do_check_eq(backoffInterval, 600); |
michael@0 | 118 | server.stop(run_next_test); |
michael@0 | 119 | }); |
michael@0 | 120 | }); |
michael@0 | 121 | |
michael@0 | 122 | /** |
michael@0 | 123 | * X-Weave-Quota-Remaining header notifies observer on successful requests. |
michael@0 | 124 | */ |
michael@0 | 125 | add_test(function test_weave_quota_notice() { |
michael@0 | 126 | function handler(request, response) { |
michael@0 | 127 | response.setHeader("X-Weave-Quota-Remaining", '1048576', false); |
michael@0 | 128 | response.setStatusLine(request.httpVersion, 200, "OK"); |
michael@0 | 129 | } |
michael@0 | 130 | let server = httpd_setup({"/resource": handler}); |
michael@0 | 131 | |
michael@0 | 132 | let quotaValue; |
michael@0 | 133 | Svc.Obs.add("weave:service:quota:remaining", function onQuota(subject) { |
michael@0 | 134 | Svc.Obs.remove("weave:service:quota:remaining", onQuota); |
michael@0 | 135 | quotaValue = subject; |
michael@0 | 136 | }); |
michael@0 | 137 | |
michael@0 | 138 | let request = new SyncStorageRequest(server.baseURI + "/resource"); |
michael@0 | 139 | request.get(function (error) { |
michael@0 | 140 | do_check_eq(error, null); |
michael@0 | 141 | do_check_eq(this.response.status, 200); |
michael@0 | 142 | do_check_eq(quotaValue, 1048576); |
michael@0 | 143 | server.stop(run_next_test); |
michael@0 | 144 | }); |
michael@0 | 145 | }); |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * X-Weave-Quota-Remaining header doesn't notify observer on failed requests. |
michael@0 | 149 | */ |
michael@0 | 150 | add_test(function test_weave_quota_error() { |
michael@0 | 151 | function handler(request, response) { |
michael@0 | 152 | response.setHeader("X-Weave-Quota-Remaining", '1048576', false); |
michael@0 | 153 | response.setStatusLine(request.httpVersion, 400, "Bad Request"); |
michael@0 | 154 | } |
michael@0 | 155 | let server = httpd_setup({"/resource": handler}); |
michael@0 | 156 | |
michael@0 | 157 | let quotaValue; |
michael@0 | 158 | function onQuota(subject) { |
michael@0 | 159 | quotaValue = subject; |
michael@0 | 160 | } |
michael@0 | 161 | Svc.Obs.add("weave:service:quota:remaining", onQuota); |
michael@0 | 162 | |
michael@0 | 163 | let request = new SyncStorageRequest(server.baseURI + "/resource"); |
michael@0 | 164 | request.get(function (error) { |
michael@0 | 165 | do_check_eq(error, null); |
michael@0 | 166 | do_check_eq(this.response.status, 400); |
michael@0 | 167 | do_check_eq(quotaValue, undefined); |
michael@0 | 168 | Svc.Obs.remove("weave:service:quota:remaining", onQuota); |
michael@0 | 169 | server.stop(run_next_test); |
michael@0 | 170 | }); |
michael@0 | 171 | }); |
michael@0 | 172 | |
michael@0 | 173 | add_test(function test_abort() { |
michael@0 | 174 | function handler(request, response) { |
michael@0 | 175 | response.setHeader("X-Weave-Timestamp", "" + TIMESTAMP, false); |
michael@0 | 176 | response.setHeader("X-Weave-Quota-Remaining", '1048576', false); |
michael@0 | 177 | response.setHeader("X-Weave-Backoff", '600', false); |
michael@0 | 178 | response.setStatusLine(request.httpVersion, 200, "OK"); |
michael@0 | 179 | } |
michael@0 | 180 | let server = httpd_setup({"/resource": handler}); |
michael@0 | 181 | |
michael@0 | 182 | let request = new SyncStorageRequest(server.baseURI + "/resource"); |
michael@0 | 183 | |
michael@0 | 184 | // Aborting a request that hasn't been sent yet is pointless and will throw. |
michael@0 | 185 | do_check_throws(function () { |
michael@0 | 186 | request.abort(); |
michael@0 | 187 | }); |
michael@0 | 188 | |
michael@0 | 189 | function throwy() { |
michael@0 | 190 | do_throw("Shouldn't have gotten here!"); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | Svc.Obs.add("weave:service:backoff:interval", throwy); |
michael@0 | 194 | Svc.Obs.add("weave:service:quota:remaining", throwy); |
michael@0 | 195 | request.onProgress = request.onComplete = throwy; |
michael@0 | 196 | |
michael@0 | 197 | request.get(); |
michael@0 | 198 | request.abort(); |
michael@0 | 199 | do_check_eq(request.status, request.ABORTED); |
michael@0 | 200 | |
michael@0 | 201 | // Aborting an already aborted request is pointless and will throw. |
michael@0 | 202 | do_check_throws(function () { |
michael@0 | 203 | request.abort(); |
michael@0 | 204 | }); |
michael@0 | 205 | |
michael@0 | 206 | Utils.nextTick(function () { |
michael@0 | 207 | // Verify that we didn't try to process any of the values. |
michael@0 | 208 | do_check_eq(SyncStorageRequest.serverTime, undefined); |
michael@0 | 209 | |
michael@0 | 210 | Svc.Obs.remove("weave:service:backoff:interval", throwy); |
michael@0 | 211 | Svc.Obs.remove("weave:service:quota:remaining", throwy); |
michael@0 | 212 | |
michael@0 | 213 | server.stop(run_next_test); |
michael@0 | 214 | }); |
michael@0 | 215 | }); |