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