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.

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

mercurial