Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_cache-entry-id.js
blobf0011de47fe665596bbf6ac5568a7d792ccd9462
1 /**
2 * Test for the "CacheEntryId" under several cases.
3 */
5 "use strict";
7 const { HttpServer } = ChromeUtils.importESModule(
8 "resource://testing-common/httpd.sys.mjs"
9 );
11 ChromeUtils.defineLazyGetter(this, "URL", function () {
12 return "http://localhost:" + httpServer.identity.primaryPort + "/content";
13 });
15 var httpServer = null;
17 const responseContent = "response body";
18 const responseContent2 = "response body 2";
19 const altContent = "!@#$%^&*()";
20 const altContentType = "text/binary";
22 function isParentProcess() {
23 let appInfo = Cc["@mozilla.org/xre/app-info;1"];
24 return (
25 !appInfo ||
26 Services.appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT
30 var handlers = [
31 (m, r) => {
32 r.bodyOutputStream.write(responseContent, responseContent.length);
34 (m, r) => {
35 r.setStatusLine(m.httpVersion, 304, "Not Modified");
37 (m, r) => {
38 r.setStatusLine(m.httpVersion, 304, "Not Modified");
40 (m, r) => {
41 r.setStatusLine(m.httpVersion, 304, "Not Modified");
43 (m, r) => {
44 r.setStatusLine(m.httpVersion, 304, "Not Modified");
46 (m, r) => {
47 r.bodyOutputStream.write(responseContent2, responseContent2.length);
49 (m, r) => {
50 r.setStatusLine(m.httpVersion, 304, "Not Modified");
54 function contentHandler(metadata, response) {
55 response.setHeader("Content-Type", "text/plain");
56 response.setHeader("Cache-Control", "no-cache");
58 var handler = handlers.shift();
59 if (handler) {
60 handler(metadata, response);
61 return;
64 Assert.ok(false, "Should not reach here.");
67 function fetch(preferredDataType = null) {
68 return new Promise(resolve => {
69 var chan = NetUtil.newChannel({ uri: URL, loadUsingSystemPrincipal: true });
71 if (preferredDataType) {
72 var cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
73 cc.preferAlternativeDataType(
74 altContentType,
75 "",
76 Ci.nsICacheInfoChannel.ASYNC
80 chan.asyncOpen(
81 new ChannelListener((request, buffer, ctx, isFromCache, cacheEntryId) => {
82 resolve({ request, buffer, isFromCache, cacheEntryId });
83 }, null)
85 });
88 function check(
89 response,
90 content,
91 preferredDataType,
92 isFromCache,
93 cacheEntryIdChecker
94 ) {
95 var cc = response.request.QueryInterface(Ci.nsICacheInfoChannel);
97 Assert.equal(response.buffer, content);
98 Assert.equal(cc.alternativeDataType, preferredDataType);
99 Assert.equal(response.isFromCache, isFromCache);
100 Assert.ok(!cacheEntryIdChecker || cacheEntryIdChecker(response.cacheEntryId));
102 return response;
105 function writeAltData(request) {
106 var cc = request.QueryInterface(Ci.nsICacheInfoChannel);
107 var os = cc.openAlternativeOutputStream(altContentType, altContent.length);
108 os.write(altContent, altContent.length);
109 os.close();
110 gc(); // We need to do a GC pass to ensure the cache entry has been freed.
112 return new Promise(resolve => {
113 if (isParentProcess()) {
114 Services.cache2.QueryInterface(Ci.nsICacheTesting).flush(resolve);
115 } else {
116 do_send_remote_message("flush");
117 do_await_remote_message("flushed").then(resolve);
122 function run_test() {
123 do_get_profile();
124 httpServer = new HttpServer();
125 httpServer.registerPathHandler("/content", contentHandler);
126 httpServer.start(-1);
127 do_test_pending();
129 var targetCacheEntryId = null;
131 return (
132 Promise.resolve()
133 // Setup testing environment: Placing alternative data into HTTP cache.
134 .then(_ => fetch(altContentType))
135 .then(r =>
136 check(
138 responseContent,
140 false,
141 cacheEntryId => cacheEntryId === undefined
144 .then(r => writeAltData(r.request))
146 // Start testing.
147 .then(_ => fetch(altContentType))
148 .then(r =>
149 check(
151 altContent,
152 altContentType,
153 true,
154 cacheEntryId => cacheEntryId !== undefined
157 .then(r => (targetCacheEntryId = r.cacheEntryId))
159 .then(_ => fetch())
160 .then(r =>
161 check(
163 responseContent,
165 true,
166 cacheEntryId => cacheEntryId === targetCacheEntryId
170 .then(_ => fetch(altContentType))
171 .then(r =>
172 check(
174 altContent,
175 altContentType,
176 true,
177 cacheEntryId => cacheEntryId === targetCacheEntryId
181 .then(_ => fetch())
182 .then(r =>
183 check(
185 responseContent,
187 true,
188 cacheEntryId => cacheEntryId === targetCacheEntryId
192 .then(_ => fetch()) // The response is changed here.
193 .then(r =>
194 check(
196 responseContent2,
198 false,
199 cacheEntryId => cacheEntryId === undefined
203 .then(_ => fetch())
204 .then(r =>
205 check(
207 responseContent2,
209 true,
210 cacheEntryId =>
211 cacheEntryId !== undefined && cacheEntryId !== targetCacheEntryId
215 // Tear down.
216 .catch(e => Assert.ok(false, "Unexpected exception: " + e))
217 .then(_ => Assert.equal(handlers.length, 0))
218 .then(_ => httpServer.stop(do_test_finished))