summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/test_XHR_system.html
blob: 1d61e3f77270f1a23613d5be19b8f76ab0f07900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for XMLHttpRequest with system privileges</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="runTests();">
<p id="display">
</p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.8">

let tests = [];

const PROTECTED_URL = "file:///etc/passwd";
const REDIRECT_URL = window.location.protocol + "//" + window.location.host + "/tests/dom/xhr/tests/file_XHR_system_redirect.html";
const CROSSSITE_URL = "http://example.com/tests/dom/xhr/tests/test_XHR_system.html";

tests.push(function test_cross_origin() {
  // System XHR can load cross-origin resources.

  is(window.location.hostname, "mochi.test", "correct origin");

  let xhr = new XMLHttpRequest({mozSystem: true});
  is(xhr.mozSystem, true, ".mozSystem == true");
  xhr.open("GET", CROSSSITE_URL);
  xhr.onload = function onload() {
    is(xhr.status, 200, "correct HTTP status");
    ok(xhr.responseText != null, "HTTP response non-null");
    ok(xhr.responseText.length, "HTTP response not empty");
    runNextTest();
  };
  xhr.onerror = function onerror(event) {
    ok(false, "Got an error event: " + event);
    runNextTest();
  }
  xhr.send();
});

tests.push(function test_file_uri() {
  // System XHR is not permitted to access file:/// URIs.

  let xhr = new XMLHttpRequest({mozSystem: true});
  is(xhr.mozSystem, true, ".mozSystem == true");
  xhr.open("GET", PROTECTED_URL);
  xhr.onload = function() {
    ok(false, "Should not have loaded");
    runNextTest();
  }
  xhr.onerror = function(event) {
    ok(true, "Got an error event: " + event);
    is(xhr.status, 0, "HTTP status is 0");
    runNextTest();
  }
  xhr.send();
});

tests.push(function test_redirect_to_file_uri() {
  // System XHR won't load file:/// URIs even if an HTTP resource redirects there.

  let xhr = new XMLHttpRequest({mozSystem: true});
  is(xhr.mozSystem, true, ".mozSystem == true");
  xhr.open("GET", REDIRECT_URL);
  xhr.onload = function onload() {
    ok(false, "Should not have loaded");
    runNextTest();
  };
  xhr.onerror = function onerror(event) {
    ok(true, "Got an error event: " + event);
    is(xhr.status, 0, "HTTP status is 0");
    runNextTest();
  }
  xhr.send();
});


function runNextTest() {
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }
  tests.shift()();
}

function runTests() {
  SimpleTest.waitForExplicitFinish();

  SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runNextTest);
}

</script>
</pre>
</body>
</html>