content/base/test/test_XHR_system.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_XHR_system.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +<!DOCTYPE html>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <meta charset="utf-8">
     1.8 +  <title>Test for XMLHttpRequest with system privileges</title>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body onload="runTests();">
    1.13 +<p id="display">
    1.14 +</p>
    1.15 +<div id="content" style="display: none">
    1.16 +  
    1.17 +</div>
    1.18 +<pre id="test">
    1.19 +<script class="testbody" type="application/javascript;version=1.8">
    1.20 +
    1.21 +let tests = [];
    1.22 +
    1.23 +const PROTECTED_URL = "file:///etc/passwd";
    1.24 +const REDIRECT_URL = window.location.protocol + "//" + window.location.host + "/tests/content/base/test/file_XHR_system_redirect.html";
    1.25 +const CROSSSITE_URL = "http://example.com/tests/content/base/test/test_XHR_system.html";
    1.26 +
    1.27 +tests.push(function test_cross_origin() {
    1.28 +  // System XHR can load cross-origin resources.
    1.29 +
    1.30 +  is(window.location.hostname, "mochi.test", "correct origin");
    1.31 +
    1.32 +  let xhr = new XMLHttpRequest({mozSystem: true});
    1.33 +  is(xhr.mozSystem, true, ".mozSystem == true");
    1.34 +  xhr.open("GET", CROSSSITE_URL);
    1.35 +  xhr.onload = function onload() {
    1.36 +    is(xhr.status, 200, "correct HTTP status");
    1.37 +    ok(xhr.responseText != null, "HTTP response non-null");
    1.38 +    ok(xhr.responseText.length, "HTTP response not empty");
    1.39 +    runNextTest();
    1.40 +  };
    1.41 +  xhr.onerror = function onerror(event) {
    1.42 +    ok(false, "Got an error event: " + event);
    1.43 +    runNextTest();
    1.44 +  }
    1.45 +  xhr.send();
    1.46 +});
    1.47 +
    1.48 +tests.push(function test_file_uri() {
    1.49 +  // System XHR is not permitted to access file:/// URIs.
    1.50 +
    1.51 +  let xhr = new XMLHttpRequest({mozSystem: true});
    1.52 +  is(xhr.mozSystem, true, ".mozSystem == true");
    1.53 +  xhr.open("GET", PROTECTED_URL);
    1.54 +  let error;
    1.55 +  try {
    1.56 +    xhr.send();
    1.57 +  } catch (ex) {
    1.58 +    error = ex;
    1.59 +  }
    1.60 +  ok(!!error, "got exception");
    1.61 +  is(error.name, "NS_ERROR_DOM_BAD_URI");
    1.62 +  is(error.message, "Access to restricted URI denied");
    1.63 +
    1.64 +  runNextTest();
    1.65 +});
    1.66 +
    1.67 +tests.push(function test_redirect_to_file_uri() {
    1.68 +  // System XHR won't load file:/// URIs even if an HTTP resource redirects there.
    1.69 +
    1.70 +  let xhr = new XMLHttpRequest({mozSystem: true});
    1.71 +  is(xhr.mozSystem, true, ".mozSystem == true");
    1.72 +  xhr.open("GET", REDIRECT_URL);
    1.73 +  xhr.onload = function onload() {
    1.74 +    ok(false, "Should not have loaded");
    1.75 +    runNextTest();
    1.76 +  };
    1.77 +  xhr.onerror = function onerror(event) {
    1.78 +    ok(true, "Got an error event: " + event);
    1.79 +    is(xhr.status, 0, "HTTP status is 0");
    1.80 +    runNextTest();
    1.81 +  }
    1.82 +  xhr.send();
    1.83 +});
    1.84 +
    1.85 +
    1.86 +function runNextTest() {
    1.87 +  if (!tests.length) {
    1.88 +    SimpleTest.finish();
    1.89 +    return;
    1.90 +  }
    1.91 +  tests.shift()();
    1.92 +}
    1.93 +
    1.94 +function runTests() {
    1.95 +  SimpleTest.waitForExplicitFinish();
    1.96 +
    1.97 +  SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runNextTest);
    1.98 +}
    1.99 +
   1.100 +</script>
   1.101 +</pre>
   1.102 +</body>
   1.103 +</html>

mercurial