5 <title>Test for XMLHttpRequest with system privileges
</title>
6 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
9 <body onload=
"setup();">
11 <iframe id=
"loader"></iframe>
13 <div id=
"content" style=
"display: none">
17 <script class=
"testbody" type=
"application/javascript">
19 // An XHR with the anon flag set will not send cookie and auth information.
20 const TEST_URL =
"http://example.com/tests/dom/xhr/tests/file_XHR_anon.sjs";
21 document.cookie =
"foo=bar";
27 this.authMgr = SpecialPowers.Cc[
"@mozilla.org/network/http-auth-manager;1"]
28 .getService(SpecialPowers.Ci.nsIHttpAuthManager)
32 this.authMgr.setAuthIdentity(
"http",
"example.com", -
1,
"basic",
"testrealm",
33 "",
"example.com",
"user1",
"password1");
37 this.authMgr.clearAll();
41 var tests = [ test1, test2, test2a, test3, test3, test3, test4, test4, test4, test5, test5, test5 ];
47 // Resetting the cookie.
48 document.cookie =
"foo=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
53 var test = tests.shift();
60 let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
61 is(xhr.mozAnon, true,
"test1: .mozAnon == true");
62 xhr.open(
"GET", TEST_URL);
63 xhr.onload = function onload() {
64 is(xhr.status,
200,
"test1: " + xhr.responseText);
68 xhr.onerror = function onerror() {
69 ok(false,
"Got an error event!");
79 let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
80 is(xhr.mozAnon, true,
"test2: .mozAnon == true");
81 xhr.open(
"GET", TEST_URL +
"?expectAuth=true", true,
82 "user2name",
"pass2word");
83 xhr.onload = function onload() {
84 is(xhr.status,
200,
"test2: " + xhr.responseText);
85 let response = JSON.parse(xhr.responseText);
86 is(response.authorization,
"Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
90 xhr.onerror = function onerror() {
91 ok(false,
"Got an error event!");
101 let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
102 is(xhr.mozAnon, true,
"test2: .mozAnon == true");
103 xhr.open(
"GET", TEST_URL +
"?expectAuth=true", true,
104 "user1",
"pass2word");
105 xhr.onload = function onload() {
106 is(xhr.status,
200,
"test2: " + xhr.responseText);
107 let response = JSON.parse(xhr.responseText);
108 is(response.authorization,
"Basic dXNlcjE6cGFzczJ3b3Jk");
112 xhr.onerror = function onerror() {
113 ok(false,
"Got an error event!");
123 let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
124 is(xhr.mozAnon, true,
"test3: .mozAnon == true");
125 xhr.open(
"GET", TEST_URL +
"?expectAuth=true", true);
126 xhr.onload = function onload() {
127 is(xhr.status,
401,
"test3: " + xhr.responseText);
131 xhr.onerror = function onerror() {
132 ok(false,
"Got an error event!");
140 let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
141 is(xhr.mozAnon, true,
"test4: .mozAnon == true");
142 xhr.open(
"GET", TEST_URL +
"?expectAuth=true", true);
143 xhr.onload = function onload() {
144 is(xhr.status,
401,
"test4: " + xhr.responseText);
147 xhr.onerror = function onerror() {
148 ok(false,
"Got an error event!");
155 let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
156 is(xhr.mozAnon, true,
"test5: .mozAnon == true");
157 xhr.open(
"GET", TEST_URL +
"?expectAuth=true", true,
158 "user2name",
"pass2word");
159 xhr.onload = function onload() {
160 is(xhr.status,
200,
"test5: " + xhr.responseText);
161 let response = JSON.parse(xhr.responseText);
162 is(response.authorization,
"Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
165 xhr.onerror = function onerror() {
166 ok(false,
"Got an error event!");
174 SimpleTest.waitForExplicitFinish();
175 SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runTests);