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 | function ok(what, msg) { |
michael@0 | 2 | postMessage({ event: msg, test: 'ok', a: what }); |
michael@0 | 3 | } |
michael@0 | 4 | |
michael@0 | 5 | function is(a, b, msg) { |
michael@0 | 6 | postMessage({ event: msg, test: 'is', a: a, b: b }); |
michael@0 | 7 | } |
michael@0 | 8 | |
michael@0 | 9 | // This is a copy of content/base/test/test_XHR_parameters.js |
michael@0 | 10 | var validParameters = [ |
michael@0 | 11 | undefined, |
michael@0 | 12 | null, |
michael@0 | 13 | {}, |
michael@0 | 14 | {mozSystem: ""}, |
michael@0 | 15 | {mozSystem: 0}, |
michael@0 | 16 | {mozAnon: 1}, |
michael@0 | 17 | {mozAnon: []}, |
michael@0 | 18 | {get mozAnon() { return true; }}, |
michael@0 | 19 | 0, |
michael@0 | 20 | 7, |
michael@0 | 21 | Math.PI, |
michael@0 | 22 | "string", |
michael@0 | 23 | true, |
michael@0 | 24 | false, |
michael@0 | 25 | ]; |
michael@0 | 26 | |
michael@0 | 27 | var invalidParameters = [ |
michael@0 | 28 | {get mozSystem() { throw "Bla"; } }, |
michael@0 | 29 | ]; |
michael@0 | 30 | |
michael@0 | 31 | |
michael@0 | 32 | function testParameters(havePrivileges) { |
michael@0 | 33 | |
michael@0 | 34 | function testValidParameter(value) { |
michael@0 | 35 | var xhr; |
michael@0 | 36 | try { |
michael@0 | 37 | xhr = new XMLHttpRequest(value); |
michael@0 | 38 | } catch (ex) { |
michael@0 | 39 | ok(false, "Got unexpected exception: " + ex); |
michael@0 | 40 | return; |
michael@0 | 41 | } |
michael@0 | 42 | ok(!!xhr, "passed " + JSON.stringify(value)); |
michael@0 | 43 | |
michael@0 | 44 | // If the page doesnt have privileges to create a system or anon XHR, |
michael@0 | 45 | // these flags will always be false no matter what is passed. |
michael@0 | 46 | var expectedAnon = false; |
michael@0 | 47 | var expectedSystem = false; |
michael@0 | 48 | if (havePrivileges) { |
michael@0 | 49 | expectedAnon = Boolean(value && value.mozAnon); |
michael@0 | 50 | expectedSystem = Boolean(value && value.mozSystem); |
michael@0 | 51 | } |
michael@0 | 52 | is(xhr.mozAnon, expectedAnon, "testing mozAnon"); |
michael@0 | 53 | is(xhr.mozSystem, expectedSystem, "testing mozSystem"); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | function testInvalidParameter(value) { |
michael@0 | 58 | try { |
michael@0 | 59 | new XMLHttpRequest(value); |
michael@0 | 60 | ok(false, "invalid parameter did not cause exception: " + |
michael@0 | 61 | JSON.stringify(value)); |
michael@0 | 62 | } catch (ex) { |
michael@0 | 63 | ok(true, "invalid parameter raised exception as expected: " + |
michael@0 | 64 | JSON.stringify(ex)); |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | validParameters.forEach(testValidParameter); |
michael@0 | 69 | invalidParameters.forEach(testInvalidParameter); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | self.onmessage = function onmessage(event) { |
michael@0 | 73 | testParameters(event.data); |
michael@0 | 74 | postMessage({test: "finish"}); |
michael@0 | 75 | }; |