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