content/base/test/test_XHR_parameters.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial