netwerk/test/unit/test_cacheflags.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

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

mercurial