netwerk/test/unit/test_fallback_request-error_canceled.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     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 // redirect handler returns redirect
    51 function redirectHandler(metadata, response)
    52 {
    53   response.setStatusLine(metadata.httpVersion, 301, "Moved");
    54   response.setHeader("Location", "http://example.com/", false);
    55 }
    57 // finally check we got fallback content
    58 function finish_test(request, buffer)
    59 {
    60   do_check_eq(buffer, "");
    61   do_test_finished();
    62 }
    64 function run_test()
    65 {
    66   httpServer = new HttpServer();
    67   httpServer.registerPathHandler("/masterEntry", masterEntryHandler);
    68   httpServer.registerPathHandler("/manifest", manifestHandler);
    69   httpServer.registerPathHandler("/content", contentHandler);
    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.
    94     // doing this to eval the lazy getter, otherwise the make_channel call would hang
    95     randomURI;
    96     httpServer.stop(function() {
    97       // Now shut the server down to have an error in onStartRequest
    98       var chan = make_channel(randomURI);
    99       chan.notificationCallbacks = new ChannelEventSink(ES_ABORT_REDIRECT);
   100       var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel);
   101       chanac.chooseApplicationCache = true;
   102       chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE), null);
   103     });
   104   }}
   107   var os = Cc["@mozilla.org/observer-service;1"].
   108            getService(Ci.nsIObserverService);
   109   os.addObserver(cacheUpdateObserver, "offline-cache-update-completed", false);
   111   var us = Cc["@mozilla.org/offlinecacheupdate-service;1"].
   112            getService(Ci.nsIOfflineCacheUpdateService);
   113   us.scheduleUpdate(make_uri("http://localhost:" +
   114                              httpServer.identity.primaryPort + "/manifest"),
   115                     make_uri("http://localhost:" +
   116                              httpServer.identity.primaryPort + "/masterEntry"),
   117                     null);
   119   do_test_pending();
   120 }

mercurial