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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/**
* Test for overwriting the alternative data in a cache entry.
*
* - run_test loads a new channel
* - readServerContent checks the content, and saves alt-data
* - cacheFlushObserver creates a new channel with "text/binary" alt-data type
* - readAltContent checks that it gets back alt-data and creates a channel with the dummy/null alt-data type
* - readServerContent2 checks that it gets regular content, from the cache and tries to overwrite the alt-data with the same representation
* - cacheFlushObserver2 creates a new channel with "text/binary" alt-data type
* - readAltContent2 checks that it gets back alt-data, and tries to overwrite with a kind of alt-data
* - cacheFlushObserver3 creates a new channel with "text/binary2" alt-data type
* - readAltContent3 checks that it gets back the newly saved alt-data
*/
"use strict";
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
XPCOMUtils.defineLazyGetter(this, "URL", function() {
return "http://localhost:" + httpServer.identity.primaryPort + "/content";
});
let httpServer = null;
function make_and_open_channel(url, altContentType, callback) {
let chan = NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
if (altContentType) {
let cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
cc.preferAlternativeDataType(
altContentType,
"",
Ci.nsICacheInfoChannel.ASYNC
);
}
chan.asyncOpen(new ChannelListener(callback, null));
}
const responseContent = "response body";
const altContent = "!@#$%^&*()";
const altContentType = "text/binary";
const altContent2 = "abc";
const altContentType2 = "text/binary2";
let servedNotModified = false;
function contentHandler(metadata, response) {
response.setHeader("Content-Type", "text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("ETag", "test-etag1");
let etag = "";
try {
etag = metadata.getHeader("If-None-Match");
} catch (ex) {
etag = "";
}
if (etag == "test-etag1") {
response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
servedNotModified = true;
} else {
servedNotModified = false;
response.bodyOutputStream.write(responseContent, responseContent.length);
}
}
function run_test() {
do_get_profile();
httpServer = new HttpServer();
httpServer.registerPathHandler("/content", contentHandler);
httpServer.start(-1);
do_test_pending();
make_and_open_channel(URL, altContentType, readServerContent);
}
function readServerContent(request, buffer) {
let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
Assert.equal(buffer, responseContent);
Assert.equal(cc.alternativeDataType, "");
executeSoon(() => {
let os = cc.openAlternativeOutputStream(altContentType, altContent.length);
os.write(altContent, altContent.length);
os.close();
executeSoon(flushAndOpenAltChannel);
});
}
function flushAndOpenAltChannel() {
// We need to do a GC pass to ensure the cache entry has been freed.
Cu.forceShrinkingGC();
Services.cache2.QueryInterface(Ci.nsICacheTesting).flush(cacheFlushObserver);
}
// needs to be rooted
let cacheFlushObserver = {
observe() {
if (!cacheFlushObserver) {
info("ignoring cacheFlushObserver\n");
return;
}
cacheFlushObserver = null;
Cu.forceShrinkingGC();
make_and_open_channel(URL, altContentType, readAltContent);
},
};
function readAltContent(request, buffer, closure, fromCache) {
Cu.forceShrinkingGC();
let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
Assert.equal(fromCache || servedNotModified, true);
Assert.equal(cc.alternativeDataType, altContentType);
Assert.equal(buffer, altContent);
make_and_open_channel(URL, "dummy/null", readServerContent2);
}
function readServerContent2(request, buffer, closure, fromCache) {
Cu.forceShrinkingGC();
let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
Assert.equal(fromCache || servedNotModified, true);
Assert.equal(buffer, responseContent);
Assert.equal(cc.alternativeDataType, "");
executeSoon(() => {
let os = cc.openAlternativeOutputStream(altContentType, altContent.length);
os.write(altContent, altContent.length);
os.close();
executeSoon(flushAndOpenAltChannel2);
});
}
function flushAndOpenAltChannel2() {
// We need to do a GC pass to ensure the cache entry has been freed.
Cu.forceShrinkingGC();
Services.cache2.QueryInterface(Ci.nsICacheTesting).flush(cacheFlushObserver2);
}
// needs to be rooted
let cacheFlushObserver2 = {
observe() {
if (!cacheFlushObserver2) {
info("ignoring cacheFlushObserver2\n");
return;
}
cacheFlushObserver2 = null;
Cu.forceShrinkingGC();
make_and_open_channel(URL, altContentType, readAltContent2);
},
};
function readAltContent2(request, buffer, closure, fromCache) {
Cu.forceShrinkingGC();
let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
Assert.equal(servedNotModified || fromCache, true);
Assert.equal(cc.alternativeDataType, altContentType);
Assert.equal(buffer, altContent);
executeSoon(() => {
Cu.forceShrinkingGC();
info("writing other content\n");
let os = cc.openAlternativeOutputStream(
altContentType2,
altContent2.length
);
os.write(altContent2, altContent2.length);
os.close();
executeSoon(flushAndOpenAltChannel3);
});
}
function flushAndOpenAltChannel3() {
// We need to do a GC pass to ensure the cache entry has been freed.
Cu.forceShrinkingGC();
Services.cache2.QueryInterface(Ci.nsICacheTesting).flush(cacheFlushObserver3);
}
// needs to be rooted
let cacheFlushObserver3 = {
observe() {
if (!cacheFlushObserver3) {
info("ignoring cacheFlushObserver3\n");
return;
}
cacheFlushObserver3 = null;
Cu.forceShrinkingGC();
make_and_open_channel(URL, altContentType2, readAltContent3);
},
};
function readAltContent3(request, buffer, closure, fromCache) {
let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
Assert.equal(servedNotModified || fromCache, true);
Assert.equal(cc.alternativeDataType, altContentType2);
Assert.equal(buffer, altContent2);
httpServer.stop(do_test_finished);
}
|