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

mercurial