Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
1 /* this test does the following:
2 Always requests the same resource, while for each request getting:
3 1. 200 + ETag: "one"
4 2. 401 followed by 200 + ETag: "two"
5 3. 401 followed by 304
6 4. 407 followed by 200 + ETag: "three"
7 5. 407 followed by 304
8 */
10 Cu.import("resource://testing-common/httpd.js");
12 var httpserv;
14 function addCreds(scheme, host)
15 {
16 var authMgr = Components.classes['@mozilla.org/network/http-auth-manager;1']
17 .getService(Ci.nsIHttpAuthManager);
18 authMgr.setAuthIdentity(scheme, host, httpserv.identity.primaryPort,
19 "basic", "secret", "/", "", "user", "pass");
20 }
22 function clearCreds()
23 {
24 var authMgr = Components.classes['@mozilla.org/network/http-auth-manager;1']
25 .getService(Ci.nsIHttpAuthManager);
26 authMgr.clearAll();
27 }
29 function makeChan() {
30 var ios = Cc["@mozilla.org/network/io-service;1"]
31 .getService(Ci.nsIIOService);
32 var chan = ios.newChannel("http://localhost:" +
33 httpserv.identity.primaryPort + "/", null, null)
34 .QueryInterface(Ci.nsIHttpChannel);
35 return chan;
36 }
38 // Array of handlers that are called one by one in response to expected requests
40 var handlers = [
41 // Test 1
42 function(metadata, response) {
43 do_check_eq(metadata.hasHeader("Authorization"), false);
44 response.setStatusLine(metadata.httpVersion, 200, "OK");
45 response.setHeader("ETag", '"one"', false);
46 response.setHeader("Cache-control", 'no-cache', false);
47 response.setHeader("Content-type", 'text/plain', false);
48 var body = "Response body 1";
49 response.bodyOutputStream.write(body, body.length);
50 },
52 // Test 2
53 function(metadata, response) {
54 do_check_eq(metadata.hasHeader("Authorization"), false);
55 do_check_eq(metadata.getHeader("If-None-Match"), '"one"');
56 response.setStatusLine(metadata.httpVersion, 401, "Authenticate");
57 response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
58 addCreds("http", "localhost");
59 },
60 function(metadata, response) {
61 do_check_eq(metadata.hasHeader("Authorization"), true);
62 response.setStatusLine(metadata.httpVersion, 200, "OK");
63 response.setHeader("ETag", '"two"', false);
64 response.setHeader("Cache-control", 'no-cache', false);
65 response.setHeader("Content-type", 'text/plain', false);
66 var body = "Response body 2";
67 response.bodyOutputStream.write(body, body.length);
68 clearCreds();
69 },
71 // Test 3
72 function(metadata, response) {
73 do_check_eq(metadata.hasHeader("Authorization"), false);
74 do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
75 response.setStatusLine(metadata.httpVersion, 401, "Authenticate");
76 response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
77 addCreds("http", "localhost");
78 },
79 function(metadata, response) {
80 do_check_eq(metadata.hasHeader("Authorization"), true);
81 do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
82 response.setStatusLine(metadata.httpVersion, 304, "OK");
83 response.setHeader("ETag", '"two"', false);
84 clearCreds();
85 },
87 // Test 4
88 function(metadata, response) {
89 do_check_eq(metadata.hasHeader("Authorization"), false);
90 do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
91 response.setStatusLine(metadata.httpVersion, 407, "Proxy Authenticate");
92 response.setHeader("Proxy-Authenticate", 'Basic realm="secret"', false);
93 addCreds("http", "localhost");
94 },
95 function(metadata, response) {
96 do_check_eq(metadata.hasHeader("Proxy-Authorization"), true);
97 do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
98 response.setStatusLine(metadata.httpVersion, 200, "OK");
99 response.setHeader("ETag", '"three"', false);
100 response.setHeader("Cache-control", 'no-cache', false);
101 response.setHeader("Content-type", 'text/plain', false);
102 var body = "Response body 3";
103 response.bodyOutputStream.write(body, body.length);
104 clearCreds();
105 },
107 // Test 5
108 function(metadata, response) {
109 do_check_eq(metadata.hasHeader("Proxy-Authorization"), false);
110 do_check_eq(metadata.getHeader("If-None-Match"), '"three"');
111 response.setStatusLine(metadata.httpVersion, 407, "Proxy Authenticate");
112 response.setHeader("Proxy-Authenticate", 'Basic realm="secret"', false);
113 addCreds("http", "localhost");
114 },
115 function(metadata, response) {
116 do_check_eq(metadata.hasHeader("Proxy-Authorization"), true);
117 do_check_eq(metadata.getHeader("If-None-Match"), '"three"');
118 response.setStatusLine(metadata.httpVersion, 304, "OK");
119 response.setHeader("ETag", '"three"', false);
120 response.setHeader("Cache-control", 'no-cache', false);
121 clearCreds();
122 }
123 ];
125 function handler(metadata, response)
126 {
127 handlers.shift()(metadata, response);
128 }
130 // Array of tests to run, self-driven
132 function sync_and_run_next_test()
133 {
134 syncWithCacheIOThread(function() {
135 tests.shift()();
136 });
137 }
139 var tests = [
140 // Test 1: 200 (cacheable)
141 function() {
142 var ch = makeChan();
143 ch.asyncOpen(new ChannelListener(function(req, body) {
144 do_check_eq(body, "Response body 1");
145 sync_and_run_next_test();
146 }, null, CL_NOT_FROM_CACHE), null);
147 },
149 // Test 2: 401 and 200 + new content
150 function() {
151 var ch = makeChan();
152 ch.asyncOpen(new ChannelListener(function(req, body) {
153 do_check_eq(body, "Response body 2");
154 sync_and_run_next_test();
155 }, null, CL_NOT_FROM_CACHE), null);
156 },
158 // Test 3: 401 and 304
159 function() {
160 var ch = makeChan();
161 ch.asyncOpen(new ChannelListener(function(req, body) {
162 do_check_eq(body, "Response body 2");
163 sync_and_run_next_test();
164 }, null, CL_FROM_CACHE), null);
165 },
167 // Test 4: 407 and 200 + new content
168 function() {
169 var ch = makeChan();
170 ch.asyncOpen(new ChannelListener(function(req, body) {
171 do_check_eq(body, "Response body 3");
172 sync_and_run_next_test();
173 }, null, CL_NOT_FROM_CACHE), null);
174 },
176 // Test 5: 407 and 304
177 function() {
178 var ch = makeChan();
179 ch.asyncOpen(new ChannelListener(function(req, body) {
180 do_check_eq(body, "Response body 3");
181 sync_and_run_next_test();
182 }, null, CL_FROM_CACHE), null);
183 },
185 // End of test run
186 function() {
187 httpserv.stop(do_test_finished);
188 }
189 ];
191 function run_test()
192 {
193 do_get_profile();
195 httpserv = new HttpServer();
196 httpserv.registerPathHandler("/", handler);
197 httpserv.start(-1);
199 const prefs = Cc["@mozilla.org/preferences-service;1"]
200 .getService(Ci.nsIPrefBranch);
201 prefs.setCharPref("network.proxy.http", "localhost");
202 prefs.setIntPref("network.proxy.http_port", httpserv.identity.primaryPort);
203 prefs.setCharPref("network.proxy.no_proxies_on", "");
204 prefs.setIntPref("network.proxy.type", 1);
206 tests.shift()();
207 do_test_pending();
208 }