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
|
"use strict";
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
var httpServer = null;
var path = "/bug699001";
XPCOMUtils.defineLazyGetter(this, "URI", function () {
return "http://localhost:" + httpServer.identity.primaryPort + path;
});
function make_channel(url) {
return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
}
var fetched;
// The test loads a resource that expires in one year, has an etag and varies only by User-Agent
// First we load it, then check we load it only from the cache w/o even checking with the server
// Then we modify our User-Agent and try it again
// We have to get a new content (even though with the same etag) and again on next load only from
// cache w/o accessing the server
// Goal is to check we've updated User-Agent request header in cache after we've got 304 response
// from the server
var tests = [
{
prepare() {},
test(response) {
Assert.ok(fetched);
},
},
{
prepare() {},
test(response) {
Assert.ok(!fetched);
},
},
{
prepare() {
setUA("A different User Agent");
},
test(response) {
Assert.ok(fetched);
},
},
{
prepare() {},
test(response) {
Assert.ok(!fetched);
},
},
{
prepare() {
setUA("And another User Agent");
},
test(response) {
Assert.ok(fetched);
},
},
{
prepare() {},
test(response) {
Assert.ok(!fetched);
},
},
];
function handler(metadata, response) {
if (metadata.hasHeader("If-None-Match")) {
response.setStatusLine(metadata.httpVersion, 304, "Not modified");
} else {
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("Content-Type", "text/plain");
var body = "body";
response.bodyOutputStream.write(body, body.length);
}
fetched = true;
response.setHeader("Expires", getDateString(+1));
response.setHeader("Cache-Control", "private");
response.setHeader("Vary", "User-Agent");
response.setHeader("ETag", "1234");
}
function run_test() {
httpServer = new HttpServer();
httpServer.registerPathHandler(path, handler);
httpServer.start(-1);
do_test_pending();
nextTest();
}
function nextTest() {
fetched = false;
tests[0].prepare();
dump("Testing with User-Agent: " + getUA() + "\n");
var chan = make_channel(URI);
// Give the old channel a chance to close the cache entry first.
// XXX This is actually a race condition that might be considered a bug...
executeSoon(function () {
chan.asyncOpen(new ChannelListener(checkAndShiftTest, null));
});
}
function checkAndShiftTest(request, response) {
tests[0].test(response);
tests.shift();
if (!tests.length) {
httpServer.stop(tearDown);
return;
}
nextTest();
}
function tearDown() {
setUA("");
do_test_finished();
}
// Helpers
function getUA() {
var httphandler = Cc["@mozilla.org/network/protocol;1?name=http"].getService(
Ci.nsIHttpProtocolHandler
);
return httphandler.userAgent;
}
function setUA(value) {
Services.prefs.setCharPref("general.useragent.override", value);
}
function getDateString(yearDelta) {
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();
return (
days[d.getUTCDay()] +
", " +
d.getUTCDate() +
" " +
months[d.getUTCMonth()] +
" " +
(d.getUTCFullYear() + yearDelta) +
" " +
d.getUTCHours() +
":" +
d.getUTCMinutes() +
":" +
d.getUTCSeconds() +
" UTC"
);
}
|