|
1 "use strict"; |
|
2 // https://bugzilla.mozilla.org/show_bug.cgi?id=760955 |
|
3 |
|
4 Cu.import("resource://testing-common/httpd.js"); |
|
5 |
|
6 var httpServer = null; |
|
7 const testFileName = "test_nsHttpChannel_CacheForOfflineUse-no-store"; |
|
8 const cacheClientID = testFileName + "|fake-group-id"; |
|
9 const basePath = "/" + testFileName + "/"; |
|
10 |
|
11 XPCOMUtils.defineLazyGetter(this, "baseURI", function() { |
|
12 return "http://localhost:" + httpServer.identity.primaryPort + basePath; |
|
13 }); |
|
14 |
|
15 const normalEntry = "normal"; |
|
16 const noStoreEntry = "no-store"; |
|
17 |
|
18 var cacheUpdateObserver = null; |
|
19 var appCache = null; |
|
20 |
|
21 function make_channel_for_offline_use(url, callback, ctx) { |
|
22 var ios = Cc["@mozilla.org/network/io-service;1"]. |
|
23 getService(Ci.nsIIOService); |
|
24 var chan = ios.newChannel(url, "", null); |
|
25 |
|
26 var cacheService = Components.classes["@mozilla.org/network/application-cache-service;1"]. |
|
27 getService(Components.interfaces.nsIApplicationCacheService); |
|
28 appCache = cacheService.getApplicationCache(cacheClientID); |
|
29 |
|
30 var appCacheChan = chan.QueryInterface(Ci.nsIApplicationCacheChannel); |
|
31 appCacheChan.applicationCacheForWrite = appCache; |
|
32 return chan; |
|
33 } |
|
34 |
|
35 function make_uri(url) { |
|
36 var ios = Cc["@mozilla.org/network/io-service;1"]. |
|
37 getService(Ci.nsIIOService); |
|
38 return ios.newURI(url, null, null); |
|
39 } |
|
40 |
|
41 function CacheListener() { } |
|
42 CacheListener.prototype = { |
|
43 QueryInterface : function(iid) |
|
44 { |
|
45 if (iid.equals(Components.interfaces.nsICacheListener)) |
|
46 return this; |
|
47 throw Components.results.NS_NOINTERFACE; |
|
48 }, |
|
49 }; |
|
50 |
|
51 |
|
52 const responseBody = "response body"; |
|
53 |
|
54 // A HTTP channel for updating the offline cache should normally succeed. |
|
55 function normalHandler(metadata, response) |
|
56 { |
|
57 do_print("normalHandler"); |
|
58 response.setHeader("Content-Type", "text/plain"); |
|
59 response.bodyOutputStream.write(responseBody, responseBody.length); |
|
60 } |
|
61 function checkNormal(request, buffer) |
|
62 { |
|
63 do_check_eq(buffer, responseBody); |
|
64 asyncCheckCacheEntryPresence(baseURI + normalEntry, "appcache", true, run_next_test, appCache); |
|
65 } |
|
66 add_test(function test_normal() { |
|
67 var chan = make_channel_for_offline_use(baseURI + normalEntry); |
|
68 chan.asyncOpen(new ChannelListener(checkNormal, chan), null); |
|
69 }); |
|
70 |
|
71 // An HTTP channel for updating the offline cache should fail when it gets a |
|
72 // response with Cache-Control: no-store. |
|
73 function noStoreHandler(metadata, response) |
|
74 { |
|
75 do_print("noStoreHandler"); |
|
76 response.setHeader("Content-Type", "text/plain"); |
|
77 response.setHeader("Cache-Control", "no-store"); |
|
78 response.bodyOutputStream.write(responseBody, responseBody.length); |
|
79 } |
|
80 function checkNoStore(request, buffer) |
|
81 { |
|
82 do_check_eq(buffer, ""); |
|
83 asyncCheckCacheEntryPresence(baseURI + noStoreEntry, "appcache", false, run_next_test, appCache); |
|
84 } |
|
85 add_test(function test_noStore() { |
|
86 var chan = make_channel_for_offline_use(baseURI + noStoreEntry); |
|
87 // The no-store should cause the channel to fail to load. |
|
88 chan.asyncOpen(new ChannelListener(checkNoStore, chan, CL_EXPECT_FAILURE), |
|
89 null); |
|
90 }); |
|
91 |
|
92 function run_test() |
|
93 { |
|
94 do_get_profile(); |
|
95 |
|
96 httpServer = new HttpServer(); |
|
97 httpServer.registerPathHandler(basePath + normalEntry, normalHandler); |
|
98 httpServer.registerPathHandler(basePath + noStoreEntry, noStoreHandler); |
|
99 httpServer.start(-1); |
|
100 run_next_test(); |
|
101 } |
|
102 |
|
103 function finish_test(request, buffer) |
|
104 { |
|
105 httpServer.stop(do_test_finished); |
|
106 } |