netwerk/test/unit/test_cacheflags.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 Cu.import("resource://testing-common/httpd.js");
michael@0 2
michael@0 3 var httpserver = new HttpServer();
michael@0 4 httpserver.start(-1);
michael@0 5
michael@0 6 // Need to randomize, because apparently no one clears our cache
michael@0 7 var suffix = Math.random();
michael@0 8 var httpBase = "http://localhost:" + httpserver.identity.primaryPort;
michael@0 9 var httpsBase = "http://localhost:4445";
michael@0 10 var shortexpPath = "/shortexp" + suffix;
michael@0 11 var longexpPath = "/longexp/" + suffix;
michael@0 12 var longexp2Path = "/longexp/2/" + suffix;
michael@0 13 var nocachePath = "/nocache" + suffix;
michael@0 14 var nostorePath = "/nostore" + suffix;
michael@0 15
michael@0 16 // We attach this to channel when we want to test Private Browsing mode
michael@0 17 function LoadContext(usePrivateBrowsing) {
michael@0 18 this.usePrivateBrowsing = usePrivateBrowsing;
michael@0 19 }
michael@0 20
michael@0 21 LoadContext.prototype = {
michael@0 22 usePrivateBrowsing: false,
michael@0 23 // don't bother defining rest of nsILoadContext fields: don't need 'em
michael@0 24
michael@0 25 QueryInterface: function(iid) {
michael@0 26 if (iid.equals(Ci.nsILoadContext))
michael@0 27 return this;
michael@0 28 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 29 },
michael@0 30
michael@0 31 getInterface: function(iid) {
michael@0 32 if (iid.equals(Ci.nsILoadContext))
michael@0 33 return this;
michael@0 34 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 35 }
michael@0 36 };
michael@0 37
michael@0 38 PrivateBrowsingLoadContext = new LoadContext(true);
michael@0 39
michael@0 40 function make_channel(url, flags, usePrivateBrowsing) {
michael@0 41 var ios = Cc["@mozilla.org/network/io-service;1"].
michael@0 42 getService(Ci.nsIIOService);
michael@0 43 var req = ios.newChannel(url, null, null);
michael@0 44 req.loadFlags = flags;
michael@0 45 if (usePrivateBrowsing) {
michael@0 46 req.notificationCallbacks = PrivateBrowsingLoadContext;
michael@0 47 }
michael@0 48 return req;
michael@0 49 }
michael@0 50
michael@0 51 function Test(path, flags, expectSuccess, readFromCache, hitServer,
michael@0 52 usePrivateBrowsing /* defaults to false */) {
michael@0 53 this.path = path;
michael@0 54 this.flags = flags;
michael@0 55 this.expectSuccess = expectSuccess;
michael@0 56 this.readFromCache = readFromCache;
michael@0 57 this.hitServer = hitServer;
michael@0 58 this.usePrivateBrowsing = usePrivateBrowsing;
michael@0 59 }
michael@0 60
michael@0 61 Test.prototype = {
michael@0 62 flags: 0,
michael@0 63 expectSuccess: true,
michael@0 64 readFromCache: false,
michael@0 65 hitServer: true,
michael@0 66 usePrivateBrowsing: false,
michael@0 67 _buffer: "",
michael@0 68 _isFromCache: false,
michael@0 69
michael@0 70 QueryInterface: function(iid) {
michael@0 71 if (iid.equals(Components.interfaces.nsIStreamListener) ||
michael@0 72 iid.equals(Components.interfaces.nsIRequestObserver) ||
michael@0 73 iid.equals(Components.interfaces.nsISupports))
michael@0 74 return this;
michael@0 75 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 76 },
michael@0 77
michael@0 78 onStartRequest: function(request, context) {
michael@0 79 var cachingChannel = request.QueryInterface(Ci.nsICacheInfoChannel);
michael@0 80 this._isFromCache = request.isPending() && cachingChannel.isFromCache();
michael@0 81 },
michael@0 82
michael@0 83 onDataAvailable: function(request, context, stream, offset, count) {
michael@0 84 this._buffer = this._buffer.concat(read_stream(stream, count));
michael@0 85 },
michael@0 86
michael@0 87 onStopRequest: function(request, context, status) {
michael@0 88 do_check_eq(Components.isSuccessCode(status), this.expectSuccess);
michael@0 89 do_check_eq(this._isFromCache, this.readFromCache);
michael@0 90 do_check_eq(gHitServer, this.hitServer);
michael@0 91
michael@0 92 do_timeout(0, run_next_test);
michael@0 93 },
michael@0 94
michael@0 95 run: function() {
michael@0 96 dump("Running:" +
michael@0 97 "\n " + this.path +
michael@0 98 "\n " + this.flags +
michael@0 99 "\n " + this.expectSuccess +
michael@0 100 "\n " + this.readFromCache +
michael@0 101 "\n " + this.hitServer + "\n");
michael@0 102 gHitServer = false;
michael@0 103 var channel = make_channel(this.path, this.flags, this.usePrivateBrowsing);
michael@0 104 channel.asyncOpen(this, null);
michael@0 105 }
michael@0 106 };
michael@0 107
michael@0 108 var gHitServer = false;
michael@0 109
michael@0 110 var gTests = [
michael@0 111
michael@0 112 new Test(httpBase + shortexpPath, 0,
michael@0 113 true, // expect success
michael@0 114 false, // read from cache
michael@0 115 true, // hit server
michael@0 116 true), // USE PRIVATE BROWSING, so not cached for later requests
michael@0 117 new Test(httpBase + shortexpPath, 0,
michael@0 118 true, // expect success
michael@0 119 false, // read from cache
michael@0 120 true), // hit server
michael@0 121 new Test(httpBase + shortexpPath, 0,
michael@0 122 true, // expect success
michael@0 123 true, // read from cache
michael@0 124 true), // hit server
michael@0 125 new Test(httpBase + shortexpPath, Ci.nsIRequest.LOAD_BYPASS_CACHE,
michael@0 126 true, // expect success
michael@0 127 false, // read from cache
michael@0 128 true), // hit server
michael@0 129 new Test(httpBase + shortexpPath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
michael@0 130 false, // expect success
michael@0 131 false, // read from cache
michael@0 132 false), // hit server
michael@0 133 new Test(httpBase + shortexpPath,
michael@0 134 Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
michael@0 135 Ci.nsIRequest.VALIDATE_NEVER,
michael@0 136 true, // expect success
michael@0 137 true, // read from cache
michael@0 138 false), // hit server
michael@0 139 new Test(httpBase + shortexpPath, Ci.nsIRequest.LOAD_FROM_CACHE,
michael@0 140 true, // expect success
michael@0 141 true, // read from cache
michael@0 142 false), // hit server
michael@0 143
michael@0 144 new Test(httpBase + longexpPath, 0,
michael@0 145 true, // expect success
michael@0 146 false, // read from cache
michael@0 147 true), // hit server
michael@0 148 new Test(httpBase + longexpPath, 0,
michael@0 149 true, // expect success
michael@0 150 true, // read from cache
michael@0 151 false), // hit server
michael@0 152 new Test(httpBase + longexpPath, Ci.nsIRequest.LOAD_BYPASS_CACHE,
michael@0 153 true, // expect success
michael@0 154 false, // read from cache
michael@0 155 true), // hit server
michael@0 156 new Test(httpBase + longexpPath,
michael@0 157 Ci.nsIRequest.VALIDATE_ALWAYS,
michael@0 158 true, // expect success
michael@0 159 true, // read from cache
michael@0 160 true), // hit server
michael@0 161 new Test(httpBase + longexpPath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
michael@0 162 true, // expect success
michael@0 163 true, // read from cache
michael@0 164 false), // hit server
michael@0 165 new Test(httpBase + longexpPath,
michael@0 166 Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
michael@0 167 Ci.nsIRequest.VALIDATE_NEVER,
michael@0 168 true, // expect success
michael@0 169 true, // read from cache
michael@0 170 false), // hit server
michael@0 171 new Test(httpBase + longexpPath,
michael@0 172 Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
michael@0 173 Ci.nsIRequest.VALIDATE_ALWAYS,
michael@0 174 false, // expect success
michael@0 175 false, // read from cache
michael@0 176 false), // hit server
michael@0 177 new Test(httpBase + longexpPath, Ci.nsIRequest.LOAD_FROM_CACHE,
michael@0 178 true, // expect success
michael@0 179 true, // read from cache
michael@0 180 false), // hit server
michael@0 181
michael@0 182 new Test(httpBase + longexp2Path, 0,
michael@0 183 true, // expect success
michael@0 184 false, // read from cache
michael@0 185 true), // hit server
michael@0 186 new Test(httpBase + longexp2Path, 0,
michael@0 187 true, // expect success
michael@0 188 true, // read from cache
michael@0 189 false), // hit server
michael@0 190
michael@0 191 new Test(httpBase + nocachePath, 0,
michael@0 192 true, // expect success
michael@0 193 false, // read from cache
michael@0 194 true), // hit server
michael@0 195 new Test(httpBase + nocachePath, 0,
michael@0 196 true, // expect success
michael@0 197 true, // read from cache
michael@0 198 true), // hit server
michael@0 199 new Test(httpBase + nocachePath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
michael@0 200 false, // expect success
michael@0 201 false, // read from cache
michael@0 202 false), // hit server
michael@0 203
michael@0 204 // CACHE2: mayhemer - entry is doomed... I think the logic is wrong, we should not doom them
michael@0 205 // as they are not valid, but take them as they need to reval
michael@0 206 /*
michael@0 207 new Test(httpBase + nocachePath, Ci.nsIRequest.LOAD_FROM_CACHE,
michael@0 208 true, // expect success
michael@0 209 true, // read from cache
michael@0 210 false), // hit server
michael@0 211 */
michael@0 212
michael@0 213 // LOAD_ONLY_FROM_CACHE would normally fail (because no-cache forces
michael@0 214 // a validation), but VALIDATE_NEVER should override that.
michael@0 215 new Test(httpBase + nocachePath,
michael@0 216 Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
michael@0 217 Ci.nsIRequest.VALIDATE_NEVER,
michael@0 218 true, // expect success
michael@0 219 true, // read from cache
michael@0 220 false), // hit server
michael@0 221
michael@0 222 // ... however, no-cache over ssl should act like no-store and force
michael@0 223 // a validation (and therefore failure) even if VALIDATE_NEVER is
michael@0 224 // set.
michael@0 225 /* XXX bug 466524: We can't currently start an ssl server in xpcshell tests,
michael@0 226 so this test is currently disabled.
michael@0 227 new Test(httpsBase + nocachePath,
michael@0 228 Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
michael@0 229 Ci.nsIRequest.VALIDATE_NEVER,
michael@0 230 false, // expect success
michael@0 231 false, // read from cache
michael@0 232 false) // hit server
michael@0 233 */
michael@0 234
michael@0 235 new Test(httpBase + nostorePath, 0,
michael@0 236 true, // expect success
michael@0 237 false, // read from cache
michael@0 238 true), // hit server
michael@0 239 new Test(httpBase + nostorePath, 0,
michael@0 240 true, // expect success
michael@0 241 false, // read from cache
michael@0 242 true), // hit server
michael@0 243 new Test(httpBase + nostorePath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
michael@0 244 false, // expect success
michael@0 245 false, // read from cache
michael@0 246 false), // hit server
michael@0 247 new Test(httpBase + nostorePath, Ci.nsIRequest.LOAD_FROM_CACHE,
michael@0 248 true, // expect success
michael@0 249 true, // read from cache
michael@0 250 false), // hit server
michael@0 251 // no-store should force the validation (and therefore failure, with
michael@0 252 // LOAD_ONLY_FROM_CACHE) even if VALIDATE_NEVER is set.
michael@0 253 new Test(httpBase + nostorePath,
michael@0 254 Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
michael@0 255 Ci.nsIRequest.VALIDATE_NEVER,
michael@0 256 false, // expect success
michael@0 257 false, // read from cache
michael@0 258 false) // hit server
michael@0 259 ];
michael@0 260
michael@0 261 function run_next_test()
michael@0 262 {
michael@0 263 if (gTests.length == 0) {
michael@0 264 httpserver.stop(do_test_finished);
michael@0 265 return;
michael@0 266 }
michael@0 267
michael@0 268 var test = gTests.shift();
michael@0 269 test.run();
michael@0 270 }
michael@0 271
michael@0 272 function handler(metadata, response) {
michael@0 273 gHitServer = true;
michael@0 274 try {
michael@0 275 var etag = metadata.getHeader("If-None-Match");
michael@0 276 } catch(ex) {
michael@0 277 var etag = "";
michael@0 278 }
michael@0 279 if (etag == "testtag") {
michael@0 280 // Allow using the cached data
michael@0 281 response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
michael@0 282 } else {
michael@0 283 response.setStatusLine(metadata.httpVersion, 200, "OK");
michael@0 284 response.setHeader("Content-Type", "text/plain", false);
michael@0 285 response.setHeader("ETag", "testtag", false);
michael@0 286 const body = "data";
michael@0 287 response.bodyOutputStream.write(body, body.length);
michael@0 288 }
michael@0 289 }
michael@0 290
michael@0 291 function nocache_handler(metadata, response) {
michael@0 292 response.setHeader("Cache-Control", "no-cache", false);
michael@0 293 handler(metadata, response);
michael@0 294 }
michael@0 295
michael@0 296 function nostore_handler(metadata, response) {
michael@0 297 response.setHeader("Cache-Control", "no-store", false);
michael@0 298 handler(metadata, response);
michael@0 299 }
michael@0 300
michael@0 301 function shortexp_handler(metadata, response) {
michael@0 302 response.setHeader("Cache-Control", "max-age=0", false);
michael@0 303 handler(metadata, response);
michael@0 304 }
michael@0 305
michael@0 306 function longexp_handler(metadata, response) {
michael@0 307 response.setHeader("Cache-Control", "max-age=10000", false);
michael@0 308 handler(metadata, response);
michael@0 309 }
michael@0 310
michael@0 311 // test spaces around max-age value token
michael@0 312 function longexp2_handler(metadata, response) {
michael@0 313 response.setHeader("Cache-Control", "max-age = 10000", false);
michael@0 314 handler(metadata, response);
michael@0 315 }
michael@0 316
michael@0 317 function run_test() {
michael@0 318 httpserver.registerPathHandler(shortexpPath, shortexp_handler);
michael@0 319 httpserver.registerPathHandler(longexpPath, longexp_handler);
michael@0 320 httpserver.registerPathHandler(longexp2Path, longexp2_handler);
michael@0 321 httpserver.registerPathHandler(nocachePath, nocache_handler);
michael@0 322 httpserver.registerPathHandler(nostorePath, nostore_handler);
michael@0 323
michael@0 324 run_next_test();
michael@0 325 do_test_pending();
michael@0 326 }

mercurial