netwerk/test/unit/test_fallback_request-error_passing.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

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 // redirect handler returns redirect
michael@0 51 function redirectHandler(metadata, response)
michael@0 52 {
michael@0 53 response.setStatusLine(metadata.httpVersion, 301, "Moved");
michael@0 54 response.setHeader("Location", "http://example.com/", false);
michael@0 55 }
michael@0 56
michael@0 57 // finally check we got fallback content
michael@0 58 function finish_test(request, buffer)
michael@0 59 {
michael@0 60 do_check_eq(buffer, responseBody);
michael@0 61 do_test_finished();
michael@0 62 }
michael@0 63
michael@0 64 function run_test()
michael@0 65 {
michael@0 66 httpServer = new HttpServer();
michael@0 67 httpServer.registerPathHandler("/masterEntry", masterEntryHandler);
michael@0 68 httpServer.registerPathHandler("/manifest", manifestHandler);
michael@0 69 httpServer.registerPathHandler("/content", contentHandler);
michael@0 70 httpServer.start(-1);
michael@0 71
michael@0 72 var pm = Cc["@mozilla.org/permissionmanager;1"]
michael@0 73 .getService(Ci.nsIPermissionManager);
michael@0 74 var uri = make_uri("http://localhost:" + httpServer.identity.primaryPort);
michael@0 75 var principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 76 .getService(Ci.nsIScriptSecurityManager)
michael@0 77 .getNoAppCodebasePrincipal(uri);
michael@0 78
michael@0 79 if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) {
michael@0 80 dump("Previous test failed to clear offline-app permission! Expect failures.\n");
michael@0 81 }
michael@0 82 pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 83
michael@0 84 var ps = Cc["@mozilla.org/preferences-service;1"]
michael@0 85 .getService(Ci.nsIPrefBranch);
michael@0 86 dump(ps.getBoolPref("browser.cache.offline.enable"));
michael@0 87 ps.setBoolPref("browser.cache.offline.enable", true);
michael@0 88 ps.setComplexValue("browser.cache.offline.parent_directory", Ci.nsILocalFile, do_get_profile());
michael@0 89
michael@0 90 cacheUpdateObserver = {observe: function() {
michael@0 91 dump("got offline-cache-update-completed\n");
michael@0 92 // offline cache update completed.
michael@0 93 var _x = randomURI; // doing this so the lazy value gets computed
michael@0 94 httpServer.stop(function() {
michael@0 95 // Now shut the server down to have an error in onstartrequest
michael@0 96 var chan = make_channel(randomURI);
michael@0 97 var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel);
michael@0 98 chanac.chooseApplicationCache = true;
michael@0 99 chan.asyncOpen(new ChannelListener(finish_test), null);
michael@0 100 });
michael@0 101 }}
michael@0 102
michael@0 103
michael@0 104 var os = Cc["@mozilla.org/observer-service;1"].
michael@0 105 getService(Ci.nsIObserverService);
michael@0 106 os.addObserver(cacheUpdateObserver, "offline-cache-update-completed", false);
michael@0 107
michael@0 108 var us = Cc["@mozilla.org/offlinecacheupdate-service;1"].
michael@0 109 getService(Ci.nsIOfflineCacheUpdateService);
michael@0 110 us.scheduleUpdate(make_uri("http://localhost:" +
michael@0 111 httpServer.identity.primaryPort + "/manifest"),
michael@0 112 make_uri("http://localhost:" +
michael@0 113 httpServer.identity.primaryPort + "/masterEntry"),
michael@0 114 null);
michael@0 115
michael@0 116 do_test_pending();
michael@0 117 }

mercurial