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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
"use strict";
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
var httpserver = new HttpServer();
httpserver.start(-1);
var cache = null;
var base_url = "http://localhost:" + httpserver.identity.primaryPort;
var resource_age_100 = "/resource_age_100";
var resource_age_100_url = base_url + resource_age_100;
var resource_stale_100 = "/resource_stale_100";
var resource_stale_100_url = base_url + resource_stale_100;
var resource_fresh_100 = "/resource_fresh_100";
var resource_fresh_100_url = base_url + resource_fresh_100;
// Test flags
var hit_server = false;
function make_channel(url, cache_control) {
// Reset test global status
hit_server = false;
var req = NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
req.QueryInterface(Ci.nsIHttpChannel);
if (cache_control) {
req.setRequestHeader("Cache-control", cache_control, false);
}
return req;
}
function make_uri(url) {
return Services.io.newURI(url);
}
function resource_age_100_handler(metadata, response) {
hit_server = true;
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("Content-Type", "text/plain", false);
response.setHeader("Age", "100", false);
response.setHeader("Last-Modified", date_string_from_now(-100), false);
response.setHeader("Expires", date_string_from_now(+9999), false);
const body = "data1";
response.bodyOutputStream.write(body, body.length);
}
function resource_stale_100_handler(metadata, response) {
hit_server = true;
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("Content-Type", "text/plain", false);
response.setHeader("Date", date_string_from_now(-200), false);
response.setHeader("Last-Modified", date_string_from_now(-200), false);
response.setHeader("Cache-Control", "max-age=100", false);
response.setHeader("Expires", date_string_from_now(-100), false);
const body = "data2";
response.bodyOutputStream.write(body, body.length);
}
function resource_fresh_100_handler(metadata, response) {
hit_server = true;
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("Content-Type", "text/plain", false);
response.setHeader("Last-Modified", date_string_from_now(0), false);
response.setHeader("Cache-Control", "max-age=100", false);
response.setHeader("Expires", date_string_from_now(+100), false);
const body = "data3";
response.bodyOutputStream.write(body, body.length);
}
function run_test() {
do_get_profile();
do_test_pending();
Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
httpserver.registerPathHandler(resource_age_100, resource_age_100_handler);
httpserver.registerPathHandler(
resource_stale_100,
resource_stale_100_handler
);
httpserver.registerPathHandler(
resource_fresh_100,
resource_fresh_100_handler
);
cache = getCacheStorage("disk");
wait_for_cache_index(run_next_test);
}
// Here starts the list of tests
// ============================================================================
// Cache-Control: no-store
add_test(() => {
// Must not create a cache entry
var ch = make_channel(resource_age_100_url, "no-store");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(!cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// Prepare state only, cache the entry
var ch = make_channel(resource_age_100_url);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// Check the prepared cache entry is used when no special directives are added
var ch = make_channel(resource_age_100_url);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// Try again, while we already keep a cache entry,
// the channel must not use it, entry should stay in the cache
var ch = make_channel(resource_age_100_url, "no-store");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
// ============================================================================
// Cache-Control: no-cache
add_test(() => {
// Check the prepared cache entry is used when no special directives are added
var ch = make_channel(resource_age_100_url);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// The existing entry should be revalidated (we expect a server hit)
var ch = make_channel(resource_age_100_url, "no-cache");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
// ============================================================================
// Cache-Control: max-age
add_test(() => {
// Check the prepared cache entry is used when no special directives are added
var ch = make_channel(resource_age_100_url);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// The existing entry's age is greater than the maximum requested,
// should hit server
var ch = make_channel(resource_age_100_url, "max-age=10");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// The existing entry's age is greater than the maximum requested,
// but the max-stale directive says to use it when it's fresh enough
var ch = make_channel(resource_age_100_url, "max-age=10, max-stale=99999");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// The existing entry's age is lesser than the maximum requested,
// should go from cache
var ch = make_channel(resource_age_100_url, "max-age=1000");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_age_100_url), ""));
run_next_test();
}, null)
);
});
// ============================================================================
// Cache-Control: max-stale
add_test(() => {
// Preprate the entry first
var ch = make_channel(resource_stale_100_url);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_stale_100_url), ""));
// Must shift the expiration time set on the entry to |now| be in the past
do_timeout(1500, run_next_test);
}, null)
);
});
add_test(() => {
// Check it's not reused (as it's stale) when no special directives
// are provided
var ch = make_channel(resource_stale_100_url);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_stale_100_url), ""));
do_timeout(1500, run_next_test);
}, null)
);
});
add_test(() => {
// Accept cached responses of any stale time
var ch = make_channel(resource_stale_100_url, "max-stale");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_stale_100_url), ""));
do_timeout(1500, run_next_test);
}, null)
);
});
add_test(() => {
// The entry is stale only by 100 seconds, accept it
var ch = make_channel(resource_stale_100_url, "max-stale=1000");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_stale_100_url), ""));
do_timeout(1500, run_next_test);
}, null)
);
});
add_test(() => {
// The entry is stale by 100 seconds but we only accept a 10 seconds stale
// entry, go from server
var ch = make_channel(resource_stale_100_url, "max-stale=10");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_stale_100_url), ""));
run_next_test();
}, null)
);
});
// ============================================================================
// Cache-Control: min-fresh
add_test(() => {
// Preprate the entry first
var ch = make_channel(resource_fresh_100_url);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_fresh_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// Check it's reused when no special directives are provided
var ch = make_channel(resource_fresh_100_url);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_fresh_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// Entry fresh enough to be served from the cache
var ch = make_channel(resource_fresh_100_url, "min-fresh=10");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(!hit_server);
Assert.ok(cache.exists(make_uri(resource_fresh_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
// The entry is not fresh enough
var ch = make_channel(resource_fresh_100_url, "min-fresh=1000");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_fresh_100_url), ""));
run_next_test();
}, null)
);
});
// ============================================================================
// Parser test, if the Cache-Control header would not parse correctly, the entry
// doesn't load from the server.
add_test(() => {
var ch = make_channel(
resource_fresh_100_url,
'unknown1,unknown2 = "a,b", min-fresh = 1000 '
);
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_fresh_100_url), ""));
run_next_test();
}, null)
);
});
add_test(() => {
var ch = make_channel(resource_fresh_100_url, "no-cache = , min-fresh = 10");
ch.asyncOpen(
new ChannelListener(function (request, data) {
Assert.ok(hit_server);
Assert.ok(cache.exists(make_uri(resource_fresh_100_url), ""));
run_next_test();
}, null)
);
});
// ============================================================================
// Done
add_test(() => {
run_next_test();
httpserver.stop(do_test_finished);
});
// ============================================================================
// Helpers
function date_string_from_now(delta_secs) {
var months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var d = new Date();
d.setTime(d.getTime() + delta_secs * 1000);
return (
days[d.getUTCDay()] +
", " +
d.getUTCDate() +
" " +
months[d.getUTCMonth()] +
" " +
d.getUTCFullYear() +
" " +
d.getUTCHours() +
":" +
d.getUTCMinutes() +
":" +
d.getUTCSeconds() +
" UTC"
);
}
|