1 /* this test does the following:
2 Always requests the same resource, while for each request getting:
4 2. 401 followed by 200 + ETag: "two"
6 4. 407 followed by 200 + ETag: "three"
12 const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
16 function addCreds(scheme, host) {
17 var authMgr = Cc["@mozilla.org/network/http-auth-manager;1"].getService(
20 authMgr.setAuthIdentity(
23 httpserv.identity.primaryPort,
33 function clearCreds() {
34 var authMgr = Cc["@mozilla.org/network/http-auth-manager;1"].getService(
41 return NetUtil.newChannel({
42 uri: "http://localhost:" + httpserv.identity.primaryPort + "/",
43 loadUsingSystemPrincipal: true,
44 }).QueryInterface(Ci.nsIHttpChannel);
47 // Array of handlers that are called one by one in response to expected requests
51 function(metadata, response) {
52 Assert.equal(metadata.hasHeader("Authorization"), false);
53 response.setStatusLine(metadata.httpVersion, 200, "OK");
54 response.setHeader("ETag", '"one"', false);
55 response.setHeader("Cache-control", "no-cache", false);
56 response.setHeader("Content-type", "text/plain", false);
57 var body = "Response body 1";
58 response.bodyOutputStream.write(body, body.length);
62 function(metadata, response) {
63 Assert.equal(metadata.hasHeader("Authorization"), false);
64 Assert.equal(metadata.getHeader("If-None-Match"), '"one"');
65 response.setStatusLine(metadata.httpVersion, 401, "Authenticate");
66 response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
67 addCreds("http", "localhost");
69 function(metadata, response) {
70 Assert.equal(metadata.hasHeader("Authorization"), true);
71 response.setStatusLine(metadata.httpVersion, 200, "OK");
72 response.setHeader("ETag", '"two"', false);
73 response.setHeader("Cache-control", "no-cache", false);
74 response.setHeader("Content-type", "text/plain", false);
75 var body = "Response body 2";
76 response.bodyOutputStream.write(body, body.length);
81 function(metadata, response) {
82 Assert.equal(metadata.hasHeader("Authorization"), false);
83 Assert.equal(metadata.getHeader("If-None-Match"), '"two"');
84 response.setStatusLine(metadata.httpVersion, 401, "Authenticate");
85 response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
86 addCreds("http", "localhost");
88 function(metadata, response) {
89 Assert.equal(metadata.hasHeader("Authorization"), true);
90 Assert.equal(metadata.getHeader("If-None-Match"), '"two"');
91 response.setStatusLine(metadata.httpVersion, 304, "OK");
92 response.setHeader("ETag", '"two"', false);
97 function(metadata, response) {
98 Assert.equal(metadata.hasHeader("Authorization"), false);
99 Assert.equal(metadata.getHeader("If-None-Match"), '"two"');
100 response.setStatusLine(metadata.httpVersion, 407, "Proxy Authenticate");
101 response.setHeader("Proxy-Authenticate", 'Basic realm="secret"', false);
102 addCreds("http", "localhost");
104 function(metadata, response) {
105 Assert.equal(metadata.hasHeader("Proxy-Authorization"), true);
106 Assert.equal(metadata.getHeader("If-None-Match"), '"two"');
107 response.setStatusLine(metadata.httpVersion, 200, "OK");
108 response.setHeader("ETag", '"three"', false);
109 response.setHeader("Cache-control", "no-cache", false);
110 response.setHeader("Content-type", "text/plain", false);
111 var body = "Response body 3";
112 response.bodyOutputStream.write(body, body.length);
117 function(metadata, response) {
118 Assert.equal(metadata.hasHeader("Proxy-Authorization"), false);
119 Assert.equal(metadata.getHeader("If-None-Match"), '"three"');
120 response.setStatusLine(metadata.httpVersion, 407, "Proxy Authenticate");
121 response.setHeader("Proxy-Authenticate", 'Basic realm="secret"', false);
122 addCreds("http", "localhost");
124 function(metadata, response) {
125 Assert.equal(metadata.hasHeader("Proxy-Authorization"), true);
126 Assert.equal(metadata.getHeader("If-None-Match"), '"three"');
127 response.setStatusLine(metadata.httpVersion, 304, "OK");
128 response.setHeader("ETag", '"three"', false);
129 response.setHeader("Cache-control", "no-cache", false);
134 function handler(metadata, response) {
135 handlers.shift()(metadata, response);
138 // Array of tests to run, self-driven
140 function sync_and_run_next_test() {
141 syncWithCacheIOThread(function() {
147 // Test 1: 200 (cacheable)
152 function(req, body) {
153 Assert.equal(body, "Response body 1");
154 sync_and_run_next_test();
162 // Test 2: 401 and 200 + new content
167 function(req, body) {
168 Assert.equal(body, "Response body 2");
169 sync_and_run_next_test();
177 // Test 3: 401 and 304
182 function(req, body) {
183 Assert.equal(body, "Response body 2");
184 sync_and_run_next_test();
192 // Test 4: 407 and 200 + new content
197 function(req, body) {
198 Assert.equal(body, "Response body 3");
199 sync_and_run_next_test();
207 // Test 5: 407 and 304
212 function(req, body) {
213 Assert.equal(body, "Response body 3");
214 sync_and_run_next_test();
224 httpserv.stop(do_test_finished);
228 function run_test() {
231 httpserv = new HttpServer();
232 httpserv.registerPathHandler("/", handler);
235 const prefs = Services.prefs;
236 prefs.setCharPref("network.proxy.http", "localhost");
237 prefs.setIntPref("network.proxy.http_port", httpserv.identity.primaryPort);
238 prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
239 prefs.setIntPref("network.proxy.type", 1);
240 prefs.setBoolPref("network.http.rcwn.enabled", false);