1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/**
* Check how nsICachingChannel.cacheOnlyMetadata works.
* - all channels involved in this test are set cacheOnlyMetadata = true
* - do a previously uncached request for a long living content
* - check we have downloaded the content from the server (channel provides it)
* - check the entry has metadata, but zero-length content
* - load the same URL again, now cached
* - check the channel is giving no content (no call to OnDataAvailable) but succeeds
* - repeat again, but for a different URL that is not cached (immediately expires)
* - only difference is that we get a newer version of the content from the server during the second request
*/
"use strict";
const { HttpServer } = ChromeUtils.importESModule(
"resource://testing-common/httpd.sys.mjs"
);
ChromeUtils.defineLazyGetter(this, "URL", function () {
return "http://localhost:" + httpServer.identity.primaryPort;
});
var httpServer = null;
function make_channel(url, callback, ctx) {
return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
}
const responseBody1 = "response body 1";
const responseBody2a = "response body 2a";
const responseBody2b = "response body 2b";
function contentHandler1(metadata, response) {
response.setHeader("Content-Type", "text/plain");
response.setHeader("Cache-control", "max-age=999999");
response.bodyOutputStream.write(responseBody1, responseBody1.length);
}
var content2passCount = 0;
function contentHandler2(metadata, response) {
response.setHeader("Content-Type", "text/plain");
response.setHeader("Cache-control", "no-cache");
switch (content2passCount++) {
case 0:
response.setHeader("ETag", "testetag");
response.bodyOutputStream.write(responseBody2a, responseBody2a.length);
break;
case 1:
Assert.ok(metadata.hasHeader("If-None-Match"));
Assert.equal(metadata.getHeader("If-None-Match"), "testetag");
response.bodyOutputStream.write(responseBody2b, responseBody2b.length);
break;
default:
throw new Error("Unexpected request in the test");
}
}
function run_test() {
httpServer = new HttpServer();
httpServer.registerPathHandler("/content1", contentHandler1);
httpServer.registerPathHandler("/content2", contentHandler2);
httpServer.start(-1);
run_test_content1a();
do_test_pending();
}
function run_test_content1a() {
var chan = make_channel(URL + "/content1");
let caching = chan.QueryInterface(Ci.nsICachingChannel);
caching.cacheOnlyMetadata = true;
chan.asyncOpen(new ChannelListener(contentListener1a, null));
}
function contentListener1a(request, buffer) {
Assert.equal(buffer, responseBody1);
asyncOpenCacheEntry(URL + "/content1", "disk", 0, null, cacheCheck1);
}
function cacheCheck1(status, entry) {
Assert.equal(status, 0);
Assert.equal(entry.dataSize, 0);
try {
Assert.notEqual(entry.getMetaDataElement("response-head"), null);
} catch (ex) {
do_throw("Missing response head");
}
var chan = make_channel(URL + "/content1");
let caching = chan.QueryInterface(Ci.nsICachingChannel);
caching.cacheOnlyMetadata = true;
chan.asyncOpen(new ChannelListener(contentListener1b, null, CL_IGNORE_CL));
}
function contentListener1b(request, buffer) {
request.QueryInterface(Ci.nsIHttpChannel);
Assert.equal(request.requestMethod, "GET");
Assert.equal(request.responseStatus, 200);
Assert.equal(request.getResponseHeader("Cache-control"), "max-age=999999");
Assert.equal(buffer, "");
run_test_content2a();
}
// Now same set of steps but this time for an immediately expiring content.
function run_test_content2a() {
var chan = make_channel(URL + "/content2");
let caching = chan.QueryInterface(Ci.nsICachingChannel);
caching.cacheOnlyMetadata = true;
chan.asyncOpen(new ChannelListener(contentListener2a, null));
}
function contentListener2a(request, buffer) {
Assert.equal(buffer, responseBody2a);
asyncOpenCacheEntry(URL + "/content2", "disk", 0, null, cacheCheck2);
}
function cacheCheck2(status, entry) {
Assert.equal(status, 0);
Assert.equal(entry.dataSize, 0);
try {
Assert.notEqual(entry.getMetaDataElement("response-head"), null);
Assert.ok(
entry.getMetaDataElement("response-head").match("etag: testetag")
);
} catch (ex) {
do_throw("Missing response head");
}
var chan = make_channel(URL + "/content2");
let caching = chan.QueryInterface(Ci.nsICachingChannel);
caching.cacheOnlyMetadata = true;
chan.asyncOpen(new ChannelListener(contentListener2b, null));
}
function contentListener2b(request, buffer) {
Assert.equal(buffer, responseBody2b);
httpServer.stop(do_test_finished);
}
|