1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_fallback_no-cache-entry_passing.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 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 = "/redirect/" + 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 /redirect path to /content 1.39 +function manifestHandler(metadata, response) 1.40 +{ 1.41 + var manifestContent = "CACHE MANIFEST\nFALLBACK:\nredirect /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 +// finally check we got fallback content 1.54 +function finish_test(request, buffer) 1.55 +{ 1.56 + do_check_eq(buffer, responseBody); 1.57 + httpServer.stop(do_test_finished); 1.58 +} 1.59 + 1.60 +function run_test() 1.61 +{ 1.62 + httpServer = new HttpServer(); 1.63 + httpServer.registerPathHandler("/masterEntry", masterEntryHandler); 1.64 + httpServer.registerPathHandler("/manifest", manifestHandler); 1.65 + httpServer.registerPathHandler("/content", contentHandler); 1.66 + httpServer.start(-1); 1.67 + 1.68 + var pm = Cc["@mozilla.org/permissionmanager;1"] 1.69 + .getService(Ci.nsIPermissionManager); 1.70 + var uri = make_uri("http://localhost:" + httpServer.identity.primaryPort); 1.71 + var principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"] 1.72 + .getService(Ci.nsIScriptSecurityManager) 1.73 + .getNoAppCodebasePrincipal(uri); 1.74 + 1.75 + if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) { 1.76 + dump("Previous test failed to clear offline-app permission! Expect failures.\n"); 1.77 + } 1.78 + pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION); 1.79 + 1.80 + var ps = Cc["@mozilla.org/preferences-service;1"] 1.81 + .getService(Ci.nsIPrefBranch); 1.82 + dump(ps.getBoolPref("browser.cache.offline.enable")); 1.83 + ps.setBoolPref("browser.cache.offline.enable", true); 1.84 + ps.setComplexValue("browser.cache.offline.parent_directory", Ci.nsILocalFile, do_get_profile()); 1.85 + 1.86 + cacheUpdateObserver = {observe: function() { 1.87 + dump("got offline-cache-update-completed\n"); 1.88 + // offline cache update completed. 1.89 + // In this test the randomURI doesn't exists 1.90 + var chan = make_channel(randomURI); 1.91 + chan.loadFlags = (Ci.nsIRequest.INHIBIT_CACHING | 1.92 + Ci.nsIRequest.LOAD_FROM_CACHE | 1.93 + Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE); 1.94 + var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel); 1.95 + chanac.chooseApplicationCache = true; 1.96 + chan.asyncOpen(new ChannelListener(finish_test), null); 1.97 + }} 1.98 + 1.99 + var os = Cc["@mozilla.org/observer-service;1"]. 1.100 + getService(Ci.nsIObserverService); 1.101 + os.addObserver(cacheUpdateObserver, "offline-cache-update-completed", false); 1.102 + 1.103 + var us = Cc["@mozilla.org/offlinecacheupdate-service;1"]. 1.104 + getService(Ci.nsIOfflineCacheUpdateService); 1.105 + us.scheduleUpdate(make_uri("http://localhost:" + 1.106 + httpServer.identity.primaryPort + "/manifest"), 1.107 + make_uri("http://localhost:" + 1.108 + httpServer.identity.primaryPort + "/masterEntry"), 1.109 + null); 1.110 + 1.111 + do_test_pending(); 1.112 +}