netwerk/test/unit/test_bug812167.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug812167.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +/*
     1.7 +- get 302 with Cache-control: no-store
     1.8 +- check cache entry for the 302 response is cached only in memory device
     1.9 +- get 302 with Expires: -1
    1.10 +- check cache entry for the 302 response is not cached at all
    1.11 +*/
    1.12 +
    1.13 +var httpserver = null;
    1.14 +// Need to randomize, because apparently no one clears our cache
    1.15 +var randomPath1 = "/redirect-no-store/" + Math.random();
    1.16 +
    1.17 +XPCOMUtils.defineLazyGetter(this, "randomURI1", function() {
    1.18 +  return "http://localhost:" + httpserver.identity.primaryPort + randomPath1;
    1.19 +});
    1.20 +
    1.21 +var randomPath2 = "/redirect-expires-past/" + Math.random();
    1.22 +
    1.23 +XPCOMUtils.defineLazyGetter(this, "randomURI2", function() {
    1.24 +  return "http://localhost:" + httpserver.identity.primaryPort + randomPath2;
    1.25 +});
    1.26 +
    1.27 +function make_channel(url, callback, ctx) {
    1.28 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.29 +            getService(Ci.nsIIOService);
    1.30 +  return ios.newChannel(url, "", null);
    1.31 +}
    1.32 +
    1.33 +const responseBody = "response body";
    1.34 +
    1.35 +var redirectHandler_NoStore_calls = 0;
    1.36 +function redirectHandler_NoStore(metadata, response)
    1.37 +{
    1.38 +  response.setStatusLine(metadata.httpVersion, 302, "Found");
    1.39 +  response.setHeader("Location", "http://localhost:" +
    1.40 +                     httpserver.identity.primaryPort + "/content", false);
    1.41 +  response.setHeader("Cache-control", "no-store");
    1.42 +  ++redirectHandler_NoStore_calls;
    1.43 +  return;
    1.44 +}
    1.45 +
    1.46 +var redirectHandler_ExpiresInPast_calls = 0;
    1.47 +function redirectHandler_ExpiresInPast(metadata, response)
    1.48 +{
    1.49 +  response.setStatusLine(metadata.httpVersion, 302, "Found");
    1.50 +  response.setHeader("Location", "http://localhost:" +
    1.51 +                     httpserver.identity.primaryPort + "/content", false);
    1.52 +  response.setHeader("Expires", "-1");
    1.53 +  ++redirectHandler_ExpiresInPast_calls;
    1.54 +  return;
    1.55 +}
    1.56 +
    1.57 +function contentHandler(metadata, response)
    1.58 +{
    1.59 +  response.setHeader("Content-Type", "text/plain");
    1.60 +  response.bodyOutputStream.write(responseBody, responseBody.length);
    1.61 +}
    1.62 +
    1.63 +function check_response(path, request, buffer, expectedExpiration, continuation)
    1.64 +{
    1.65 +  do_check_eq(buffer, responseBody);
    1.66 +
    1.67 +  // Entry is always there, old cache wrapping code does session->SetDoomEntriesIfExpired(false),
    1.68 +  // just check it's not persisted or is expired (dep on the test).
    1.69 +  asyncOpenCacheEntry(path, "disk", Ci.nsICacheStorage.OPEN_READONLY, null, function(status, entry) {
    1.70 +    do_check_eq(status, 0);
    1.71 +
    1.72 +    // Expired entry is on disk, no-store entry is in memory
    1.73 +    do_check_eq(entry.persistent, expectedExpiration);
    1.74 +
    1.75 +    // Do the request again and check the server handler is called appropriately
    1.76 +    var chan = make_channel(path);
    1.77 +    chan.asyncOpen(new ChannelListener(function(request, buffer) {
    1.78 +      do_check_eq(buffer, responseBody);
    1.79 +
    1.80 +      if (expectedExpiration) {
    1.81 +        // Handler had to be called second time
    1.82 +        do_check_eq(redirectHandler_ExpiresInPast_calls, 2);
    1.83 +      }
    1.84 +      else {
    1.85 +        // Handler had to be called second time (no-store forces validate),
    1.86 +        // and we are just in memory
    1.87 +        do_check_eq(redirectHandler_NoStore_calls, 2);
    1.88 +        do_check_true(!entry.persistent);
    1.89 +      }
    1.90 +
    1.91 +      continuation();
    1.92 +    }, null), null);
    1.93 +  });
    1.94 +}
    1.95 +
    1.96 +function run_test_no_store()
    1.97 +{
    1.98 +  var chan = make_channel(randomURI1);
    1.99 +  chan.asyncOpen(new ChannelListener(function(request, buffer) {
   1.100 +    // Cache-control: no-store response should only be found in the memory cache.
   1.101 +    check_response(randomURI1, request, buffer, false, run_test_expires_past);
   1.102 +  }, null), null);
   1.103 +}
   1.104 +
   1.105 +function run_test_expires_past()
   1.106 +{
   1.107 +  var chan = make_channel(randomURI2);
   1.108 +  chan.asyncOpen(new ChannelListener(function(request, buffer) {
   1.109 +    // Expires: -1 response should not be found in any cache.
   1.110 +    check_response(randomURI2, request, buffer, true, finish_test);
   1.111 +  }, null), null);
   1.112 +}
   1.113 +
   1.114 +function finish_test()
   1.115 +{
   1.116 +  httpserver.stop(do_test_finished);
   1.117 +}
   1.118 +
   1.119 +function run_test()
   1.120 +{
   1.121 +  do_get_profile();
   1.122 +
   1.123 +  httpserver = new HttpServer();
   1.124 +  httpserver.registerPathHandler(randomPath1, redirectHandler_NoStore);
   1.125 +  httpserver.registerPathHandler(randomPath2, redirectHandler_ExpiresInPast);
   1.126 +  httpserver.registerPathHandler("/content", contentHandler);
   1.127 +  httpserver.start(-1);
   1.128 +
   1.129 +  run_test_no_store();
   1.130 +  do_test_pending();
   1.131 +}

mercurial