Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const apiUtils = require("sdk/deprecated/api-utils"); |
michael@0 | 6 | |
michael@0 | 7 | exports.testValidateOptionsEmpty = function (assert) { |
michael@0 | 8 | let val = apiUtils.validateOptions(null, {}); |
michael@0 | 9 | |
michael@0 | 10 | assert.deepEqual(val, {}); |
michael@0 | 11 | |
michael@0 | 12 | val = apiUtils.validateOptions(null, { foo: {} }); |
michael@0 | 13 | assert.deepEqual(val, {}); |
michael@0 | 14 | |
michael@0 | 15 | val = apiUtils.validateOptions({}, {}); |
michael@0 | 16 | assert.deepEqual(val, {}); |
michael@0 | 17 | |
michael@0 | 18 | val = apiUtils.validateOptions({}, { foo: {} }); |
michael@0 | 19 | assert.deepEqual(val, {}); |
michael@0 | 20 | }; |
michael@0 | 21 | |
michael@0 | 22 | exports.testValidateOptionsNonempty = function (assert) { |
michael@0 | 23 | let val = apiUtils.validateOptions({ foo: 123 }, {}); |
michael@0 | 24 | assert.deepEqual(val, {}); |
michael@0 | 25 | |
michael@0 | 26 | val = apiUtils.validateOptions({ foo: 123, bar: 456 }, |
michael@0 | 27 | { foo: {}, bar: {}, baz: {} }); |
michael@0 | 28 | |
michael@0 | 29 | assert.deepEqual(val, { foo: 123, bar: 456 }); |
michael@0 | 30 | }; |
michael@0 | 31 | |
michael@0 | 32 | exports.testValidateOptionsMap = function (assert) { |
michael@0 | 33 | let val = apiUtils.validateOptions({ foo: 3, bar: 2 }, { |
michael@0 | 34 | foo: { map: function (v) v * v }, |
michael@0 | 35 | bar: { map: function (v) undefined } |
michael@0 | 36 | }); |
michael@0 | 37 | assert.deepEqual(val, { foo: 9, bar: undefined }); |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | exports.testValidateOptionsMapException = function (assert) { |
michael@0 | 41 | let val = apiUtils.validateOptions({ foo: 3 }, { |
michael@0 | 42 | foo: { map: function () { throw new Error(); }} |
michael@0 | 43 | }); |
michael@0 | 44 | assert.deepEqual(val, { foo: 3 }); |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | exports.testValidateOptionsOk = function (assert) { |
michael@0 | 48 | let val = apiUtils.validateOptions({ foo: 3, bar: 2, baz: 1 }, { |
michael@0 | 49 | foo: { ok: function (v) v }, |
michael@0 | 50 | bar: { ok: function (v) v } |
michael@0 | 51 | }); |
michael@0 | 52 | assert.deepEqual(val, { foo: 3, bar: 2 }); |
michael@0 | 53 | |
michael@0 | 54 | assert.throws( |
michael@0 | 55 | function () apiUtils.validateOptions({ foo: 2, bar: 2 }, { |
michael@0 | 56 | bar: { ok: function (v) v > 2 } |
michael@0 | 57 | }), |
michael@0 | 58 | /^The option "bar" is invalid/, |
michael@0 | 59 | "ok should raise exception on invalid option" |
michael@0 | 60 | ); |
michael@0 | 61 | |
michael@0 | 62 | assert.throws( |
michael@0 | 63 | function () apiUtils.validateOptions(null, { foo: { ok: function (v) v }}), |
michael@0 | 64 | /^The option "foo" is invalid/, |
michael@0 | 65 | "ok should raise exception on invalid option" |
michael@0 | 66 | ); |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | exports.testValidateOptionsIs = function (assert) { |
michael@0 | 70 | let opts = { |
michael@0 | 71 | array: [], |
michael@0 | 72 | boolean: true, |
michael@0 | 73 | func: function () {}, |
michael@0 | 74 | nul: null, |
michael@0 | 75 | number: 1337, |
michael@0 | 76 | object: {}, |
michael@0 | 77 | string: "foo", |
michael@0 | 78 | undef1: undefined |
michael@0 | 79 | }; |
michael@0 | 80 | let requirements = { |
michael@0 | 81 | array: { is: ["array"] }, |
michael@0 | 82 | boolean: { is: ["boolean"] }, |
michael@0 | 83 | func: { is: ["function"] }, |
michael@0 | 84 | nul: { is: ["null"] }, |
michael@0 | 85 | number: { is: ["number"] }, |
michael@0 | 86 | object: { is: ["object"] }, |
michael@0 | 87 | string: { is: ["string"] }, |
michael@0 | 88 | undef1: { is: ["undefined"] }, |
michael@0 | 89 | undef2: { is: ["undefined"] } |
michael@0 | 90 | }; |
michael@0 | 91 | let val = apiUtils.validateOptions(opts, requirements); |
michael@0 | 92 | assert.deepEqual(val, opts); |
michael@0 | 93 | |
michael@0 | 94 | assert.throws( |
michael@0 | 95 | function () apiUtils.validateOptions(null, { |
michael@0 | 96 | foo: { is: ["object", "number"] } |
michael@0 | 97 | }), |
michael@0 | 98 | /^The option "foo" must be one of the following types: object, number/, |
michael@0 | 99 | "Invalid type should raise exception" |
michael@0 | 100 | ); |
michael@0 | 101 | }; |
michael@0 | 102 | |
michael@0 | 103 | exports.testValidateOptionsIsWithExportedValue = function (assert) { |
michael@0 | 104 | let { string, number, boolean, object } = apiUtils; |
michael@0 | 105 | |
michael@0 | 106 | let opts = { |
michael@0 | 107 | boolean: true, |
michael@0 | 108 | number: 1337, |
michael@0 | 109 | object: {}, |
michael@0 | 110 | string: "foo" |
michael@0 | 111 | }; |
michael@0 | 112 | let requirements = { |
michael@0 | 113 | string: { is: string }, |
michael@0 | 114 | number: { is: number }, |
michael@0 | 115 | boolean: { is: boolean }, |
michael@0 | 116 | object: { is: object } |
michael@0 | 117 | }; |
michael@0 | 118 | let val = apiUtils.validateOptions(opts, requirements); |
michael@0 | 119 | assert.deepEqual(val, opts); |
michael@0 | 120 | |
michael@0 | 121 | // Test the types are optional by default |
michael@0 | 122 | val = apiUtils.validateOptions({foo: 'bar'}, requirements); |
michael@0 | 123 | assert.deepEqual(val, {}); |
michael@0 | 124 | }; |
michael@0 | 125 | |
michael@0 | 126 | exports.testValidateOptionsIsWithEither = function (assert) { |
michael@0 | 127 | let { string, number, boolean, either } = apiUtils; |
michael@0 | 128 | let text = { is: either(string, number) }; |
michael@0 | 129 | |
michael@0 | 130 | let requirements = { |
michael@0 | 131 | text: text, |
michael@0 | 132 | boolOrText: { is: either(text, boolean) } |
michael@0 | 133 | }; |
michael@0 | 134 | |
michael@0 | 135 | let val = apiUtils.validateOptions({text: 12}, requirements); |
michael@0 | 136 | assert.deepEqual(val, {text: 12}); |
michael@0 | 137 | |
michael@0 | 138 | val = apiUtils.validateOptions({text: "12"}, requirements); |
michael@0 | 139 | assert.deepEqual(val, {text: "12"}); |
michael@0 | 140 | |
michael@0 | 141 | val = apiUtils.validateOptions({boolOrText: true}, requirements); |
michael@0 | 142 | assert.deepEqual(val, {boolOrText: true}); |
michael@0 | 143 | |
michael@0 | 144 | val = apiUtils.validateOptions({boolOrText: "true"}, requirements); |
michael@0 | 145 | assert.deepEqual(val, {boolOrText: "true"}); |
michael@0 | 146 | |
michael@0 | 147 | val = apiUtils.validateOptions({boolOrText: 1}, requirements); |
michael@0 | 148 | assert.deepEqual(val, {boolOrText: 1}); |
michael@0 | 149 | |
michael@0 | 150 | assert.throws( |
michael@0 | 151 | () => apiUtils.validateOptions({text: true}, requirements), |
michael@0 | 152 | /^The option "text" must be one of the following types/, |
michael@0 | 153 | "Invalid type should raise exception" |
michael@0 | 154 | ); |
michael@0 | 155 | |
michael@0 | 156 | assert.throws( |
michael@0 | 157 | () => apiUtils.validateOptions({boolOrText: []}, requirements), |
michael@0 | 158 | /^The option "boolOrText" must be one of the following types/, |
michael@0 | 159 | "Invalid type should raise exception" |
michael@0 | 160 | ); |
michael@0 | 161 | }; |
michael@0 | 162 | |
michael@0 | 163 | exports.testValidateOptionsWithRequiredAndOptional = function (assert) { |
michael@0 | 164 | let { string, number, required, optional } = apiUtils; |
michael@0 | 165 | |
michael@0 | 166 | let opts = { |
michael@0 | 167 | number: 1337, |
michael@0 | 168 | string: "foo" |
michael@0 | 169 | }; |
michael@0 | 170 | |
michael@0 | 171 | let requirements = { |
michael@0 | 172 | string: required(string), |
michael@0 | 173 | number: number |
michael@0 | 174 | }; |
michael@0 | 175 | |
michael@0 | 176 | let val = apiUtils.validateOptions(opts, requirements); |
michael@0 | 177 | assert.deepEqual(val, opts); |
michael@0 | 178 | |
michael@0 | 179 | val = apiUtils.validateOptions({string: "foo"}, requirements); |
michael@0 | 180 | assert.deepEqual(val, {string: "foo"}); |
michael@0 | 181 | |
michael@0 | 182 | assert.throws( |
michael@0 | 183 | () => apiUtils.validateOptions({number: 10}, requirements), |
michael@0 | 184 | /^The option "string" must be one of the following types/, |
michael@0 | 185 | "Invalid type should raise exception" |
michael@0 | 186 | ); |
michael@0 | 187 | |
michael@0 | 188 | // Makes string optional |
michael@0 | 189 | requirements.string = optional(requirements.string); |
michael@0 | 190 | |
michael@0 | 191 | val = apiUtils.validateOptions({number: 10}, requirements), |
michael@0 | 192 | assert.deepEqual(val, {number: 10}); |
michael@0 | 193 | |
michael@0 | 194 | }; |
michael@0 | 195 | |
michael@0 | 196 | |
michael@0 | 197 | |
michael@0 | 198 | exports.testValidateOptionsWithExportedValue = function (assert) { |
michael@0 | 199 | let { string, number, boolean, object } = apiUtils; |
michael@0 | 200 | |
michael@0 | 201 | let opts = { |
michael@0 | 202 | boolean: true, |
michael@0 | 203 | number: 1337, |
michael@0 | 204 | object: {}, |
michael@0 | 205 | string: "foo" |
michael@0 | 206 | }; |
michael@0 | 207 | let requirements = { |
michael@0 | 208 | string: string, |
michael@0 | 209 | number: number, |
michael@0 | 210 | boolean: boolean, |
michael@0 | 211 | object: object |
michael@0 | 212 | }; |
michael@0 | 213 | let val = apiUtils.validateOptions(opts, requirements); |
michael@0 | 214 | assert.deepEqual(val, opts); |
michael@0 | 215 | |
michael@0 | 216 | // Test the types are optional by default |
michael@0 | 217 | val = apiUtils.validateOptions({foo: 'bar'}, requirements); |
michael@0 | 218 | assert.deepEqual(val, {}); |
michael@0 | 219 | }; |
michael@0 | 220 | |
michael@0 | 221 | |
michael@0 | 222 | exports.testValidateOptionsMapIsOk = function (assert) { |
michael@0 | 223 | let [map, is, ok] = [false, false, false]; |
michael@0 | 224 | let val = apiUtils.validateOptions({ foo: 1337 }, { |
michael@0 | 225 | foo: { |
michael@0 | 226 | map: function (v) v.toString(), |
michael@0 | 227 | is: ["string"], |
michael@0 | 228 | ok: function (v) v.length > 0 |
michael@0 | 229 | } |
michael@0 | 230 | }); |
michael@0 | 231 | assert.deepEqual(val, { foo: "1337" }); |
michael@0 | 232 | |
michael@0 | 233 | let requirements = { |
michael@0 | 234 | foo: { |
michael@0 | 235 | is: ["object"], |
michael@0 | 236 | ok: function () assert.fail("is should have caused us to throw by now") |
michael@0 | 237 | } |
michael@0 | 238 | }; |
michael@0 | 239 | assert.throws( |
michael@0 | 240 | function () apiUtils.validateOptions(null, requirements), |
michael@0 | 241 | /^The option "foo" must be one of the following types: object/, |
michael@0 | 242 | "is should be used before ok is called" |
michael@0 | 243 | ); |
michael@0 | 244 | }; |
michael@0 | 245 | |
michael@0 | 246 | exports.testValidateOptionsErrorMsg = function (assert) { |
michael@0 | 247 | assert.throws( |
michael@0 | 248 | function () apiUtils.validateOptions(null, { |
michael@0 | 249 | foo: { ok: function (v) v, msg: "foo!" } |
michael@0 | 250 | }), |
michael@0 | 251 | /^foo!/, |
michael@0 | 252 | "ok should raise exception with customized message" |
michael@0 | 253 | ); |
michael@0 | 254 | }; |
michael@0 | 255 | |
michael@0 | 256 | exports.testValidateMapWithMissingKey = function (assert) { |
michael@0 | 257 | let val = apiUtils.validateOptions({ }, { |
michael@0 | 258 | foo: { |
michael@0 | 259 | map: function (v) v || "bar" |
michael@0 | 260 | } |
michael@0 | 261 | }); |
michael@0 | 262 | assert.deepEqual(val, { foo: "bar" }); |
michael@0 | 263 | |
michael@0 | 264 | val = apiUtils.validateOptions({ }, { |
michael@0 | 265 | foo: { |
michael@0 | 266 | map: function (v) { throw "bar" } |
michael@0 | 267 | } |
michael@0 | 268 | }); |
michael@0 | 269 | assert.deepEqual(val, { }); |
michael@0 | 270 | }; |
michael@0 | 271 | |
michael@0 | 272 | exports.testValidateMapWithMissingKeyAndThrown = function (assert) { |
michael@0 | 273 | let val = apiUtils.validateOptions({}, { |
michael@0 | 274 | bar: { |
michael@0 | 275 | map: function(v) { throw "bar" } |
michael@0 | 276 | }, |
michael@0 | 277 | baz: { |
michael@0 | 278 | map: function(v) "foo" |
michael@0 | 279 | } |
michael@0 | 280 | }); |
michael@0 | 281 | assert.deepEqual(val, { baz: "foo" }); |
michael@0 | 282 | }; |
michael@0 | 283 | |
michael@0 | 284 | exports.testAddIterator = function testAddIterator (assert) { |
michael@0 | 285 | let obj = {}; |
michael@0 | 286 | let keys = ["foo", "bar", "baz"]; |
michael@0 | 287 | let vals = [1, 2, 3]; |
michael@0 | 288 | let keysVals = [["foo", 1], ["bar", 2], ["baz", 3]]; |
michael@0 | 289 | apiUtils.addIterator( |
michael@0 | 290 | obj, |
michael@0 | 291 | function keysValsGen() { |
michael@0 | 292 | for each (let keyVal in keysVals) |
michael@0 | 293 | yield keyVal; |
michael@0 | 294 | } |
michael@0 | 295 | ); |
michael@0 | 296 | |
michael@0 | 297 | let keysItr = []; |
michael@0 | 298 | for (let key in obj) |
michael@0 | 299 | keysItr.push(key); |
michael@0 | 300 | |
michael@0 | 301 | assert.equal(keysItr.length, keys.length, |
michael@0 | 302 | "the keys iterator returns the correct number of items"); |
michael@0 | 303 | for (let i = 0; i < keys.length; i++) |
michael@0 | 304 | assert.equal(keysItr[i], keys[i], "the key is correct"); |
michael@0 | 305 | |
michael@0 | 306 | let valsItr = []; |
michael@0 | 307 | for each (let val in obj) |
michael@0 | 308 | valsItr.push(val); |
michael@0 | 309 | assert.equal(valsItr.length, vals.length, |
michael@0 | 310 | "the vals iterator returns the correct number of items"); |
michael@0 | 311 | for (let i = 0; i < vals.length; i++) |
michael@0 | 312 | assert.equal(valsItr[i], vals[i], "the val is correct"); |
michael@0 | 313 | |
michael@0 | 314 | }; |
michael@0 | 315 | |
michael@0 | 316 | require('test').run(exports); |