netwerk/test/unit/test_fallback_response-error_passing.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.)

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 = "/error/" + 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 /error path to /content
michael@0 36 function manifestHandler(metadata, response)
michael@0 37 {
michael@0 38 var manifestContent = "CACHE MANIFEST\nFALLBACK:\nerror /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 // error handler returns error
michael@0 51 function errorHandler(metadata, response)
michael@0 52 {
michael@0 53 response.setStatusLine(metadata.httpVersion, 404, "Bad request");
michael@0 54 }
michael@0 55
michael@0 56 // finally check we got fallback content
michael@0 57 function finish_test(request, buffer)
michael@0 58 {
michael@0 59 do_check_eq(buffer, responseBody);
michael@0 60 httpServer.stop(do_test_finished);
michael@0 61 }
michael@0 62
michael@0 63 function run_test()
michael@0 64 {
michael@0 65 httpServer = new HttpServer();
michael@0 66 httpServer.registerPathHandler("/masterEntry", masterEntryHandler);
michael@0 67 httpServer.registerPathHandler("/manifest", manifestHandler);
michael@0 68 httpServer.registerPathHandler("/content", contentHandler);
michael@0 69 httpServer.registerPathHandler(randomPath, errorHandler);
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 chan = make_channel(randomURI);
michael@0 94 var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel);
michael@0 95 chanac.chooseApplicationCache = true;
michael@0 96 chan.asyncOpen(new ChannelListener(finish_test), null);
michael@0 97 }}
michael@0 98
michael@0 99 var os = Cc["@mozilla.org/observer-service;1"].
michael@0 100 getService(Ci.nsIObserverService);
michael@0 101 os.addObserver(cacheUpdateObserver, "offline-cache-update-completed", false);
michael@0 102
michael@0 103 var us = Cc["@mozilla.org/offlinecacheupdate-service;1"].
michael@0 104 getService(Ci.nsIOfflineCacheUpdateService);
michael@0 105 us.scheduleUpdate(make_uri("http://localhost:" +
michael@0 106 httpServer.identity.primaryPort + "/manifest"),
michael@0 107 make_uri("http://localhost:" +
michael@0 108 httpServer.identity.primaryPort + "/masterEntry"),
michael@0 109 null);
michael@0 110
michael@0 111 do_test_pending();
michael@0 112 }

mercurial