netwerk/test/unit/test_cacheForOfflineUse_no-store.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_cacheForOfflineUse_no-store.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +"use strict";
     1.5 +// https://bugzilla.mozilla.org/show_bug.cgi?id=760955
     1.6 +
     1.7 +Cu.import("resource://testing-common/httpd.js");
     1.8 +
     1.9 +var httpServer = null;
    1.10 +const testFileName = "test_nsHttpChannel_CacheForOfflineUse-no-store";
    1.11 +const cacheClientID = testFileName + "|fake-group-id";
    1.12 +const basePath = "/" + testFileName + "/";
    1.13 +
    1.14 +XPCOMUtils.defineLazyGetter(this, "baseURI", function() {
    1.15 +  return "http://localhost:" + httpServer.identity.primaryPort + basePath;
    1.16 +});
    1.17 +
    1.18 +const normalEntry = "normal";
    1.19 +const noStoreEntry = "no-store";
    1.20 +
    1.21 +var cacheUpdateObserver = null;
    1.22 +var appCache = null;
    1.23 +
    1.24 +function make_channel_for_offline_use(url, callback, ctx) {
    1.25 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.26 +            getService(Ci.nsIIOService);
    1.27 +  var chan = ios.newChannel(url, "", null);
    1.28 +  
    1.29 +  var cacheService = Components.classes["@mozilla.org/network/application-cache-service;1"].
    1.30 +                     getService(Components.interfaces.nsIApplicationCacheService);
    1.31 +  appCache = cacheService.getApplicationCache(cacheClientID);
    1.32 +  
    1.33 +  var appCacheChan = chan.QueryInterface(Ci.nsIApplicationCacheChannel);
    1.34 +  appCacheChan.applicationCacheForWrite = appCache;
    1.35 +  return chan;
    1.36 +}
    1.37 +
    1.38 +function make_uri(url) {
    1.39 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.40 +            getService(Ci.nsIIOService);
    1.41 +  return ios.newURI(url, null, null);
    1.42 +}
    1.43 +
    1.44 +function CacheListener() { }
    1.45 +CacheListener.prototype = {
    1.46 +  QueryInterface : function(iid)
    1.47 +  {
    1.48 +    if (iid.equals(Components.interfaces.nsICacheListener))
    1.49 +      return this;
    1.50 +    throw Components.results.NS_NOINTERFACE;
    1.51 +  },
    1.52 +};
    1.53 +
    1.54 +
    1.55 +const responseBody = "response body";
    1.56 +
    1.57 +// A HTTP channel for updating the offline cache should normally succeed.
    1.58 +function normalHandler(metadata, response)
    1.59 +{
    1.60 +  do_print("normalHandler");
    1.61 +  response.setHeader("Content-Type", "text/plain");
    1.62 +  response.bodyOutputStream.write(responseBody, responseBody.length);
    1.63 +}
    1.64 +function checkNormal(request, buffer)
    1.65 +{
    1.66 +  do_check_eq(buffer, responseBody);
    1.67 +  asyncCheckCacheEntryPresence(baseURI + normalEntry, "appcache", true, run_next_test, appCache);
    1.68 +}
    1.69 +add_test(function test_normal() {
    1.70 +  var chan = make_channel_for_offline_use(baseURI + normalEntry);
    1.71 +  chan.asyncOpen(new ChannelListener(checkNormal, chan), null);
    1.72 +});
    1.73 +
    1.74 +// An HTTP channel for updating the offline cache should fail when it gets a
    1.75 +// response with Cache-Control: no-store.
    1.76 +function noStoreHandler(metadata, response)
    1.77 +{
    1.78 +  do_print("noStoreHandler");
    1.79 +  response.setHeader("Content-Type", "text/plain");
    1.80 +  response.setHeader("Cache-Control", "no-store");
    1.81 +  response.bodyOutputStream.write(responseBody, responseBody.length);
    1.82 +}
    1.83 +function checkNoStore(request, buffer)
    1.84 +{
    1.85 +  do_check_eq(buffer, "");
    1.86 +  asyncCheckCacheEntryPresence(baseURI + noStoreEntry, "appcache", false, run_next_test, appCache);
    1.87 +}
    1.88 +add_test(function test_noStore() {
    1.89 +  var chan = make_channel_for_offline_use(baseURI + noStoreEntry);
    1.90 +  // The no-store should cause the channel to fail to load.
    1.91 +  chan.asyncOpen(new ChannelListener(checkNoStore, chan, CL_EXPECT_FAILURE),
    1.92 +                 null);
    1.93 +});
    1.94 +
    1.95 +function run_test()
    1.96 +{
    1.97 +  do_get_profile();
    1.98 +
    1.99 +  httpServer = new HttpServer();
   1.100 +  httpServer.registerPathHandler(basePath + normalEntry, normalHandler);
   1.101 +  httpServer.registerPathHandler(basePath + noStoreEntry, noStoreHandler);
   1.102 +  httpServer.start(-1);
   1.103 +  run_next_test();
   1.104 +}
   1.105 +
   1.106 +function finish_test(request, buffer)
   1.107 +{
   1.108 +  httpServer.stop(do_test_finished);
   1.109 +}

mercurial