|
1 |
|
2 |
|
3 <!DOCTYPE html> |
|
4 <html> |
|
5 <head> |
|
6 <meta charset="utf-8"> |
|
7 <title>Test for XMLHttpRequest with system privileges</title> |
|
8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
10 </head> |
|
11 <body onload="runTests();"> |
|
12 <p id="display"> |
|
13 </p> |
|
14 <div id="content" style="display: none"> |
|
15 |
|
16 </div> |
|
17 <pre id="test"> |
|
18 <script class="testbody" type="application/javascript;version=1.8"> |
|
19 |
|
20 function runTests() { |
|
21 SimpleTest.waitForExplicitFinish(); |
|
22 |
|
23 let validParameters = [ |
|
24 undefined, |
|
25 null, |
|
26 {}, |
|
27 {mozSystem: ""}, |
|
28 {mozSystem: 0}, |
|
29 {mozAnon: 1}, |
|
30 {mozAnon: []}, |
|
31 {get mozAnon() { return true; }}, |
|
32 0, |
|
33 7, |
|
34 Math.PI, |
|
35 "string", |
|
36 true, |
|
37 false, |
|
38 ]; |
|
39 |
|
40 let invalidParameters = [ |
|
41 {get mozSystem() { throw "Bla"; } }, |
|
42 ]; |
|
43 |
|
44 let havePrivileges = false; |
|
45 |
|
46 function testValidParameter(value) { |
|
47 let xhr; |
|
48 try { |
|
49 xhr = new XMLHttpRequest(value); |
|
50 } catch (ex) { |
|
51 ok(false, "Got unexpected exception: " + ex); |
|
52 return; |
|
53 } |
|
54 ok(xhr instanceof XMLHttpRequest, "passed " + JSON.stringify(value)); |
|
55 |
|
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; |
|
60 if (havePrivileges) { |
|
61 expectedSystem = Boolean(value && value.mozSystem); |
|
62 } |
|
63 is(xhr.mozAnon, expectedAnon, "testing mozAnon"); |
|
64 is(xhr.mozSystem, expectedSystem, "testing mozSystem"); |
|
65 } |
|
66 |
|
67 function testInvalidParameter(value) { |
|
68 let expectedError; |
|
69 try { |
|
70 new XMLHttpRequest(value); |
|
71 ok(false, "invalid parameter did not cause exception: " + |
|
72 JSON.stringify(value)); |
|
73 } catch (ex) { |
|
74 expectedError = ex; |
|
75 } |
|
76 ok(expectedError, "invalid parameter raised exception as expected: " + |
|
77 JSON.stringify(expectedError)) |
|
78 } |
|
79 |
|
80 // Run the tests once without API privileges... |
|
81 validParameters.forEach(testValidParameter); |
|
82 invalidParameters.forEach(testInvalidParameter); |
|
83 |
|
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); |
|
89 |
|
90 SimpleTest.finish(); |
|
91 }); |
|
92 } |
|
93 |
|
94 </script> |
|
95 </pre> |
|
96 </body> |
|
97 </html> |