netwerk/test/unit/test_fallback_response-error_passing.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_fallback_response-error_passing.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +var httpServer = null;
     1.7 +// Need to randomize, because apparently no one clears our cache
     1.8 +var randomPath = "/error/" + Math.random();
     1.9 +
    1.10 +XPCOMUtils.defineLazyGetter(this, "randomURI", function() {
    1.11 +  return "http://localhost:" + httpServer.identity.primaryPort + randomPath;
    1.12 +});
    1.13 +
    1.14 +var cacheUpdateObserver = null;
    1.15 +
    1.16 +function make_channel(url, callback, ctx) {
    1.17 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.18 +            getService(Ci.nsIIOService);
    1.19 +  return ios.newChannel(url, "", null);
    1.20 +}
    1.21 +
    1.22 +function make_uri(url) {
    1.23 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.24 +            getService(Ci.nsIIOService);
    1.25 +  return ios.newURI(url, null, null);
    1.26 +}
    1.27 +
    1.28 +var responseBody = "Content body";
    1.29 +
    1.30 +// start the test with loading this master entry referencing the manifest
    1.31 +function masterEntryHandler(metadata, response)
    1.32 +{
    1.33 +  var masterEntryContent = "<html manifest='/manifest'></html>";
    1.34 +  response.setHeader("Content-Type", "text/html");
    1.35 +  response.bodyOutputStream.write(masterEntryContent, masterEntryContent.length);
    1.36 +}
    1.37 +
    1.38 +// manifest defines fallback namespace from any /error path to /content
    1.39 +function manifestHandler(metadata, response)
    1.40 +{
    1.41 +  var manifestContent = "CACHE MANIFEST\nFALLBACK:\nerror /content\n";
    1.42 +  response.setHeader("Content-Type", "text/cache-manifest");
    1.43 +  response.bodyOutputStream.write(manifestContent, manifestContent.length);
    1.44 +}
    1.45 +
    1.46 +// content handler correctly returns some plain text data
    1.47 +function contentHandler(metadata, response)
    1.48 +{
    1.49 +  response.setHeader("Content-Type", "text/plain");
    1.50 +  response.bodyOutputStream.write(responseBody, responseBody.length);
    1.51 +}
    1.52 +
    1.53 +// error handler returns error
    1.54 +function errorHandler(metadata, response)
    1.55 +{
    1.56 +  response.setStatusLine(metadata.httpVersion, 404, "Bad request");
    1.57 +}
    1.58 +
    1.59 +// finally check we got fallback content
    1.60 +function finish_test(request, buffer)
    1.61 +{
    1.62 +  do_check_eq(buffer, responseBody);
    1.63 +  httpServer.stop(do_test_finished);
    1.64 +}
    1.65 +
    1.66 +function run_test()
    1.67 +{
    1.68 +  httpServer = new HttpServer();
    1.69 +  httpServer.registerPathHandler("/masterEntry", masterEntryHandler);
    1.70 +  httpServer.registerPathHandler("/manifest", manifestHandler);
    1.71 +  httpServer.registerPathHandler("/content", contentHandler);
    1.72 +  httpServer.registerPathHandler(randomPath, errorHandler);
    1.73 +  httpServer.start(-1);
    1.74 +
    1.75 +  var pm = Cc["@mozilla.org/permissionmanager;1"]
    1.76 +    .getService(Ci.nsIPermissionManager);
    1.77 +  var uri = make_uri("http://localhost:" + httpServer.identity.primaryPort);
    1.78 +  var principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
    1.79 +                    .getService(Ci.nsIScriptSecurityManager)
    1.80 +                    .getNoAppCodebasePrincipal(uri);
    1.81 +
    1.82 +  if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) {
    1.83 +    dump("Previous test failed to clear offline-app permission!  Expect failures.\n");
    1.84 +  }
    1.85 +  pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
    1.86 +
    1.87 +  var ps = Cc["@mozilla.org/preferences-service;1"]
    1.88 +    .getService(Ci.nsIPrefBranch);
    1.89 +  dump(ps.getBoolPref("browser.cache.offline.enable"));
    1.90 +  ps.setBoolPref("browser.cache.offline.enable", true);
    1.91 +  ps.setComplexValue("browser.cache.offline.parent_directory", Ci.nsILocalFile, do_get_profile());
    1.92 +
    1.93 +  cacheUpdateObserver = {observe: function() {
    1.94 +    dump("got offline-cache-update-completed\n");
    1.95 +    // offline cache update completed.
    1.96 +    var chan = make_channel(randomURI);
    1.97 +    var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel);
    1.98 +    chanac.chooseApplicationCache = true;
    1.99 +    chan.asyncOpen(new ChannelListener(finish_test), null);
   1.100 +  }}
   1.101 +
   1.102 +  var os = Cc["@mozilla.org/observer-service;1"].
   1.103 +           getService(Ci.nsIObserverService);
   1.104 +  os.addObserver(cacheUpdateObserver, "offline-cache-update-completed", false);
   1.105 +
   1.106 +  var us = Cc["@mozilla.org/offlinecacheupdate-service;1"].
   1.107 +           getService(Ci.nsIOfflineCacheUpdateService);
   1.108 +  us.scheduleUpdate(make_uri("http://localhost:" +
   1.109 +                             httpServer.identity.primaryPort + "/manifest"),
   1.110 +                    make_uri("http://localhost:" +
   1.111 +                             httpServer.identity.primaryPort + "/masterEntry"),
   1.112 +                    null);
   1.113 +
   1.114 +  do_test_pending();
   1.115 +}

mercurial