netwerk/test/unit/test_fallback_no-cache-entry_canceled.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.

     1 Cu.import("resource://testing-common/httpd.js");
     3 var httpServer = null;
     4 // Need to randomize, because apparently no one clears our cache
     5 var randomPath = "/redirect/" + Math.random();
     7 XPCOMUtils.defineLazyGetter(this, "randomURI", function() {
     8   return "http://localhost:" + httpServer.identity.primaryPort + randomPath;
     9 });
    11 var cacheUpdateObserver = null;
    13 function make_channel(url, callback, ctx) {
    14   var ios = Cc["@mozilla.org/network/io-service;1"].
    15             getService(Ci.nsIIOService);
    16   return ios.newChannel(url, "", null);
    17 }
    19 function make_uri(url) {
    20   var ios = Cc["@mozilla.org/network/io-service;1"].
    21             getService(Ci.nsIIOService);
    22   return ios.newURI(url, null, null);
    23 }
    25 var responseBody = "Content body";
    27 // start the test with loading this master entry referencing the manifest
    28 function masterEntryHandler(metadata, response)
    29 {
    30   var masterEntryContent = "<html manifest='/manifest'></html>";
    31   response.setHeader("Content-Type", "text/html");
    32   response.bodyOutputStream.write(masterEntryContent, masterEntryContent.length);
    33 }
    35 // manifest defines fallback namespace from any /redirect path to /content
    36 function manifestHandler(metadata, response)
    37 {
    38   var manifestContent = "CACHE MANIFEST\nFALLBACK:\nredirect /content\n";
    39   response.setHeader("Content-Type", "text/cache-manifest");
    40   response.bodyOutputStream.write(manifestContent, manifestContent.length);
    41 }
    43 // content handler correctly returns some plain text data
    44 function contentHandler(metadata, response)
    45 {
    46   response.setHeader("Content-Type", "text/plain");
    47   response.bodyOutputStream.write(responseBody, responseBody.length);
    48 }
    50 // finally check we got fallback content
    51 function finish_test(request, buffer)
    52 {
    53   do_check_eq(buffer, "");
    54   httpServer.stop(do_test_finished);
    55 }
    57 function run_test()
    58 {
    59   httpServer = new HttpServer();
    60   httpServer.registerPathHandler("/masterEntry", masterEntryHandler);
    61   httpServer.registerPathHandler("/manifest", manifestHandler);
    62   httpServer.registerPathHandler("/content", contentHandler);
    63   httpServer.start(-1);
    65   var pm = Cc["@mozilla.org/permissionmanager;1"]
    66     .getService(Ci.nsIPermissionManager);
    67   var uri = make_uri("http://localhost:" + httpServer.identity.primaryPort);
    68   var principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
    69                     .getService(Ci.nsIScriptSecurityManager)
    70                     .getNoAppCodebasePrincipal(uri);
    72   if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) {
    73     dump("Previous test failed to clear offline-app permission!  Expect failures.\n");
    74   }
    75   pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
    77   var ps = Cc["@mozilla.org/preferences-service;1"]
    78     .getService(Ci.nsIPrefBranch);
    79   dump(ps.getBoolPref("browser.cache.offline.enable"));
    80   ps.setBoolPref("browser.cache.offline.enable", true);
    81   ps.setComplexValue("browser.cache.offline.parent_directory", Ci.nsILocalFile, do_get_profile());
    83   cacheUpdateObserver = {observe: function() {
    84     dump("got offline-cache-update-completed\n");
    85     // offline cache update completed.
    86     // In this test the randomURI doesn't exists
    87     var chan = make_channel(randomURI);
    88     chan.loadFlags = (Ci.nsIRequest.INHIBIT_CACHING |
    89                       Ci.nsIRequest.LOAD_FROM_CACHE |
    90                       Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE);
    91     chan.notificationCallbacks = new ChannelEventSink(ES_ABORT_REDIRECT);
    92     var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel);
    93     chanac.chooseApplicationCache = true;
    94     chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE), null);
    95   }}
    97   var os = Cc["@mozilla.org/observer-service;1"].
    98            getService(Ci.nsIObserverService);
    99   os.addObserver(cacheUpdateObserver, "offline-cache-update-completed", false);
   101   var us = Cc["@mozilla.org/offlinecacheupdate-service;1"].
   102            getService(Ci.nsIOfflineCacheUpdateService);
   103   us.scheduleUpdate(make_uri("http://localhost:" +
   104                              httpServer.identity.primaryPort + "/manifest"),
   105                     make_uri("http://localhost:" +
   106                              httpServer.identity.primaryPort + "/masterEntry"),
   107                     null);
   109   do_test_pending();
   110 }

mercurial