gecko-dev/netwerk/test/unit/test_bug618835.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

123 строки
3.9 KiB
JavaScript
Исходник Обычный вид История

//
// If a response to a non-safe HTTP request-method contains the Location- or
// Content-Location header, we must make sure to invalidate any cached entry
// representing the URIs pointed to by either header. RFC 2616 section 13.10
//
// This test uses 3 URIs: "/post" is the target of a POST-request and always
// redirects (301) to "/redirect". The URIs "/redirect" and "/cl" both counts
// the number of loads from the server (handler). The response from "/post"
// always contains the headers "Location: /redirect" and "Content-Location:
// /cl", whose cached entries are to be invalidated. The tests verifies that
// "/redirect" and "/cl" are loaded from server the expected number of times.
//
"use strict";
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 21:18:31 +03:00
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
var httpserv;
function setupChannel(path) {
return NetUtil.newChannel({
uri: path,
loadUsingSystemPrincipal: true,
}).QueryInterface(Ci.nsIHttpChannel);
}
// Verify that Content-Location-URI has been loaded once, load post_target
function InitialListener() {}
InitialListener.prototype = {
onStartRequest(request) {},
onStopRequest(request, status) {
Assert.equal(1, numberOfCLHandlerCalls);
executeSoon(function() {
var channel = setupChannel(
"http://localhost:" + httpserv.identity.primaryPort + "/post"
);
channel.requestMethod = "POST";
channel.asyncOpen(new RedirectingListener());
});
},
};
// Verify that Location-URI has been loaded once, reload post_target
function RedirectingListener() {}
RedirectingListener.prototype = {
onStartRequest(request) {},
onStopRequest(request, status) {
Assert.equal(1, numberOfHandlerCalls);
executeSoon(function() {
var channel = setupChannel(
"http://localhost:" + httpserv.identity.primaryPort + "/post"
);
channel.requestMethod = "POST";
channel.asyncOpen(new VerifyingListener());
});
},
};
// Verify that Location-URI has been loaded twice (cached entry invalidated),
// reload Content-Location-URI
function VerifyingListener() {}
VerifyingListener.prototype = {
onStartRequest(request) {},
onStopRequest(request, status) {
Assert.equal(2, numberOfHandlerCalls);
var channel = setupChannel(
"http://localhost:" + httpserv.identity.primaryPort + "/cl"
);
channel.asyncOpen(new FinalListener());
},
};
// Verify that Location-URI has been loaded twice (cached entry invalidated),
// stop test
function FinalListener() {}
FinalListener.prototype = {
onStartRequest(request) {},
onStopRequest(request, status) {
Assert.equal(2, numberOfCLHandlerCalls);
httpserv.stop(do_test_finished);
},
};
function run_test() {
httpserv = new HttpServer();
httpserv.registerPathHandler("/cl", content_location);
httpserv.registerPathHandler("/post", post_target);
httpserv.registerPathHandler("/redirect", redirect_target);
httpserv.start(-1);
// Clear cache
evict_cache_entries();
// Load Content-Location URI into cache and start the chain of loads
var channel = setupChannel(
"http://localhost:" + httpserv.identity.primaryPort + "/cl"
);
channel.asyncOpen(new InitialListener());
do_test_pending();
}
var numberOfCLHandlerCalls = 0;
function content_location(metadata, response) {
numberOfCLHandlerCalls++;
response.setStatusLine(metadata.httpVersion, 200, "Ok");
response.setHeader("Cache-Control", "max-age=360000", false);
}
function post_target(metadata, response) {
response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
response.setHeader("Location", "/redirect", false);
response.setHeader("Content-Location", "/cl", false);
response.setHeader("Cache-Control", "max-age=360000", false);
}
var numberOfHandlerCalls = 0;
function redirect_target(metadata, response) {
numberOfHandlerCalls++;
response.setStatusLine(metadata.httpVersion, 200, "Ok");
response.setHeader("Cache-Control", "max-age=360000", false);
}