7 <title>Test for XMLHttpRequest with system privileges
</title>
8 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
11 <body onload=
"runTests();">
14 <div id=
"content" style=
"display: none">
18 <script class=
"testbody" type=
"application/javascript">
21 SimpleTest.waitForExplicitFinish();
23 let validParameters = [
31 {get mozAnon() { return true; }},
40 let invalidParameters = [
41 {get mozSystem() { throw new Error(
"Bla"); } },
44 let havePrivileges = false;
46 function testValidParameter(value) {
49 xhr = new XMLHttpRequest(value);
51 ok(false,
"Got unexpected exception: " + ex);
54 ok(xhr instanceof XMLHttpRequest,
"passed " + JSON.stringify(value));
56 // If the page doesnt have privileges to create a system XHR,
57 // this flag will always be false no matter what is passed.
58 let expectedAnon = Boolean(value && value.mozAnon);
59 let expectedSystem = false;
61 expectedSystem = Boolean(value && value.mozSystem);
63 is(xhr.mozAnon, expectedAnon,
"testing mozAnon");
64 is(xhr.mozSystem, expectedSystem,
"testing mozSystem");
67 function testInvalidParameter(value) {
70 new XMLHttpRequest(value);
71 ok(false,
"invalid parameter did not cause exception: " +
72 JSON.stringify(value));
76 ok(expectedError,
"invalid parameter raised exception as expected: " +
77 JSON.stringify(expectedError))
80 // Run the tests once without API privileges...
81 validParameters.forEach(testValidParameter);
82 invalidParameters.forEach(testInvalidParameter);
84 // ...and once with privileges.
85 havePrivileges = true;
86 SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], function() {
87 validParameters.forEach(testValidParameter);
88 invalidParameters.forEach(testInvalidParameter);