gecko/content/base/test/file_CrossSiteXHR2_inner.html

105 lines
2.4 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script class="testbody" type="text/javascript">
function doFail(msg)
{
document.write(msg + "<p>");
parent.location.hash = "#fail";
throw 1;
}
var local = "http://example.com/tests/content/base/test/";
var passFiles = [[local + 'file_CrossSiteXHR2_pass1.xml', 'GET'],
[local + 'file_CrossSiteXHR2_pass2.xml', 'GET'],
[local + 'file_CrossSiteXHR2_pass3.xml', 'GET']
];
var failFiles = [[local + 'file_CrossSiteXHR2_fail1.xml', 'GET'],
[local + 'file_CrossSiteXHR2_fail2.xml', 'GET']
];
netscape.security.PrivilegeManager.enablePrivilege(
"UniversalXPConnect UniversalBrowserWrite");
for (i = 0; i < passFiles.length; ++i)
{
xhr = new XMLHttpRequest();
if (!xhr)
doFail("Couldn't open request : " + passFiles[i][0]);
try {
xhr.open(passFiles[i][1], passFiles[i][0], false);
}
catch(e) {
doFail("Open failed : " + passFiles[i][0]);
}
// Do the send
try {
xhr.send(null);
}
catch(e) {
doFail("Sending failed : " + passFiles[i][0]);
}
// Check the Access-Control-Origin
var channel =
xhr.channel.QueryInterface(Components.interfaces.nsIHttpChannel);
var value = null;
try {
value = channel.getRequestHeader("Access-Control-Origin");
}
catch(e) {
doFail("Getting request header failed : " + passFiles[i][0]);
}
if (value != "http://sub1.xn--lt-uia.example.org:8000")
doFail("Referer-root incorrect : " + value);
// Check the response
if (xhr.status != "200") {
doFail("Status incorrect : " + passFiles[i][0]);
}
if (xhr.responseXML) {
responseString = (new XMLSerializer()).serializeToString(
xhr.responseXML.documentElement);
if (responseString != "<res>hello</res>") {
doFail("Response incorrect : " + passFiles[i][0]);
}
}
else {
doFail("No response : " + passFiles[i][0]);
}
}
for (i = 0; i < failFiles.length; ++i)
{
xhr = new XMLHttpRequest();
if (!xhr)
doFail("Couldn't open request : " + passFiles[i][0]);
success = false;
try {
xhr.open(failFiles[i][1], failFiles[i][0], false);
xhr.send(null);
}
catch (e) {
success = true;
}
if (!success) {
doFail("Test did not fail : " + failFiles[i][0]);
}
}
document.write("Success!");
parent.location.hash = "#done";
</script>
</body>
</html>