michael@0: "use strict"; michael@0: // https://bugzilla.mozilla.org/show_bug.cgi?id=760955 michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: var httpServer = null; michael@0: const testFileName = "test_nsHttpChannel_CacheForOfflineUse-no-store"; michael@0: const cacheClientID = testFileName + "|fake-group-id"; michael@0: const basePath = "/" + testFileName + "/"; michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "baseURI", function() { michael@0: return "http://localhost:" + httpServer.identity.primaryPort + basePath; michael@0: }); michael@0: michael@0: const normalEntry = "normal"; michael@0: const noStoreEntry = "no-store"; michael@0: michael@0: var cacheUpdateObserver = null; michael@0: var appCache = null; michael@0: michael@0: function make_channel_for_offline_use(url, callback, ctx) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: var chan = ios.newChannel(url, "", null); michael@0: michael@0: var cacheService = Components.classes["@mozilla.org/network/application-cache-service;1"]. michael@0: getService(Components.interfaces.nsIApplicationCacheService); michael@0: appCache = cacheService.getApplicationCache(cacheClientID); michael@0: michael@0: var appCacheChan = chan.QueryInterface(Ci.nsIApplicationCacheChannel); michael@0: appCacheChan.applicationCacheForWrite = appCache; michael@0: return chan; michael@0: } michael@0: michael@0: function make_uri(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: return ios.newURI(url, null, null); michael@0: } michael@0: michael@0: function CacheListener() { } michael@0: CacheListener.prototype = { michael@0: QueryInterface : function(iid) michael@0: { michael@0: if (iid.equals(Components.interfaces.nsICacheListener)) michael@0: return this; michael@0: throw Components.results.NS_NOINTERFACE; michael@0: }, michael@0: }; michael@0: michael@0: michael@0: const responseBody = "response body"; michael@0: michael@0: // A HTTP channel for updating the offline cache should normally succeed. michael@0: function normalHandler(metadata, response) michael@0: { michael@0: do_print("normalHandler"); michael@0: response.setHeader("Content-Type", "text/plain"); michael@0: response.bodyOutputStream.write(responseBody, responseBody.length); michael@0: } michael@0: function checkNormal(request, buffer) michael@0: { michael@0: do_check_eq(buffer, responseBody); michael@0: asyncCheckCacheEntryPresence(baseURI + normalEntry, "appcache", true, run_next_test, appCache); michael@0: } michael@0: add_test(function test_normal() { michael@0: var chan = make_channel_for_offline_use(baseURI + normalEntry); michael@0: chan.asyncOpen(new ChannelListener(checkNormal, chan), null); michael@0: }); michael@0: michael@0: // An HTTP channel for updating the offline cache should fail when it gets a michael@0: // response with Cache-Control: no-store. michael@0: function noStoreHandler(metadata, response) michael@0: { michael@0: do_print("noStoreHandler"); michael@0: response.setHeader("Content-Type", "text/plain"); michael@0: response.setHeader("Cache-Control", "no-store"); michael@0: response.bodyOutputStream.write(responseBody, responseBody.length); michael@0: } michael@0: function checkNoStore(request, buffer) michael@0: { michael@0: do_check_eq(buffer, ""); michael@0: asyncCheckCacheEntryPresence(baseURI + noStoreEntry, "appcache", false, run_next_test, appCache); michael@0: } michael@0: add_test(function test_noStore() { michael@0: var chan = make_channel_for_offline_use(baseURI + noStoreEntry); michael@0: // The no-store should cause the channel to fail to load. michael@0: chan.asyncOpen(new ChannelListener(checkNoStore, chan, CL_EXPECT_FAILURE), michael@0: null); michael@0: }); michael@0: michael@0: function run_test() michael@0: { michael@0: do_get_profile(); michael@0: michael@0: httpServer = new HttpServer(); michael@0: httpServer.registerPathHandler(basePath + normalEntry, normalHandler); michael@0: httpServer.registerPathHandler(basePath + noStoreEntry, noStoreHandler); michael@0: httpServer.start(-1); michael@0: run_next_test(); michael@0: } michael@0: michael@0: function finish_test(request, buffer) michael@0: { michael@0: httpServer.stop(do_test_finished); michael@0: }