content/base/test/test_XHR_parameters.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_XHR_parameters.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,97 @@
     1.4 +
     1.5 +
     1.6 +<!DOCTYPE html>
     1.7 +<html>
     1.8 +<head>
     1.9 +  <meta charset="utf-8">
    1.10 +  <title>Test for XMLHttpRequest with system privileges</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body onload="runTests();">
    1.15 +<p id="display">
    1.16 +</p>
    1.17 +<div id="content" style="display: none">
    1.18 +
    1.19 +</div>
    1.20 +<pre id="test">
    1.21 +<script class="testbody" type="application/javascript;version=1.8">
    1.22 +
    1.23 +function runTests() {
    1.24 +  SimpleTest.waitForExplicitFinish();
    1.25 +
    1.26 +  let validParameters = [
    1.27 +    undefined,
    1.28 +    null,
    1.29 +    {},
    1.30 +    {mozSystem: ""},
    1.31 +    {mozSystem: 0},
    1.32 +    {mozAnon: 1},
    1.33 +    {mozAnon: []},
    1.34 +    {get mozAnon() { return true; }},
    1.35 +    0,
    1.36 +    7,
    1.37 +    Math.PI,
    1.38 +    "string",
    1.39 +    true,
    1.40 +    false,
    1.41 +  ];
    1.42 +
    1.43 +  let invalidParameters = [
    1.44 +    {get mozSystem() { throw "Bla"; } },
    1.45 +  ];
    1.46 +
    1.47 +  let havePrivileges = false;
    1.48 +
    1.49 +  function testValidParameter(value) {
    1.50 +    let xhr;
    1.51 +    try {
    1.52 +      xhr = new XMLHttpRequest(value);
    1.53 +    } catch (ex) {
    1.54 +      ok(false, "Got unexpected exception: " + ex);
    1.55 +      return;
    1.56 +    }
    1.57 +    ok(xhr instanceof XMLHttpRequest, "passed " + JSON.stringify(value));
    1.58 +
    1.59 +    // If the page doesnt have privileges to create a system XHR,
    1.60 +    // this flag will always be false no matter what is passed.
    1.61 +    let expectedAnon = Boolean(value && value.mozAnon);
    1.62 +    let expectedSystem = false;
    1.63 +    if (havePrivileges) {
    1.64 +      expectedSystem = Boolean(value && value.mozSystem);
    1.65 +    }
    1.66 +    is(xhr.mozAnon, expectedAnon, "testing mozAnon");
    1.67 +    is(xhr.mozSystem, expectedSystem, "testing mozSystem");
    1.68 +  }
    1.69 +
    1.70 +  function testInvalidParameter(value) {
    1.71 +    let expectedError;
    1.72 +    try {
    1.73 +      new XMLHttpRequest(value);
    1.74 +      ok(false, "invalid parameter did not cause exception: " +
    1.75 +         JSON.stringify(value));
    1.76 +    } catch (ex) {
    1.77 +      expectedError = ex;
    1.78 +    }
    1.79 +    ok(expectedError, "invalid parameter raised exception as expected: " +
    1.80 +       JSON.stringify(expectedError))
    1.81 +  }
    1.82 +
    1.83 +  // Run the tests once without API privileges...
    1.84 +  validParameters.forEach(testValidParameter);
    1.85 +  invalidParameters.forEach(testInvalidParameter);
    1.86 +
    1.87 +  // ...and once with privileges.
    1.88 +  havePrivileges = true;
    1.89 +  SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], function() {
    1.90 +    validParameters.forEach(testValidParameter);
    1.91 +    invalidParameters.forEach(testInvalidParameter);
    1.92 +
    1.93 +    SimpleTest.finish();
    1.94 +  });
    1.95 +}
    1.96 +
    1.97 +</script>
    1.98 +</pre>
    1.99 +</body>
   1.100 +</html>

mercurial