netwerk/test/unit/test_fallback_response-error_passing.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 = "/error/" + 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 /error path to /content
    36 function manifestHandler(metadata, response)
    37 {
    38   var manifestContent = "CACHE MANIFEST\nFALLBACK:\nerror /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 // error handler returns error
    51 function errorHandler(metadata, response)
    52 {
    53   response.setStatusLine(metadata.httpVersion, 404, "Bad request");
    54 }
    56 // finally check we got fallback content
    57 function finish_test(request, buffer)
    58 {
    59   do_check_eq(buffer, responseBody);
    60   httpServer.stop(do_test_finished);
    61 }
    63 function run_test()
    64 {
    65   httpServer = new HttpServer();
    66   httpServer.registerPathHandler("/masterEntry", masterEntryHandler);
    67   httpServer.registerPathHandler("/manifest", manifestHandler);
    68   httpServer.registerPathHandler("/content", contentHandler);
    69   httpServer.registerPathHandler(randomPath, errorHandler);
    70   httpServer.start(-1);
    72   var pm = Cc["@mozilla.org/permissionmanager;1"]
    73     .getService(Ci.nsIPermissionManager);
    74   var uri = make_uri("http://localhost:" + httpServer.identity.primaryPort);
    75   var principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
    76                     .getService(Ci.nsIScriptSecurityManager)
    77                     .getNoAppCodebasePrincipal(uri);
    79   if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) {
    80     dump("Previous test failed to clear offline-app permission!  Expect failures.\n");
    81   }
    82   pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
    84   var ps = Cc["@mozilla.org/preferences-service;1"]
    85     .getService(Ci.nsIPrefBranch);
    86   dump(ps.getBoolPref("browser.cache.offline.enable"));
    87   ps.setBoolPref("browser.cache.offline.enable", true);
    88   ps.setComplexValue("browser.cache.offline.parent_directory", Ci.nsILocalFile, do_get_profile());
    90   cacheUpdateObserver = {observe: function() {
    91     dump("got offline-cache-update-completed\n");
    92     // offline cache update completed.
    93     var chan = make_channel(randomURI);
    94     var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel);
    95     chanac.chooseApplicationCache = true;
    96     chan.asyncOpen(new ChannelListener(finish_test), null);
    97   }}
    99   var os = Cc["@mozilla.org/observer-service;1"].
   100            getService(Ci.nsIObserverService);
   101   os.addObserver(cacheUpdateObserver, "offline-cache-update-completed", false);
   103   var us = Cc["@mozilla.org/offlinecacheupdate-service;1"].
   104            getService(Ci.nsIOfflineCacheUpdateService);
   105   us.scheduleUpdate(make_uri("http://localhost:" +
   106                              httpServer.identity.primaryPort + "/manifest"),
   107                     make_uri("http://localhost:" +
   108                              httpServer.identity.primaryPort + "/masterEntry"),
   109                     null);
   111   do_test_pending();
   112 }

mercurial