netwerk/test/unit/test_cacheflags.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_cacheflags.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,326 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +var httpserver = new HttpServer();
     1.7 +httpserver.start(-1);
     1.8 +
     1.9 +// Need to randomize, because apparently no one clears our cache
    1.10 +var suffix = Math.random();
    1.11 +var httpBase = "http://localhost:" + httpserver.identity.primaryPort;
    1.12 +var httpsBase = "http://localhost:4445";
    1.13 +var shortexpPath = "/shortexp" + suffix;
    1.14 +var longexpPath = "/longexp/" + suffix;
    1.15 +var longexp2Path = "/longexp/2/" + suffix;
    1.16 +var nocachePath = "/nocache" + suffix;
    1.17 +var nostorePath = "/nostore" + suffix;
    1.18 +
    1.19 +// We attach this to channel when we want to test Private Browsing mode
    1.20 +function LoadContext(usePrivateBrowsing) {
    1.21 +  this.usePrivateBrowsing = usePrivateBrowsing;
    1.22 +}
    1.23 +
    1.24 +LoadContext.prototype = {
    1.25 +  usePrivateBrowsing: false,
    1.26 +  // don't bother defining rest of nsILoadContext fields: don't need 'em
    1.27 +
    1.28 +  QueryInterface: function(iid) {
    1.29 +    if (iid.equals(Ci.nsILoadContext))
    1.30 +      return this;
    1.31 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.32 +  },
    1.33 +
    1.34 +  getInterface: function(iid) {
    1.35 +    if (iid.equals(Ci.nsILoadContext))
    1.36 +      return this;
    1.37 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.38 +  }
    1.39 +};
    1.40 +
    1.41 +PrivateBrowsingLoadContext = new LoadContext(true);
    1.42 +
    1.43 +function make_channel(url, flags, usePrivateBrowsing) {
    1.44 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.45 +    getService(Ci.nsIIOService);
    1.46 +  var req = ios.newChannel(url, null, null);
    1.47 +  req.loadFlags = flags;
    1.48 +  if (usePrivateBrowsing) {
    1.49 +    req.notificationCallbacks = PrivateBrowsingLoadContext;    
    1.50 +  }
    1.51 +  return req;
    1.52 +}
    1.53 +
    1.54 +function Test(path, flags, expectSuccess, readFromCache, hitServer, 
    1.55 +              usePrivateBrowsing /* defaults to false */) {
    1.56 +  this.path = path;
    1.57 +  this.flags = flags;
    1.58 +  this.expectSuccess = expectSuccess;
    1.59 +  this.readFromCache = readFromCache;
    1.60 +  this.hitServer = hitServer;
    1.61 +  this.usePrivateBrowsing = usePrivateBrowsing;
    1.62 +}
    1.63 +
    1.64 +Test.prototype = {
    1.65 +  flags: 0,
    1.66 +  expectSuccess: true,
    1.67 +  readFromCache: false,
    1.68 +  hitServer: true,
    1.69 +  usePrivateBrowsing: false,
    1.70 +  _buffer: "",
    1.71 +  _isFromCache: false,
    1.72 +
    1.73 +  QueryInterface: function(iid) {
    1.74 +    if (iid.equals(Components.interfaces.nsIStreamListener) ||
    1.75 +        iid.equals(Components.interfaces.nsIRequestObserver) ||
    1.76 +        iid.equals(Components.interfaces.nsISupports))
    1.77 +      return this;
    1.78 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.79 +  },
    1.80 +
    1.81 +  onStartRequest: function(request, context) {
    1.82 +    var cachingChannel = request.QueryInterface(Ci.nsICacheInfoChannel);
    1.83 +    this._isFromCache = request.isPending() && cachingChannel.isFromCache();
    1.84 +  },
    1.85 +
    1.86 +  onDataAvailable: function(request, context, stream, offset, count) {
    1.87 +    this._buffer = this._buffer.concat(read_stream(stream, count));
    1.88 +  },
    1.89 +
    1.90 +  onStopRequest: function(request, context, status) {
    1.91 +    do_check_eq(Components.isSuccessCode(status), this.expectSuccess);
    1.92 +    do_check_eq(this._isFromCache, this.readFromCache);
    1.93 +    do_check_eq(gHitServer, this.hitServer);
    1.94 +
    1.95 +    do_timeout(0, run_next_test);
    1.96 +  },
    1.97 +
    1.98 +  run: function() {
    1.99 +    dump("Running:" +
   1.100 +         "\n  " + this.path +
   1.101 +         "\n  " + this.flags +
   1.102 +         "\n  " + this.expectSuccess +
   1.103 +         "\n  " + this.readFromCache +
   1.104 +         "\n  " + this.hitServer + "\n");
   1.105 +    gHitServer = false;
   1.106 +    var channel = make_channel(this.path, this.flags, this.usePrivateBrowsing);
   1.107 +    channel.asyncOpen(this, null);
   1.108 +  }
   1.109 +};
   1.110 +
   1.111 +var gHitServer = false;
   1.112 +
   1.113 +var gTests = [
   1.114 +
   1.115 +  new Test(httpBase + shortexpPath, 0,
   1.116 +           true,   // expect success
   1.117 +           false,  // read from cache
   1.118 +           true,   // hit server
   1.119 +           true),  // USE PRIVATE BROWSING, so not cached for later requests
   1.120 +  new Test(httpBase + shortexpPath, 0,
   1.121 +           true,   // expect success
   1.122 +           false,  // read from cache
   1.123 +           true),  // hit server
   1.124 +  new Test(httpBase + shortexpPath, 0,
   1.125 +           true,   // expect success
   1.126 +           true,   // read from cache
   1.127 +           true),  // hit server
   1.128 +  new Test(httpBase + shortexpPath, Ci.nsIRequest.LOAD_BYPASS_CACHE,
   1.129 +           true,   // expect success
   1.130 +           false,  // read from cache
   1.131 +           true),  // hit server
   1.132 +  new Test(httpBase + shortexpPath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
   1.133 +           false,  // expect success
   1.134 +           false,  // read from cache
   1.135 +           false), // hit server
   1.136 +  new Test(httpBase + shortexpPath,
   1.137 +           Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
   1.138 +           Ci.nsIRequest.VALIDATE_NEVER,
   1.139 +           true,   // expect success
   1.140 +           true,   // read from cache
   1.141 +           false), // hit server
   1.142 +  new Test(httpBase + shortexpPath, Ci.nsIRequest.LOAD_FROM_CACHE,
   1.143 +           true,   // expect success
   1.144 +           true,   // read from cache
   1.145 +           false), // hit server
   1.146 +
   1.147 +  new Test(httpBase + longexpPath, 0,
   1.148 +           true,   // expect success
   1.149 +           false,  // read from cache
   1.150 +           true),  // hit server
   1.151 +  new Test(httpBase + longexpPath, 0,
   1.152 +           true,   // expect success
   1.153 +           true,   // read from cache
   1.154 +           false), // hit server
   1.155 +  new Test(httpBase + longexpPath, Ci.nsIRequest.LOAD_BYPASS_CACHE,
   1.156 +           true,   // expect success
   1.157 +           false,  // read from cache
   1.158 +           true),  // hit server
   1.159 +  new Test(httpBase + longexpPath,
   1.160 +           Ci.nsIRequest.VALIDATE_ALWAYS,
   1.161 +           true,   // expect success
   1.162 +           true,   // read from cache
   1.163 +           true),  // hit server
   1.164 +  new Test(httpBase + longexpPath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
   1.165 +           true,   // expect success
   1.166 +           true,   // read from cache
   1.167 +           false), // hit server
   1.168 +  new Test(httpBase + longexpPath,
   1.169 +           Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
   1.170 +           Ci.nsIRequest.VALIDATE_NEVER,
   1.171 +           true,   // expect success
   1.172 +           true,   // read from cache
   1.173 +           false), // hit server
   1.174 +  new Test(httpBase + longexpPath,
   1.175 +           Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
   1.176 +           Ci.nsIRequest.VALIDATE_ALWAYS,
   1.177 +           false,  // expect success
   1.178 +           false,  // read from cache
   1.179 +           false), // hit server
   1.180 +  new Test(httpBase + longexpPath, Ci.nsIRequest.LOAD_FROM_CACHE,
   1.181 +           true,   // expect success
   1.182 +           true,   // read from cache
   1.183 +           false), // hit server
   1.184 +
   1.185 +  new Test(httpBase + longexp2Path, 0,
   1.186 +           true,   // expect success
   1.187 +           false,  // read from cache
   1.188 +           true),  // hit server
   1.189 +  new Test(httpBase + longexp2Path, 0,
   1.190 +           true,   // expect success
   1.191 +           true,   // read from cache
   1.192 +           false), // hit server
   1.193 +
   1.194 +  new Test(httpBase + nocachePath, 0,
   1.195 +           true,   // expect success
   1.196 +           false,  // read from cache
   1.197 +           true),  // hit server
   1.198 +  new Test(httpBase + nocachePath, 0,
   1.199 +           true,   // expect success
   1.200 +           true,   // read from cache
   1.201 +           true),  // hit server
   1.202 +  new Test(httpBase + nocachePath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
   1.203 +           false,  // expect success
   1.204 +           false,  // read from cache
   1.205 +           false), // hit server
   1.206 +
   1.207 +  // CACHE2: mayhemer - entry is doomed... I think the logic is wrong, we should not doom them
   1.208 +  // as they are not valid, but take them as they need to reval
   1.209 +  /*
   1.210 +  new Test(httpBase + nocachePath, Ci.nsIRequest.LOAD_FROM_CACHE,
   1.211 +           true,   // expect success
   1.212 +           true,   // read from cache
   1.213 +           false), // hit server
   1.214 +  */
   1.215 +
   1.216 +  // LOAD_ONLY_FROM_CACHE would normally fail (because no-cache forces
   1.217 +  // a validation), but VALIDATE_NEVER should override that.
   1.218 +  new Test(httpBase + nocachePath,
   1.219 +           Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
   1.220 +           Ci.nsIRequest.VALIDATE_NEVER,
   1.221 +           true,   // expect success
   1.222 +           true,   // read from cache
   1.223 +           false), // hit server
   1.224 +
   1.225 +  // ... however, no-cache over ssl should act like no-store and force
   1.226 +  // a validation (and therefore failure) even if VALIDATE_NEVER is
   1.227 +  // set.
   1.228 +  /* XXX bug 466524: We can't currently start an ssl server in xpcshell tests,
   1.229 +                     so this test is currently disabled.
   1.230 +  new Test(httpsBase + nocachePath,
   1.231 +           Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
   1.232 +           Ci.nsIRequest.VALIDATE_NEVER,
   1.233 +           false,  // expect success
   1.234 +           false,  // read from cache
   1.235 +           false)  // hit server
   1.236 +  */
   1.237 +
   1.238 +  new Test(httpBase + nostorePath, 0,
   1.239 +           true,   // expect success
   1.240 +           false,  // read from cache
   1.241 +           true),  // hit server
   1.242 +  new Test(httpBase + nostorePath, 0,
   1.243 +           true,   // expect success
   1.244 +           false,  // read from cache
   1.245 +           true),  // hit server
   1.246 +  new Test(httpBase + nostorePath, Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE,
   1.247 +           false,  // expect success
   1.248 +           false,  // read from cache
   1.249 +           false), // hit server
   1.250 +  new Test(httpBase + nostorePath, Ci.nsIRequest.LOAD_FROM_CACHE,
   1.251 +           true,   // expect success
   1.252 +           true,   // read from cache
   1.253 +           false), // hit server
   1.254 +  // no-store should force the validation (and therefore failure, with
   1.255 +  // LOAD_ONLY_FROM_CACHE) even if VALIDATE_NEVER is set.
   1.256 +  new Test(httpBase + nostorePath,
   1.257 +           Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
   1.258 +           Ci.nsIRequest.VALIDATE_NEVER,
   1.259 +           false,  // expect success
   1.260 +           false,  // read from cache
   1.261 +           false)  // hit server
   1.262 +  ];
   1.263 +
   1.264 +function run_next_test()
   1.265 +{
   1.266 +  if (gTests.length == 0) {
   1.267 +    httpserver.stop(do_test_finished);
   1.268 +    return;
   1.269 +  }
   1.270 +
   1.271 +  var test = gTests.shift();
   1.272 +  test.run();
   1.273 +}
   1.274 +
   1.275 +function handler(metadata, response) {
   1.276 +  gHitServer = true;
   1.277 +  try {
   1.278 +    var etag = metadata.getHeader("If-None-Match");
   1.279 +  } catch(ex) {
   1.280 +    var etag = "";
   1.281 +  }
   1.282 +  if (etag == "testtag") {
   1.283 +    // Allow using the cached data
   1.284 +    response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
   1.285 +  } else {
   1.286 +    response.setStatusLine(metadata.httpVersion, 200, "OK");
   1.287 +    response.setHeader("Content-Type", "text/plain", false);
   1.288 +    response.setHeader("ETag", "testtag", false);
   1.289 +    const body = "data";
   1.290 +    response.bodyOutputStream.write(body, body.length);
   1.291 +  }
   1.292 +}
   1.293 +
   1.294 +function nocache_handler(metadata, response) {
   1.295 +  response.setHeader("Cache-Control", "no-cache", false);
   1.296 +  handler(metadata, response);
   1.297 +}
   1.298 +
   1.299 +function nostore_handler(metadata, response) {
   1.300 +  response.setHeader("Cache-Control", "no-store", false);
   1.301 +  handler(metadata, response);
   1.302 +}
   1.303 +
   1.304 +function shortexp_handler(metadata, response) {
   1.305 +  response.setHeader("Cache-Control", "max-age=0", false);
   1.306 +  handler(metadata, response);
   1.307 +}
   1.308 +
   1.309 +function longexp_handler(metadata, response) {
   1.310 +  response.setHeader("Cache-Control", "max-age=10000", false);
   1.311 +  handler(metadata, response);
   1.312 +}
   1.313 +
   1.314 +// test spaces around max-age value token
   1.315 +function longexp2_handler(metadata, response) {
   1.316 +  response.setHeader("Cache-Control", "max-age = 10000", false);
   1.317 +  handler(metadata, response);
   1.318 +}
   1.319 +
   1.320 +function run_test() {
   1.321 +  httpserver.registerPathHandler(shortexpPath, shortexp_handler);
   1.322 +  httpserver.registerPathHandler(longexpPath, longexp_handler);
   1.323 +  httpserver.registerPathHandler(longexp2Path, longexp2_handler);
   1.324 +  httpserver.registerPathHandler(nocachePath, nocache_handler);
   1.325 +  httpserver.registerPathHandler(nostorePath, nostore_handler);
   1.326 +
   1.327 +  run_next_test();
   1.328 +  do_test_pending();
   1.329 +}

mercurial