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