2012-06-11 03:44:50 +04:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>Test for XMLHttpRequest with system privileges</title>
|
2020-06-18 01:45:31 +03:00
|
|
|
<script src="/tests/SimpleTest/SimpleTest.js"></script>
|
2012-06-11 03:44:50 +04:00
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
2013-03-29 16:43:27 +04:00
|
|
|
<body onload="setup();">
|
2012-06-11 03:44:50 +04:00
|
|
|
<p id="display">
|
|
|
|
<iframe id="loader"></iframe>
|
|
|
|
</p>
|
|
|
|
<div id="content" style="display: none">
|
2020-06-18 01:45:31 +03:00
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
</div>
|
|
|
|
<pre id="test">
|
2017-02-23 00:10:07 +03:00
|
|
|
<script class="testbody" type="application/javascript">
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
// An XHR with the anon flag set will not send cookie and auth information.
|
2016-06-27 20:13:40 +03:00
|
|
|
const TEST_URL = "http://example.com/tests/dom/xhr/tests/file_XHR_anon.sjs";
|
2012-10-24 18:05:13 +04:00
|
|
|
document.cookie = "foo=bar";
|
|
|
|
|
|
|
|
let am = {
|
|
|
|
authMgr: null,
|
|
|
|
|
|
|
|
init() {
|
2013-02-25 22:43:03 +04:00
|
|
|
this.authMgr = SpecialPowers.Cc["@mozilla.org/network/http-auth-manager;1"]
|
|
|
|
.getService(SpecialPowers.Ci.nsIHttpAuthManager)
|
2012-10-24 18:05:13 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
addIdentity() {
|
|
|
|
this.authMgr.setAuthIdentity("http", "example.com", -1, "basic", "testrealm",
|
|
|
|
"", "example.com", "user1", "password1");
|
|
|
|
},
|
|
|
|
|
|
|
|
tearDown() {
|
|
|
|
this.authMgr.clearAll();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = [ test1, test2, test2a, test3, test3, test3, test4, test4, test4, test5, test5, test5 ];
|
2012-06-11 03:44:50 +04:00
|
|
|
|
|
|
|
function runTests() {
|
2012-10-24 18:05:13 +04:00
|
|
|
if (!tests.length) {
|
|
|
|
am.tearDown();
|
2016-06-27 20:13:40 +03:00
|
|
|
|
|
|
|
// Resetting the cookie.
|
|
|
|
document.cookie = "foo=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
2012-10-24 18:05:13 +04:00
|
|
|
SimpleTest.finish();
|
|
|
|
return;
|
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
var test = tests.shift();
|
|
|
|
test();
|
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
function test1() {
|
|
|
|
am.addIdentity();
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
|
|
|
|
is(xhr.mozAnon, true, "test1: .mozAnon == true");
|
|
|
|
xhr.open("GET", TEST_URL);
|
|
|
|
xhr.onload = function onload() {
|
|
|
|
is(xhr.status, 200, "test1: " + xhr.responseText);
|
|
|
|
am.tearDown();
|
|
|
|
runTests();
|
|
|
|
};
|
|
|
|
xhr.onerror = function onerror() {
|
|
|
|
ok(false, "Got an error event!");
|
|
|
|
am.tearDown();
|
|
|
|
runTests();
|
|
|
|
}
|
|
|
|
xhr.send();
|
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
function test2() {
|
|
|
|
am.addIdentity();
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
|
|
|
|
is(xhr.mozAnon, true, "test2: .mozAnon == true");
|
|
|
|
xhr.open("GET", TEST_URL + "?expectAuth=true", true,
|
|
|
|
"user2name", "pass2word");
|
|
|
|
xhr.onload = function onload() {
|
|
|
|
is(xhr.status, 200, "test2: " + xhr.responseText);
|
|
|
|
let response = JSON.parse(xhr.responseText);
|
|
|
|
is(response.authorization, "Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
|
|
|
|
am.tearDown();
|
|
|
|
runTests();
|
|
|
|
};
|
|
|
|
xhr.onerror = function onerror() {
|
|
|
|
ok(false, "Got an error event!");
|
|
|
|
am.tearDown();
|
|
|
|
runTests();
|
|
|
|
}
|
|
|
|
xhr.send();
|
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
function test2a() {
|
|
|
|
am.addIdentity();
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
|
|
|
|
is(xhr.mozAnon, true, "test2: .mozAnon == true");
|
|
|
|
xhr.open("GET", TEST_URL + "?expectAuth=true", true,
|
|
|
|
"user1", "pass2word");
|
|
|
|
xhr.onload = function onload() {
|
|
|
|
is(xhr.status, 200, "test2: " + xhr.responseText);
|
|
|
|
let response = JSON.parse(xhr.responseText);
|
|
|
|
is(response.authorization, "Basic dXNlcjE6cGFzczJ3b3Jk");
|
|
|
|
am.tearDown();
|
|
|
|
runTests();
|
|
|
|
};
|
|
|
|
xhr.onerror = function onerror() {
|
|
|
|
ok(false, "Got an error event!");
|
|
|
|
am.tearDown();
|
|
|
|
runTests();
|
|
|
|
}
|
|
|
|
xhr.send();
|
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
function test3() {
|
|
|
|
am.addIdentity();
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
|
|
|
|
is(xhr.mozAnon, true, "test3: .mozAnon == true");
|
|
|
|
xhr.open("GET", TEST_URL + "?expectAuth=true", true);
|
|
|
|
xhr.onload = function onload() {
|
|
|
|
is(xhr.status, 401, "test3: " + xhr.responseText);
|
|
|
|
am.tearDown();
|
|
|
|
runTests();
|
|
|
|
};
|
|
|
|
xhr.onerror = function onerror() {
|
|
|
|
ok(false, "Got an error event!");
|
|
|
|
am.tearDown();
|
|
|
|
runTests();
|
2012-06-11 03:44:50 +04:00
|
|
|
}
|
2012-10-24 18:05:13 +04:00
|
|
|
xhr.send();
|
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
function test4() {
|
|
|
|
let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
|
|
|
|
is(xhr.mozAnon, true, "test4: .mozAnon == true");
|
|
|
|
xhr.open("GET", TEST_URL + "?expectAuth=true", true);
|
|
|
|
xhr.onload = function onload() {
|
|
|
|
is(xhr.status, 401, "test4: " + xhr.responseText);
|
|
|
|
runTests();
|
|
|
|
};
|
|
|
|
xhr.onerror = function onerror() {
|
|
|
|
ok(false, "Got an error event!");
|
|
|
|
runTests();
|
2012-06-11 03:44:50 +04:00
|
|
|
}
|
2012-10-24 18:05:13 +04:00
|
|
|
xhr.send();
|
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
function test5() {
|
|
|
|
let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
|
|
|
|
is(xhr.mozAnon, true, "test5: .mozAnon == true");
|
|
|
|
xhr.open("GET", TEST_URL + "?expectAuth=true", true,
|
|
|
|
"user2name", "pass2word");
|
|
|
|
xhr.onload = function onload() {
|
|
|
|
is(xhr.status, 200, "test5: " + xhr.responseText);
|
|
|
|
let response = JSON.parse(xhr.responseText);
|
|
|
|
is(response.authorization, "Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
|
|
|
|
runTests();
|
|
|
|
};
|
|
|
|
xhr.onerror = function onerror() {
|
|
|
|
ok(false, "Got an error event!");
|
|
|
|
runTests();
|
|
|
|
}
|
|
|
|
xhr.send();
|
2012-06-11 03:44:50 +04:00
|
|
|
}
|
|
|
|
|
2013-03-29 16:43:27 +04:00
|
|
|
function setup() {
|
|
|
|
am.init();
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runTests);
|
|
|
|
}
|
2012-06-11 03:44:50 +04:00
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|