2011-03-24 20:45:07 +00:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Test bug 482935</title>
|
|
|
|
<script type="text/javascript" src="/MochiKit/packed.js"></script>
|
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href=" /tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
<body onload="onWindowLoad()">
|
|
|
|
<script class="testbody" type="text/javascript">
|
|
|
|
|
|
|
|
var url = "bug482935.sjs";
|
|
|
|
|
|
|
|
function clearCache() {
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
Components.classes["@mozilla.org/network/cache-service;1"].
|
|
|
|
getService(Components.interfaces.nsICacheService).
|
|
|
|
evictEntries(Components.interfaces.nsICache.STORE_ANYWHERE);
|
|
|
|
netscape.security.PrivilegeManager.disablePrivilege("UniversalXPConnect");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that the response is cached if the request is cancelled
|
|
|
|
// after it has reached state 4
|
|
|
|
function testCancelInPhase4() {
|
|
|
|
|
|
|
|
clearCache();
|
|
|
|
|
|
|
|
// First request - should be loaded from server
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.addEventListener("readystatechange", function(e) {
|
|
|
|
if (xhr.readyState >= 4) {
|
2011-04-12 22:50:25 +00:00
|
|
|
xhr.addEventListener("abort", function() {
|
|
|
|
setTimeout(function() {
|
|
|
|
// This request was cancelled, so the responseText should be empty string
|
|
|
|
is(xhr.responseText, "", "Expected empty response to cancelled request");
|
|
|
|
|
|
|
|
// Second request - should be found in cache
|
|
|
|
var xhr2 = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr2.addEventListener("load", function() {
|
|
|
|
is(xhr2.responseText, "0", "Received fresh value for second request");
|
|
|
|
testCancelBeforePhase4();
|
|
|
|
}, false);
|
|
|
|
|
2011-04-20 19:42:09 +00:00
|
|
|
xhr2.open("GET", url);
|
2011-04-12 22:50:25 +00:00
|
|
|
xhr2.setRequestHeader("X-Request", "1", false);
|
|
|
|
|
|
|
|
try { xhr2.send(); }
|
|
|
|
catch(e) {
|
|
|
|
is(xhr2.status, "200", "Exception!");
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
}, false);
|
|
|
|
|
2011-03-24 20:45:07 +00:00
|
|
|
xhr.abort();
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
xhr.open("GET", url, true);
|
|
|
|
xhr.setRequestHeader("X-Request", "0", false);
|
|
|
|
try { xhr.send(); }
|
|
|
|
catch(e) {
|
|
|
|
is("Nothing", "Exception", "Boom: " + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that response is NOT cached if the request is cancelled
|
|
|
|
// before it has reached state 4
|
|
|
|
function testCancelBeforePhase4() {
|
|
|
|
|
|
|
|
clearCache();
|
|
|
|
|
|
|
|
// First request - should be loaded from server
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.addEventListener("readystatechange", function(e) {
|
|
|
|
if (xhr.readyState == 3) {
|
2011-04-12 22:50:25 +00:00
|
|
|
xhr.addEventListener("abort", function() {
|
|
|
|
setTimeout(function() {
|
|
|
|
// This request was cancelled, so the responseText should be empty string
|
|
|
|
is(xhr.responseText, "", "Expected empty response to cancelled request");
|
2011-03-24 20:45:07 +00:00
|
|
|
|
2011-04-12 22:50:25 +00:00
|
|
|
// Second request - should be found in cache
|
|
|
|
var xhr2 = new XMLHttpRequest();
|
2011-03-24 20:45:07 +00:00
|
|
|
|
2011-04-12 22:50:25 +00:00
|
|
|
xhr2.addEventListener("load", function() {
|
|
|
|
is(xhr2.responseText, "1", "Received cached value for second request");
|
|
|
|
SimpleTest.finish();
|
|
|
|
}, false);
|
2011-03-24 20:45:07 +00:00
|
|
|
|
2011-04-20 19:42:09 +00:00
|
|
|
xhr2.open("GET", url);
|
2011-04-12 22:50:25 +00:00
|
|
|
xhr2.setRequestHeader("X-Request", "1", false);
|
2011-03-24 20:45:07 +00:00
|
|
|
|
2011-04-12 22:50:25 +00:00
|
|
|
try { xhr2.send(); }
|
|
|
|
catch(e) {
|
|
|
|
is(xhr2.status, "200", "Exception!");
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
xhr.abort();
|
2011-03-24 20:45:07 +00:00
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
xhr.open("GET", url, true);
|
|
|
|
xhr.setRequestHeader("X-Request", "0", false);
|
|
|
|
try { xhr.send(); }
|
|
|
|
catch(e) {
|
|
|
|
is("Nothing", "Exception", "Boom: " + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onWindowLoad() {
|
|
|
|
testCancelInPhase4();
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|