content/base/test/test_XHR_parameters.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     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">
    16 </div>
    17 <pre id="test">
    18 <script class="testbody" type="application/javascript;version=1.8">
    20 function runTests() {
    21   SimpleTest.waitForExplicitFinish();
    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   ];
    40   let invalidParameters = [
    41     {get mozSystem() { throw "Bla"; } },
    42   ];
    44   let havePrivileges = false;
    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));
    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   }
    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   }
    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);
    90     SimpleTest.finish();
    91   });
    92 }
    94 </script>
    95 </pre>
    96 </body>
    97 </html>

mercurial