2012-08-14 18:06:04 +04:00
|
|
|
Cu.import("resource://testing-common/httpd.js");
|
2015-12-22 03:29:26 +03:00
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
2016-08-31 00:54:58 +03:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2008-12-12 08:57:47 +03:00
|
|
|
|
2013-07-19 21:19:28 +04:00
|
|
|
var httpserver = new HttpServer();
|
|
|
|
httpserver.start(-1);
|
2008-12-12 08:57:47 +03:00
|
|
|
|
|
|
|
// Need to randomize, because apparently no one clears our cache
|
|
|
|
var suffix = Math.random();
|
2013-07-19 21:19:28 +04:00
|
|
|
var httpBase = "http://localhost:" + httpserver.identity.primaryPort;
|
2008-12-12 08:57:47 +03:00
|
|
|
var httpsBase = "http://localhost:4445";
|
|
|
|
var shortexpPath = "/shortexp" + suffix;
|
2013-06-29 05:16:43 +04:00
|
|
|
var longexpPath = "/longexp/" + suffix;
|
|
|
|
var longexp2Path = "/longexp/2/" + suffix;
|
2008-12-12 08:57:47 +03:00
|
|
|
var nocachePath = "/nocache" + suffix;
|
|
|
|
var nostorePath = "/nostore" + suffix;
|
2015-12-22 22:58:44 +03:00
|
|
|
var test410Path = "/test410" + suffix;
|
2015-12-23 00:06:07 +03:00
|
|
|
var test404Path = "/test404" + suffix;
|
2008-12-12 08:57:47 +03:00
|
|
|
|
2017-04-09 05:02:16 +03:00
|
|
|
var PrivateBrowsingLoadContext = Cc["@mozilla.org/privateloadcontext;1"].createInstance(Ci.nsILoadContext);
|
2012-07-23 02:35:33 +04:00
|
|
|
|
|
|
|
function make_channel(url, flags, usePrivateBrowsing) {
|
2016-04-13 16:34:36 +03:00
|
|
|
var securityFlags = Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL;
|
2016-08-31 00:54:58 +03:00
|
|
|
|
2017-01-09 22:27:26 +03:00
|
|
|
var uri = Services.io.newURI(url);
|
2016-08-31 00:54:58 +03:00
|
|
|
var principal = Services.scriptSecurityManager.createCodebasePrincipal(uri,
|
|
|
|
{ privateBrowsingId : usePrivateBrowsing ? 1 : 0 });
|
|
|
|
|
|
|
|
var req = NetUtil.newChannel({uri: uri,
|
|
|
|
loadingPrincipal: principal,
|
|
|
|
securityFlags: securityFlags,
|
|
|
|
contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER});
|
|
|
|
|
2008-12-12 08:57:47 +03:00
|
|
|
req.loadFlags = flags;
|
2012-07-23 02:35:33 +04:00
|
|
|
if (usePrivateBrowsing) {
|
2016-04-13 16:34:36 +03:00
|
|
|
req.notificationCallbacks = PrivateBrowsingLoadContext;
|
2012-07-23 02:35:33 +04:00
|
|
|
}
|
2008-12-12 08:57:47 +03:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2012-07-23 02:35:33 +04:00
|
|
|
function Test(path, flags, expectSuccess, readFromCache, hitServer,
|
|
|
|
usePrivateBrowsing /* defaults to false */) {
|
2008-12-12 08:57:47 +03:00
|
|
|
this.path = path;
|
|
|
|
this.flags = flags;
|
|
|
|
this.expectSuccess = expectSuccess;
|
|
|
|
this.readFromCache = readFromCache;
|
|
|
|
this.hitServer = hitServer;
|
2012-07-23 02:35:33 +04:00
|
|
|
this.usePrivateBrowsing = usePrivateBrowsing;
|
2008-12-12 08:57:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Test.prototype = {
|
|
|
|
flags: 0,
|
|
|
|
expectSuccess: true,
|
|
|
|
readFromCache: false,
|
|
|
|
hitServer: true,
|
2012-07-23 02:35:33 +04:00
|
|
|
usePrivateBrowsing: false,
|
2008-12-12 08:57:47 +03:00
|
|
|
_buffer: "",
|
|
|
|
_isFromCache: false,
|
|
|
|
|
|
|
|
QueryInterface: function(iid) {
|
|
|
|
if (iid.equals(Components.interfaces.nsIStreamListener) ||
|
|
|
|
iid.equals(Components.interfaces.nsIRequestObserver) ||
|
|
|
|
iid.equals(Components.interfaces.nsISupports))
|
|
|
|
return this;
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
onStartRequest: function(request, context) {
|
2012-07-23 02:35:33 +04:00
|
|
|
var cachingChannel = request.QueryInterface(Ci.nsICacheInfoChannel);
|
2008-12-12 08:57:47 +03:00
|
|
|
this._isFromCache = request.isPending() && cachingChannel.isFromCache();
|
|
|
|
},
|
|
|
|
|
|
|
|
onDataAvailable: function(request, context, stream, offset, count) {
|
|
|
|
this._buffer = this._buffer.concat(read_stream(stream, count));
|
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest: function(request, context, status) {
|
|
|
|
do_check_eq(Components.isSuccessCode(status), this.expectSuccess);
|
|
|
|
do_check_eq(this._isFromCache, this.readFromCache);
|
|
|
|
do_check_eq(gHitServer, this.hitServer);
|
|
|
|
|
2009-11-24 22:58:42 +03:00
|
|
|
do_timeout(0, run_next_test);
|
2008-12-12 08:57:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
run: function() {
|
|
|
|
dump("Running:" +
|
|
|
|
"\n " + this.path +
|
|
|
|
"\n " + this.flags +
|
|
|
|
"\n " + this.expectSuccess +
|
|
|
|
"\n " + this.readFromCache +
|
|
|
|
"\n " + this.hitServer + "\n");
|
|
|
|
gHitServer = false;
|
2012-07-23 02:35:33 +04:00
|
|
|
var channel = make_channel(this.path, this.flags, this.usePrivateBrowsing);
|
2015-12-22 03:29:26 +03:00
|
|
|
channel.asyncOpen2(this);
|
2008-12-12 08:57:47 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var gHitServer = false;
|
|
|
|
|
|
|
|
var gTests = [
|
2012-07-23 02:35:33 +04:00
|
|
|
|
|
|
|
new Test(httpBase + shortexpPath, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true, // hit server
|
|
|
|
true), // USE PRIVATE BROWSING, so not cached for later requests
|
2008-12-12 08:57:47 +03:00
|
|
|
new Test(httpBase + shortexpPath, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + shortexpPath, 0,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + shortexpPath, Ci.nsIRequest.LOAD_BYPASS_CACHE,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + shortexpPath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
|
|
|
|
false, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
new Test(httpBase + shortexpPath,
|
|
|
|
Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
|
|
|
|
Ci.nsIRequest.VALIDATE_NEVER,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
new Test(httpBase + shortexpPath, Ci.nsIRequest.LOAD_FROM_CACHE,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
|
|
|
|
new Test(httpBase + longexpPath, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + longexpPath, 0,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
new Test(httpBase + longexpPath, Ci.nsIRequest.LOAD_BYPASS_CACHE,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + longexpPath,
|
|
|
|
Ci.nsIRequest.VALIDATE_ALWAYS,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + longexpPath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
new Test(httpBase + longexpPath,
|
|
|
|
Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
|
|
|
|
Ci.nsIRequest.VALIDATE_NEVER,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
new Test(httpBase + longexpPath,
|
|
|
|
Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
|
|
|
|
Ci.nsIRequest.VALIDATE_ALWAYS,
|
|
|
|
false, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
new Test(httpBase + longexpPath, Ci.nsIRequest.LOAD_FROM_CACHE,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
|
2013-06-29 05:16:43 +04:00
|
|
|
new Test(httpBase + longexp2Path, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + longexp2Path, 0,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
|
2008-12-12 08:57:47 +03:00
|
|
|
new Test(httpBase + nocachePath, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + nocachePath, 0,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + nocachePath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
|
|
|
|
false, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
false), // hit server
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
// CACHE2: mayhemer - entry is doomed... I think the logic is wrong, we should not doom them
|
|
|
|
// as they are not valid, but take them as they need to reval
|
|
|
|
/*
|
2008-12-12 08:57:47 +03:00
|
|
|
new Test(httpBase + nocachePath, Ci.nsIRequest.LOAD_FROM_CACHE,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
2013-09-20 13:11:25 +04:00
|
|
|
*/
|
|
|
|
|
2008-12-12 08:57:47 +03:00
|
|
|
// LOAD_ONLY_FROM_CACHE would normally fail (because no-cache forces
|
|
|
|
// a validation), but VALIDATE_NEVER should override that.
|
|
|
|
new Test(httpBase + nocachePath,
|
|
|
|
Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
|
|
|
|
Ci.nsIRequest.VALIDATE_NEVER,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
|
|
|
|
// ... however, no-cache over ssl should act like no-store and force
|
|
|
|
// a validation (and therefore failure) even if VALIDATE_NEVER is
|
|
|
|
// set.
|
|
|
|
/* XXX bug 466524: We can't currently start an ssl server in xpcshell tests,
|
|
|
|
so this test is currently disabled.
|
|
|
|
new Test(httpsBase + nocachePath,
|
|
|
|
Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
|
|
|
|
Ci.nsIRequest.VALIDATE_NEVER,
|
|
|
|
false, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
false) // hit server
|
|
|
|
*/
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2008-12-12 08:57:47 +03:00
|
|
|
new Test(httpBase + nostorePath, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + nostorePath, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + nostorePath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
|
|
|
|
false, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
new Test(httpBase + nostorePath, Ci.nsIRequest.LOAD_FROM_CACHE,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
|
|
|
false), // hit server
|
|
|
|
// no-store should force the validation (and therefore failure, with
|
|
|
|
// LOAD_ONLY_FROM_CACHE) even if VALIDATE_NEVER is set.
|
|
|
|
new Test(httpBase + nostorePath,
|
|
|
|
Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
|
|
|
|
Ci.nsIRequest.VALIDATE_NEVER,
|
|
|
|
false, // expect success
|
|
|
|
false, // read from cache
|
2015-12-22 22:58:44 +03:00
|
|
|
false), // hit server
|
|
|
|
|
|
|
|
new Test(httpBase + test410Path, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + test410Path, 0,
|
|
|
|
true, // expect success
|
|
|
|
true, // read from cache
|
2015-12-23 00:06:07 +03:00
|
|
|
false), // hit server
|
|
|
|
|
|
|
|
new Test(httpBase + test404Path, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true), // hit server
|
|
|
|
new Test(httpBase + test404Path, 0,
|
|
|
|
true, // expect success
|
|
|
|
false, // read from cache
|
|
|
|
true) // hit server
|
2015-12-22 22:58:44 +03:00
|
|
|
];
|
2008-12-12 08:57:47 +03:00
|
|
|
|
|
|
|
function run_next_test()
|
|
|
|
{
|
|
|
|
if (gTests.length == 0) {
|
2009-04-16 00:19:35 +04:00
|
|
|
httpserver.stop(do_test_finished);
|
2008-12-12 08:57:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var test = gTests.shift();
|
|
|
|
test.run();
|
|
|
|
}
|
|
|
|
|
2015-12-22 22:58:44 +03:00
|
|
|
function handler(httpStatus, metadata, response) {
|
2008-12-12 08:57:47 +03:00
|
|
|
gHitServer = true;
|
|
|
|
try {
|
|
|
|
var etag = metadata.getHeader("If-None-Match");
|
|
|
|
} catch(ex) {
|
|
|
|
var etag = "";
|
|
|
|
}
|
|
|
|
if (etag == "testtag") {
|
|
|
|
// Allow using the cached data
|
|
|
|
response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
|
|
|
|
} else {
|
2015-12-22 22:58:44 +03:00
|
|
|
response.setStatusLine(metadata.httpVersion, httpStatus, "Useless Phrase");
|
2008-12-12 08:57:47 +03:00
|
|
|
response.setHeader("Content-Type", "text/plain", false);
|
|
|
|
response.setHeader("ETag", "testtag", false);
|
|
|
|
const body = "data";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function nocache_handler(metadata, response) {
|
|
|
|
response.setHeader("Cache-Control", "no-cache", false);
|
2015-12-22 22:58:44 +03:00
|
|
|
handler(200, metadata, response);
|
2008-12-12 08:57:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function nostore_handler(metadata, response) {
|
|
|
|
response.setHeader("Cache-Control", "no-store", false);
|
2015-12-22 22:58:44 +03:00
|
|
|
handler(200, metadata, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test410_handler(metadata, response) {
|
|
|
|
handler(410, metadata, response);
|
2008-12-12 08:57:47 +03:00
|
|
|
}
|
|
|
|
|
2015-12-23 00:06:07 +03:00
|
|
|
function test404_handler(metadata, response) {
|
|
|
|
handler(404, metadata, response);
|
|
|
|
}
|
|
|
|
|
2008-12-12 08:57:47 +03:00
|
|
|
function shortexp_handler(metadata, response) {
|
|
|
|
response.setHeader("Cache-Control", "max-age=0", false);
|
2015-12-22 22:58:44 +03:00
|
|
|
handler(200, metadata, response);
|
2008-12-12 08:57:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function longexp_handler(metadata, response) {
|
|
|
|
response.setHeader("Cache-Control", "max-age=10000", false);
|
2015-12-22 22:58:44 +03:00
|
|
|
handler(200, metadata, response);
|
2008-12-12 08:57:47 +03:00
|
|
|
}
|
|
|
|
|
2013-06-29 05:16:43 +04:00
|
|
|
// test spaces around max-age value token
|
|
|
|
function longexp2_handler(metadata, response) {
|
|
|
|
response.setHeader("Cache-Control", "max-age = 10000", false);
|
2015-12-22 22:58:44 +03:00
|
|
|
handler(200, metadata, response);
|
2013-06-29 05:16:43 +04:00
|
|
|
}
|
|
|
|
|
2008-12-12 08:57:47 +03:00
|
|
|
function run_test() {
|
|
|
|
httpserver.registerPathHandler(shortexpPath, shortexp_handler);
|
|
|
|
httpserver.registerPathHandler(longexpPath, longexp_handler);
|
2013-06-29 05:16:43 +04:00
|
|
|
httpserver.registerPathHandler(longexp2Path, longexp2_handler);
|
2008-12-12 08:57:47 +03:00
|
|
|
httpserver.registerPathHandler(nocachePath, nocache_handler);
|
|
|
|
httpserver.registerPathHandler(nostorePath, nostore_handler);
|
2015-12-22 22:58:44 +03:00
|
|
|
httpserver.registerPathHandler(test410Path, test410_handler);
|
2015-12-23 00:06:07 +03:00
|
|
|
httpserver.registerPathHandler(test404Path, test404_handler);
|
2008-12-12 08:57:47 +03:00
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
do_test_pending();
|
|
|
|
}
|