Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_bug812167.js
blob1773a733c291946ca8c4c7799ee9e41255a7461c
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
7 /*
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;
20 });
22 var randomPath2 = "/redirect-expires-past/" + Math.random();
24 ChromeUtils.defineLazyGetter(this, "randomURI2", function () {
25 return "http://localhost:" + httpserver.identity.primaryPort + randomPath2;
26 });
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");
37 response.setHeader(
38 "Location",
39 "http://localhost:" + httpserver.identity.primaryPort + "/content",
40 false
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");
49 response.setHeader(
50 "Location",
51 "http://localhost:" + httpserver.identity.primaryPort + "/content",
52 false
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(
64 path,
65 request,
66 buffer,
67 expectedExpiration,
68 continuation
69 ) {
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).
74 asyncOpenCacheEntry(
75 path,
76 "disk",
77 Ci.nsICacheStorage.OPEN_READONLY,
78 null,
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);
87 chan.asyncOpen(
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);
94 } else {
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);
101 continuation();
102 }, null)
108 function run_test_no_store() {
109 var chan = make_channel(randomURI1);
110 chan.asyncOpen(
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);
114 }, null)
118 function run_test_expires_past() {
119 var chan = make_channel(randomURI2);
120 chan.asyncOpen(
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);
124 }, null)
128 function finish_test() {
129 httpserver.stop(do_test_finished);
132 function run_test() {
133 do_get_profile();
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);
141 run_test_no_store();
142 do_test_pending();