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
|
/* this test does the following:
Always requests the same resource, while for each request getting:
1. 200 + ETag: "one"
2. 401 followed by 200 + ETag: "two"
3. 401 followed by 304
4. 407 followed by 200 + ETag: "three"
5. 407 followed by 304
*/
Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/NetUtil.jsm");
var httpserv;
function addCreds(scheme, host)
{
var authMgr = Components.classes['@mozilla.org/network/http-auth-manager;1']
.getService(Ci.nsIHttpAuthManager);
authMgr.setAuthIdentity(scheme, host, httpserv.identity.primaryPort,
"basic", "secret", "/", "", "user", "pass");
}
function clearCreds()
{
var authMgr = Components.classes['@mozilla.org/network/http-auth-manager;1']
.getService(Ci.nsIHttpAuthManager);
authMgr.clearAll();
}
function makeChan() {
return NetUtil.newChannel({
uri: "http://localhost:" + httpserv.identity.primaryPort + "/",
loadUsingSystemPrincipal: true
}).QueryInterface(Ci.nsIHttpChannel);
}
// Array of handlers that are called one by one in response to expected requests
var handlers = [
// Test 1
function(metadata, response) {
do_check_eq(metadata.hasHeader("Authorization"), false);
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("ETag", '"one"', false);
response.setHeader("Cache-control", 'no-cache', false);
response.setHeader("Content-type", 'text/plain', false);
var body = "Response body 1";
response.bodyOutputStream.write(body, body.length);
},
// Test 2
function(metadata, response) {
do_check_eq(metadata.hasHeader("Authorization"), false);
do_check_eq(metadata.getHeader("If-None-Match"), '"one"');
response.setStatusLine(metadata.httpVersion, 401, "Authenticate");
response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
addCreds("http", "localhost");
},
function(metadata, response) {
do_check_eq(metadata.hasHeader("Authorization"), true);
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("ETag", '"two"', false);
response.setHeader("Cache-control", 'no-cache', false);
response.setHeader("Content-type", 'text/plain', false);
var body = "Response body 2";
response.bodyOutputStream.write(body, body.length);
clearCreds();
},
// Test 3
function(metadata, response) {
do_check_eq(metadata.hasHeader("Authorization"), false);
do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
response.setStatusLine(metadata.httpVersion, 401, "Authenticate");
response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
addCreds("http", "localhost");
},
function(metadata, response) {
do_check_eq(metadata.hasHeader("Authorization"), true);
do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
response.setStatusLine(metadata.httpVersion, 304, "OK");
response.setHeader("ETag", '"two"', false);
clearCreds();
},
// Test 4
function(metadata, response) {
do_check_eq(metadata.hasHeader("Authorization"), false);
do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
response.setStatusLine(metadata.httpVersion, 407, "Proxy Authenticate");
response.setHeader("Proxy-Authenticate", 'Basic realm="secret"', false);
addCreds("http", "localhost");
},
function(metadata, response) {
do_check_eq(metadata.hasHeader("Proxy-Authorization"), true);
do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("ETag", '"three"', false);
response.setHeader("Cache-control", 'no-cache', false);
response.setHeader("Content-type", 'text/plain', false);
var body = "Response body 3";
response.bodyOutputStream.write(body, body.length);
clearCreds();
},
// Test 5
function(metadata, response) {
do_check_eq(metadata.hasHeader("Proxy-Authorization"), false);
do_check_eq(metadata.getHeader("If-None-Match"), '"three"');
response.setStatusLine(metadata.httpVersion, 407, "Proxy Authenticate");
response.setHeader("Proxy-Authenticate", 'Basic realm="secret"', false);
addCreds("http", "localhost");
},
function(metadata, response) {
do_check_eq(metadata.hasHeader("Proxy-Authorization"), true);
do_check_eq(metadata.getHeader("If-None-Match"), '"three"');
response.setStatusLine(metadata.httpVersion, 304, "OK");
response.setHeader("ETag", '"three"', false);
response.setHeader("Cache-control", 'no-cache', false);
clearCreds();
}
];
function handler(metadata, response)
{
handlers.shift()(metadata, response);
}
// Array of tests to run, self-driven
function sync_and_run_next_test()
{
syncWithCacheIOThread(function() {
tests.shift()();
});
}
var tests = [
// Test 1: 200 (cacheable)
function() {
var ch = makeChan();
ch.asyncOpen2(new ChannelListener(function(req, body) {
do_check_eq(body, "Response body 1");
sync_and_run_next_test();
}, null, CL_NOT_FROM_CACHE));
},
// Test 2: 401 and 200 + new content
function() {
var ch = makeChan();
ch.asyncOpen2(new ChannelListener(function(req, body) {
do_check_eq(body, "Response body 2");
sync_and_run_next_test();
}, null, CL_NOT_FROM_CACHE));
},
// Test 3: 401 and 304
function() {
var ch = makeChan();
ch.asyncOpen2(new ChannelListener(function(req, body) {
do_check_eq(body, "Response body 2");
sync_and_run_next_test();
}, null, CL_FROM_CACHE));
},
// Test 4: 407 and 200 + new content
function() {
var ch = makeChan();
ch.asyncOpen2(new ChannelListener(function(req, body) {
do_check_eq(body, "Response body 3");
sync_and_run_next_test();
}, null, CL_NOT_FROM_CACHE));
},
// Test 5: 407 and 304
function() {
var ch = makeChan();
ch.asyncOpen2(new ChannelListener(function(req, body) {
do_check_eq(body, "Response body 3");
sync_and_run_next_test();
}, null, CL_FROM_CACHE));
},
// End of test run
function() {
httpserv.stop(do_test_finished);
}
];
function run_test()
{
do_get_profile();
httpserv = new HttpServer();
httpserv.registerPathHandler("/", handler);
httpserv.start(-1);
const prefs = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefBranch);
prefs.setCharPref("network.proxy.http", "localhost");
prefs.setIntPref("network.proxy.http_port", httpserv.identity.primaryPort);
prefs.setCharPref("network.proxy.no_proxies_on", "");
prefs.setIntPref("network.proxy.type", 1);
tests.shift()();
do_test_pending();
}
|