2 * Check how nsICachingChannel.cacheOnlyMetadata works.
3 * - all channels involved in this test are set cacheOnlyMetadata = true
4 * - do a previously uncached request for a long living content
5 * - check we have downloaded the content from the server (channel provides it)
6 * - check the entry has metadata, but zero-length content
7 * - load the same URL again, now cached
8 * - check the channel is giving no content (no call to OnDataAvailable) but succeeds
9 * - repeat again, but for a different URL that is not cached (immediately expires)
10 * - only difference is that we get a newer version of the content from the server during the second request
15 const { HttpServer
} = ChromeUtils
.importESModule(
16 "resource://testing-common/httpd.sys.mjs"
19 ChromeUtils
.defineLazyGetter(this, "URL", function () {
20 return "http://localhost:" + httpServer
.identity
.primaryPort
;
23 var httpServer
= null;
25 function make_channel(url
) {
26 return NetUtil
.newChannel({ uri
: url
, loadUsingSystemPrincipal
: true });
29 const responseBody1
= "response body 1";
30 const responseBody2a
= "response body 2a";
31 const responseBody2b
= "response body 2b";
33 function contentHandler1(metadata
, response
) {
34 response
.setHeader("Content-Type", "text/plain");
35 response
.setHeader("Cache-control", "max-age=999999");
36 response
.bodyOutputStream
.write(responseBody1
, responseBody1
.length
);
39 var content2passCount
= 0;
41 function contentHandler2(metadata
, response
) {
42 response
.setHeader("Content-Type", "text/plain");
43 response
.setHeader("Cache-control", "no-cache");
44 switch (content2passCount
++) {
46 response
.setHeader("ETag", "testetag");
47 response
.bodyOutputStream
.write(responseBody2a
, responseBody2a
.length
);
50 Assert
.ok(metadata
.hasHeader("If-None-Match"));
51 Assert
.equal(metadata
.getHeader("If-None-Match"), "testetag");
52 response
.bodyOutputStream
.write(responseBody2b
, responseBody2b
.length
);
55 throw new Error("Unexpected request in the test");
60 httpServer
= new HttpServer();
61 httpServer
.registerPathHandler("/content1", contentHandler1
);
62 httpServer
.registerPathHandler("/content2", contentHandler2
);
69 function run_test_content1a() {
70 var chan
= make_channel(URL
+ "/content1");
71 let caching
= chan
.QueryInterface(Ci
.nsICachingChannel
);
72 caching
.cacheOnlyMetadata
= true;
73 chan
.asyncOpen(new ChannelListener(contentListener1a
, null));
76 function contentListener1a(request
, buffer
) {
77 Assert
.equal(buffer
, responseBody1
);
79 asyncOpenCacheEntry(URL
+ "/content1", "disk", 0, null, cacheCheck1
);
82 function cacheCheck1(status
, entry
) {
83 Assert
.equal(status
, 0);
84 Assert
.equal(entry
.dataSize
, 0);
86 Assert
.notEqual(entry
.getMetaDataElement("response-head"), null);
88 do_throw("Missing response head");
91 var chan
= make_channel(URL
+ "/content1");
92 let caching
= chan
.QueryInterface(Ci
.nsICachingChannel
);
93 caching
.cacheOnlyMetadata
= true;
94 chan
.asyncOpen(new ChannelListener(contentListener1b
, null, CL_IGNORE_CL
));
97 function contentListener1b(request
, buffer
) {
98 request
.QueryInterface(Ci
.nsIHttpChannel
);
99 Assert
.equal(request
.requestMethod
, "GET");
100 Assert
.equal(request
.responseStatus
, 200);
101 Assert
.equal(request
.getResponseHeader("Cache-control"), "max-age=999999");
103 Assert
.equal(buffer
, "");
104 run_test_content2a();
107 // Now same set of steps but this time for an immediately expiring content.
109 function run_test_content2a() {
110 var chan
= make_channel(URL
+ "/content2");
111 let caching
= chan
.QueryInterface(Ci
.nsICachingChannel
);
112 caching
.cacheOnlyMetadata
= true;
113 chan
.asyncOpen(new ChannelListener(contentListener2a
, null));
116 function contentListener2a(request
, buffer
) {
117 Assert
.equal(buffer
, responseBody2a
);
119 asyncOpenCacheEntry(URL
+ "/content2", "disk", 0, null, cacheCheck2
);
122 function cacheCheck2(status
, entry
) {
123 Assert
.equal(status
, 0);
124 Assert
.equal(entry
.dataSize
, 0);
126 Assert
.notEqual(entry
.getMetaDataElement("response-head"), null);
128 entry
.getMetaDataElement("response-head").match("etag: testetag")
131 do_throw("Missing response head");
134 var chan
= make_channel(URL
+ "/content2");
135 let caching
= chan
.QueryInterface(Ci
.nsICachingChannel
);
136 caching
.cacheOnlyMetadata
= true;
137 chan
.asyncOpen(new ChannelListener(contentListener2b
, null));
140 function contentListener2b(request
, buffer
) {
141 Assert
.equal(buffer
, responseBody2b
);
143 httpServer
.stop(do_test_finished
);