2 // If a response to a non-safe HTTP request-method contains the Location- or
3 // Content-Location header, we must make sure to invalidate any cached entry
4 // representing the URIs pointed to by either header. RFC 2616 section 13.10
6 // This test uses 3 URIs: "/post" is the target of a POST-request and always
7 // redirects (301) to "/redirect". The URIs "/redirect" and "/cl" both counts
8 // the number of loads from the server (handler). The response from "/post"
9 // always contains the headers "Location: /redirect" and "Content-Location:
10 // /cl", whose cached entries are to be invalidated. The tests verifies that
11 // "/redirect" and "/cl" are loaded from server the expected number of times.
16 const { HttpServer } = ChromeUtils.importESModule(
17 "resource://testing-common/httpd.sys.mjs"
22 function setupChannel(path) {
23 return NetUtil.newChannel({
25 loadUsingSystemPrincipal: true,
26 }).QueryInterface(Ci.nsIHttpChannel);
29 // Verify that Content-Location-URI has been loaded once, load post_target
30 function InitialListener() {}
31 InitialListener.prototype = {
34 Assert.equal(1, numberOfCLHandlerCalls);
35 executeSoon(function () {
36 var channel = setupChannel(
37 "http://localhost:" + httpserv.identity.primaryPort + "/post"
39 channel.requestMethod = "POST";
40 channel.asyncOpen(new RedirectingListener());
45 // Verify that Location-URI has been loaded once, reload post_target
46 function RedirectingListener() {}
47 RedirectingListener.prototype = {
50 Assert.equal(1, numberOfHandlerCalls);
51 executeSoon(function () {
52 var channel = setupChannel(
53 "http://localhost:" + httpserv.identity.primaryPort + "/post"
55 channel.requestMethod = "POST";
56 channel.asyncOpen(new VerifyingListener());
61 // Verify that Location-URI has been loaded twice (cached entry invalidated),
62 // reload Content-Location-URI
63 function VerifyingListener() {}
64 VerifyingListener.prototype = {
67 Assert.equal(2, numberOfHandlerCalls);
68 var channel = setupChannel(
69 "http://localhost:" + httpserv.identity.primaryPort + "/cl"
71 channel.asyncOpen(new FinalListener());
75 // Verify that Location-URI has been loaded twice (cached entry invalidated),
77 function FinalListener() {}
78 FinalListener.prototype = {
81 Assert.equal(2, numberOfCLHandlerCalls);
82 httpserv.stop(do_test_finished);
87 httpserv = new HttpServer();
88 httpserv.registerPathHandler("/cl", content_location);
89 httpserv.registerPathHandler("/post", post_target);
90 httpserv.registerPathHandler("/redirect", redirect_target);
94 evict_cache_entries();
96 // Load Content-Location URI into cache and start the chain of loads
97 var channel = setupChannel(
98 "http://localhost:" + httpserv.identity.primaryPort + "/cl"
100 channel.asyncOpen(new InitialListener());
105 var numberOfCLHandlerCalls = 0;
106 function content_location(metadata, response) {
107 numberOfCLHandlerCalls++;
108 response.setStatusLine(metadata.httpVersion, 200, "Ok");
109 response.setHeader("Cache-Control", "max-age=360000", false);
112 function post_target(metadata, response) {
113 response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
114 response.setHeader("Location", "/redirect", false);
115 response.setHeader("Content-Location", "/cl", false);
116 response.setHeader("Cache-Control", "max-age=360000", false);
119 var numberOfHandlerCalls = 0;
120 function redirect_target(metadata, response) {
121 numberOfHandlerCalls++;
122 response.setStatusLine(metadata.httpVersion, 200, "Ok");
123 response.setHeader("Cache-Control", "max-age=360000", false);