3 const { HttpServer
} = ChromeUtils
.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
8 - get 302 with Cache-control: no-store
9 - check cache entry for the 302 response is cached only in memory device
10 - get 302 with Expires: -1
11 - check cache entry for the 302 response is not cached at all
14 var httpserver
= null;
15 // Need to randomize, because apparently no one clears our cache
16 var randomPath1
= "/redirect-no-store/" + Math
.random();
18 ChromeUtils
.defineLazyGetter(this, "randomURI1", function () {
19 return "http://localhost:" + httpserver
.identity
.primaryPort
+ randomPath1
;
22 var randomPath2
= "/redirect-expires-past/" + Math
.random();
24 ChromeUtils
.defineLazyGetter(this, "randomURI2", function () {
25 return "http://localhost:" + httpserver
.identity
.primaryPort
+ randomPath2
;
28 function make_channel(url
) {
29 return NetUtil
.newChannel({ uri
: url
, loadUsingSystemPrincipal
: true });
32 const responseBody
= "response body";
34 var redirectHandler_NoStore_calls
= 0;
35 function redirectHandler_NoStore(metadata
, response
) {
36 response
.setStatusLine(metadata
.httpVersion
, 302, "Found");
39 "http://localhost:" + httpserver
.identity
.primaryPort
+ "/content",
42 response
.setHeader("Cache-control", "no-store");
43 ++redirectHandler_NoStore_calls
;
46 var redirectHandler_ExpiresInPast_calls
= 0;
47 function redirectHandler_ExpiresInPast(metadata
, response
) {
48 response
.setStatusLine(metadata
.httpVersion
, 302, "Found");
51 "http://localhost:" + httpserver
.identity
.primaryPort
+ "/content",
54 response
.setHeader("Expires", "-1");
55 ++redirectHandler_ExpiresInPast_calls
;
58 function contentHandler(metadata
, response
) {
59 response
.setHeader("Content-Type", "text/plain");
60 response
.bodyOutputStream
.write(responseBody
, responseBody
.length
);
63 function check_response(
70 Assert
.equal(buffer
, responseBody
);
72 // Entry is always there, old cache wrapping code does session->SetDoomEntriesIfExpired(false),
73 // just check it's not persisted or is expired (dep on the test).
77 Ci
.nsICacheStorage
.OPEN_READONLY
,
79 function (status
, entry
) {
80 Assert
.equal(status
, 0);
82 // Expired entry is on disk, no-store entry is in memory
83 Assert
.equal(entry
.persistent
, expectedExpiration
);
85 // Do the request again and check the server handler is called appropriately
86 var chan
= make_channel(path
);
88 new ChannelListener(function (request1
, buffer1
) {
89 Assert
.equal(buffer1
, responseBody
);
91 if (expectedExpiration
) {
92 // Handler had to be called second time
93 Assert
.equal(redirectHandler_ExpiresInPast_calls
, 2);
95 // Handler had to be called second time (no-store forces validate),
96 // and we are just in memory
97 Assert
.equal(redirectHandler_NoStore_calls
, 2);
98 Assert
.ok(!entry
.persistent
);
108 function run_test_no_store() {
109 var chan
= make_channel(randomURI1
);
111 new ChannelListener(function (request
, buffer
) {
112 // Cache-control: no-store response should only be found in the memory cache.
113 check_response(randomURI1
, request
, buffer
, false, run_test_expires_past
);
118 function run_test_expires_past() {
119 var chan
= make_channel(randomURI2
);
121 new ChannelListener(function (request
, buffer
) {
122 // Expires: -1 response should not be found in any cache.
123 check_response(randomURI2
, request
, buffer
, true, finish_test
);
128 function finish_test() {
129 httpserver
.stop(do_test_finished
);
132 function run_test() {
135 httpserver
= new HttpServer();
136 httpserver
.registerPathHandler(randomPath1
, redirectHandler_NoStore
);
137 httpserver
.registerPathHandler(randomPath2
, redirectHandler_ExpiresInPast
);
138 httpserver
.registerPathHandler("/content", contentHandler
);
139 httpserver
.start(-1);