3 const { HttpServer
} = ChromeUtils
.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
7 var httpserver
= new HttpServer();
10 // Need to randomize, because apparently no one clears our cache
11 var suffix
= Math
.random();
12 var httpBase
= "http://localhost:" + httpserver
.identity
.primaryPort
;
13 var shortexpPath
= "/shortexp" + suffix
;
14 var longexpPath
= "/longexp/" + suffix
;
15 var longexp2Path
= "/longexp/2/" + suffix
;
16 var nocachePath
= "/nocache" + suffix
;
17 var nostorePath
= "/nostore" + suffix
;
18 var test410Path
= "/test410" + suffix
;
19 var test404Path
= "/test404" + suffix
;
21 var PrivateBrowsingLoadContext
= Cu
.createPrivateLoadContext();
23 function make_channel(url
, flags
, usePrivateBrowsing
) {
24 var securityFlags
= Ci
.nsILoadInfo
.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL
;
26 var uri
= Services
.io
.newURI(url
);
27 var principal
= Services
.scriptSecurityManager
.createContentPrincipal(uri
, {
28 privateBrowsingId
: usePrivateBrowsing
? 1 : 0,
31 var req
= NetUtil
.newChannel({
33 loadingPrincipal
: principal
,
35 contentPolicyType
: Ci
.nsIContentPolicy
.TYPE_OTHER
,
38 req
.loadFlags
= flags
;
39 if (usePrivateBrowsing
) {
40 req
.notificationCallbacks
= PrivateBrowsingLoadContext
;
51 usePrivateBrowsing
/* defaults to false */
55 this.expectSuccess
= expectSuccess
;
56 this.readFromCache
= readFromCache
;
57 this.hitServer
= hitServer
;
58 this.usePrivateBrowsing
= usePrivateBrowsing
;
66 usePrivateBrowsing
: false,
70 QueryInterface
: ChromeUtils
.generateQI([
75 onStartRequest(request
) {
76 var cachingChannel
= request
.QueryInterface(Ci
.nsICacheInfoChannel
);
77 this._isFromCache
= request
.isPending() && cachingChannel
.isFromCache();
80 onDataAvailable(request
, stream
, offset
, count
) {
81 this._buffer
= this._buffer
.concat(read_stream(stream
, count
));
84 onStopRequest(request
, status
) {
85 Assert
.equal(Components
.isSuccessCode(status
), this.expectSuccess
);
86 Assert
.equal(this._isFromCache
, this.readFromCache
);
87 Assert
.equal(gHitServer
, this.hitServer
);
89 do_timeout(0, run_next_test
);
108 var channel
= make_channel(this.path
, this.flags
, this.usePrivateBrowsing
);
109 channel
.asyncOpen(this);
113 var gHitServer
= false;
117 httpBase
+ shortexpPath
,
119 true, // expect success
120 false, // read from cache
123 ), // USE PRIVATE BROWSING, so not cached for later requests
125 httpBase
+ shortexpPath
,
127 true, // expect success
128 false, // read from cache
132 httpBase
+ shortexpPath
,
134 true, // expect success
135 true, // read from cache
139 httpBase
+ shortexpPath
,
140 Ci
.nsIRequest
.LOAD_BYPASS_CACHE
,
141 true, // expect success
142 false, // read from cache
146 httpBase
+ shortexpPath
,
147 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
,
148 false, // expect success
149 false, // read from cache
153 httpBase
+ shortexpPath
,
154 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
| Ci
.nsIRequest
.VALIDATE_NEVER
,
155 true, // expect success
156 true, // read from cache
160 httpBase
+ shortexpPath
,
161 Ci
.nsIRequest
.LOAD_FROM_CACHE
,
162 true, // expect success
163 true, // read from cache
168 httpBase
+ longexpPath
,
170 true, // expect success
171 false, // read from cache
175 httpBase
+ longexpPath
,
177 true, // expect success
178 true, // read from cache
182 httpBase
+ longexpPath
,
183 Ci
.nsIRequest
.LOAD_BYPASS_CACHE
,
184 true, // expect success
185 false, // read from cache
189 httpBase
+ longexpPath
,
190 Ci
.nsIRequest
.VALIDATE_ALWAYS
,
191 true, // expect success
192 true, // read from cache
196 httpBase
+ longexpPath
,
197 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
,
198 true, // expect success
199 true, // read from cache
203 httpBase
+ longexpPath
,
204 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
| Ci
.nsIRequest
.VALIDATE_NEVER
,
205 true, // expect success
206 true, // read from cache
210 httpBase
+ longexpPath
,
211 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
| Ci
.nsIRequest
.VALIDATE_ALWAYS
,
212 false, // expect success
213 false, // read from cache
217 httpBase
+ longexpPath
,
218 Ci
.nsIRequest
.LOAD_FROM_CACHE
,
219 true, // expect success
220 true, // read from cache
225 httpBase
+ longexp2Path
,
227 true, // expect success
228 false, // read from cache
232 httpBase
+ longexp2Path
,
234 true, // expect success
235 true, // read from cache
240 httpBase
+ nocachePath
,
242 true, // expect success
243 false, // read from cache
247 httpBase
+ nocachePath
,
249 true, // expect success
250 true, // read from cache
254 httpBase
+ nocachePath
,
255 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
,
256 false, // expect success
257 false, // read from cache
261 // CACHE2: mayhemer - entry is doomed... I think the logic is wrong, we should not doom them
262 // as they are not valid, but take them as they need to reval
264 new Test(httpBase + nocachePath, Ci.nsIRequest.LOAD_FROM_CACHE,
265 true, // expect success
266 true, // read from cache
267 false), // hit server
270 // LOAD_ONLY_FROM_CACHE would normally fail (because no-cache forces
271 // a validation), but VALIDATE_NEVER should override that.
273 httpBase
+ nocachePath
,
274 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
| Ci
.nsIRequest
.VALIDATE_NEVER
,
275 true, // expect success
276 true, // read from cache
280 // ... however, no-cache over ssl should act like no-store and force
281 // a validation (and therefore failure) even if VALIDATE_NEVER is
283 /* XXX bug 466524: We can't currently start an ssl server in xpcshell tests,
284 so this test is currently disabled.
285 new Test(httpsBase + nocachePath,
286 Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
287 Ci.nsIRequest.VALIDATE_NEVER,
288 false, // expect success
289 false, // read from cache
294 httpBase
+ nostorePath
,
296 true, // expect success
297 false, // read from cache
301 httpBase
+ nostorePath
,
303 true, // expect success
304 false, // read from cache
308 httpBase
+ nostorePath
,
309 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
,
310 false, // expect success
311 false, // read from cache
315 httpBase
+ nostorePath
,
316 Ci
.nsIRequest
.LOAD_FROM_CACHE
,
317 true, // expect success
318 true, // read from cache
321 // no-store should force the validation (and therefore failure, with
322 // LOAD_ONLY_FROM_CACHE) even if VALIDATE_NEVER is set.
324 httpBase
+ nostorePath
,
325 Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
| Ci
.nsIRequest
.VALIDATE_NEVER
,
326 false, // expect success
327 false, // read from cache
332 httpBase
+ test410Path
,
334 true, // expect success
335 false, // read from cache
339 httpBase
+ test410Path
,
341 true, // expect success
342 true, // read from cache
347 httpBase
+ test404Path
,
349 true, // expect success
350 false, // read from cache
354 httpBase
+ test404Path
,
356 true, // expect success
357 false, // read from cache
362 function run_next_test() {
363 if (!gTests
.length
) {
364 httpserver
.stop(do_test_finished
);
368 var test
= gTests
.shift();
372 function handler(httpStatus
, metadata
, response
) {
376 etag
= metadata
.getHeader("If-None-Match");
380 if (etag
== "testtag") {
381 // Allow using the cached data
382 response
.setStatusLine(metadata
.httpVersion
, 304, "Not Modified");
384 response
.setStatusLine(metadata
.httpVersion
, httpStatus
, "Useless Phrase");
385 response
.setHeader("Content-Type", "text/plain", false);
386 response
.setHeader("ETag", "testtag", false);
388 response
.bodyOutputStream
.write(body
, body
.length
);
392 function nocache_handler(metadata
, response
) {
393 response
.setHeader("Cache-Control", "no-cache", false);
394 handler(200, metadata
, response
);
397 function nostore_handler(metadata
, response
) {
398 response
.setHeader("Cache-Control", "no-store", false);
399 handler(200, metadata
, response
);
402 function test410_handler(metadata
, response
) {
403 handler(410, metadata
, response
);
406 function test404_handler(metadata
, response
) {
407 handler(404, metadata
, response
);
410 function shortexp_handler(metadata
, response
) {
411 response
.setHeader("Cache-Control", "max-age=0", false);
412 handler(200, metadata
, response
);
415 function longexp_handler(metadata
, response
) {
416 response
.setHeader("Cache-Control", "max-age=10000", false);
417 handler(200, metadata
, response
);
420 // test spaces around max-age value token
421 function longexp2_handler(metadata
, response
) {
422 response
.setHeader("Cache-Control", "max-age = 10000", false);
423 handler(200, metadata
, response
);
426 function run_test() {
427 httpserver
.registerPathHandler(shortexpPath
, shortexp_handler
);
428 httpserver
.registerPathHandler(longexpPath
, longexp_handler
);
429 httpserver
.registerPathHandler(longexp2Path
, longexp2_handler
);
430 httpserver
.registerPathHandler(nocachePath
, nocache_handler
);
431 httpserver
.registerPathHandler(nostorePath
, nostore_handler
);
432 httpserver
.registerPathHandler(test410Path
, test410_handler
);
433 httpserver
.registerPathHandler(test404Path
, test404_handler
);