mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-21 01:05:45 +00:00
918ed6c474
This was done using the following script:
37e3803c7a/processors/chromeutils-import.jsm
MozReview-Commit-ID: 1Nc3XDu0wGl
--HG--
extra : source : 12fc4dee861c812fd2bd032c63ef17af61800c70
extra : intermediate-source : 34c999fa006bffe8705cf50c54708aa21a962e62
extra : histedit_source : b2be2c5e5d226e6c347312456a6ae339c1e634b0
149 lines
4.9 KiB
JavaScript
149 lines
4.9 KiB
JavaScript
/**
|
|
* Test for the "CacheEntryId" under several cases.
|
|
*/
|
|
|
|
ChromeUtils.import("resource://testing-common/httpd.js");
|
|
ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "URL", function() {
|
|
return "http://localhost:" + httpServer.identity.primaryPort + "/content";
|
|
});
|
|
|
|
var httpServer = null;
|
|
|
|
const responseContent = "response body";
|
|
const responseContent2 = "response body 2";
|
|
const altContent = "!@#$%^&*()";
|
|
const altContentType = "text/binary";
|
|
|
|
function isParentProcess() {
|
|
let appInfo = Cc["@mozilla.org/xre/app-info;1"];
|
|
return (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT);
|
|
}
|
|
|
|
var handlers = [
|
|
(m, r) => {r.bodyOutputStream.write(responseContent, responseContent.length)},
|
|
(m, r) => {r.setStatusLine(m.httpVersion, 304, "Not Modified")},
|
|
(m, r) => {r.setStatusLine(m.httpVersion, 304, "Not Modified")},
|
|
(m, r) => {r.setStatusLine(m.httpVersion, 304, "Not Modified")},
|
|
(m, r) => {r.setStatusLine(m.httpVersion, 304, "Not Modified")},
|
|
(m, r) => {r.bodyOutputStream.write(responseContent2, responseContent2.length)},
|
|
(m, r) => {r.setStatusLine(m.httpVersion, 304, "Not Modified")},
|
|
];
|
|
|
|
function contentHandler(metadata, response)
|
|
{
|
|
response.setHeader("Content-Type", "text/plain");
|
|
response.setHeader("Cache-Control", "no-cache");
|
|
|
|
var handler = handlers.shift();
|
|
if (handler) {
|
|
handler(metadata, response);
|
|
return;
|
|
}
|
|
|
|
Assert.ok(false, "Should not reach here.");
|
|
}
|
|
|
|
function fetch(preferredDataType = null)
|
|
{
|
|
return new Promise(resolve => {
|
|
var chan = NetUtil.newChannel({uri: URL, loadUsingSystemPrincipal: true});
|
|
|
|
if (preferredDataType) {
|
|
var cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
|
|
cc.preferAlternativeDataType(altContentType);
|
|
}
|
|
|
|
chan.asyncOpen2(new ChannelListener((request,
|
|
buffer,
|
|
ctx,
|
|
isFromCache,
|
|
cacheEntryId) => {
|
|
resolve({request, buffer, isFromCache, cacheEntryId});
|
|
}, null));
|
|
});
|
|
}
|
|
|
|
function check(response, content, preferredDataType, isFromCache, cacheEntryIdChecker)
|
|
{
|
|
var cc = response.request.QueryInterface(Ci.nsICacheInfoChannel);
|
|
|
|
Assert.equal(response.buffer, content);
|
|
Assert.equal(cc.alternativeDataType, preferredDataType);
|
|
Assert.equal(response.isFromCache, isFromCache);
|
|
Assert.ok(!cacheEntryIdChecker || cacheEntryIdChecker(response.cacheEntryId));
|
|
|
|
return response;
|
|
}
|
|
|
|
function writeAltData(request)
|
|
{
|
|
var cc = request.QueryInterface(Ci.nsICacheInfoChannel);
|
|
var os = cc.openAlternativeOutputStream(altContentType);
|
|
os.write(altContent, altContent.length);
|
|
os.close();
|
|
gc(); // We need to do a GC pass to ensure the cache entry has been freed.
|
|
|
|
return new Promise(resolve => {
|
|
if (isParentProcess()) {
|
|
Services.cache2.QueryInterface(Ci.nsICacheTesting)
|
|
.flush(resolve);
|
|
} else {
|
|
do_send_remote_message('flush');
|
|
do_await_remote_message('flushed').then(resolve);
|
|
}
|
|
});
|
|
}
|
|
|
|
function run_test()
|
|
{
|
|
do_get_profile();
|
|
httpServer = new HttpServer();
|
|
httpServer.registerPathHandler("/content", contentHandler);
|
|
httpServer.start(-1);
|
|
do_test_pending();
|
|
|
|
var targetCacheEntryId = null;
|
|
|
|
return Promise.resolve()
|
|
// Setup testing environment: Placing alternative data into HTTP cache.
|
|
.then(_ => fetch(altContentType))
|
|
.then(r => check(r, responseContent, "", false,
|
|
cacheEntryId => cacheEntryId === undefined))
|
|
.then(r => writeAltData(r.request))
|
|
|
|
// Start testing.
|
|
.then(_ => fetch(altContentType))
|
|
.then(r => check(r, altContent, altContentType, true,
|
|
cacheEntryId => cacheEntryId !== undefined))
|
|
.then(r => targetCacheEntryId = r.cacheEntryId)
|
|
|
|
.then(_ => fetch())
|
|
.then(r => check(r, responseContent, "", true,
|
|
cacheEntryId => cacheEntryId === targetCacheEntryId))
|
|
|
|
.then(_ => fetch(altContentType))
|
|
.then(r => check(r, altContent, altContentType, true,
|
|
cacheEntryId => cacheEntryId === targetCacheEntryId))
|
|
|
|
.then(_ => fetch())
|
|
.then(r => check(r, responseContent, "", true,
|
|
cacheEntryId => cacheEntryId === targetCacheEntryId))
|
|
|
|
.then(_ => fetch()) // The response is changed here.
|
|
.then(r => check(r, responseContent2, "", false,
|
|
cacheEntryId => cacheEntryId === undefined))
|
|
|
|
.then(_ => fetch())
|
|
.then(r => check(r, responseContent2, "", true,
|
|
cacheEntryId => cacheEntryId !== undefined &&
|
|
cacheEntryId !== targetCacheEntryId))
|
|
|
|
// Tear down.
|
|
.catch(e => Assert.ok(false, "Unexpected exception: " + e))
|
|
.then(_ => Assert.equal(handlers.length, 0))
|
|
.then(_ => httpServer.stop(do_test_finished));
|
|
}
|