michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: /* michael@0: - get 302 with Cache-control: no-store michael@0: - check cache entry for the 302 response is cached only in memory device michael@0: - get 302 with Expires: -1 michael@0: - check cache entry for the 302 response is not cached at all michael@0: */ michael@0: michael@0: var httpserver = null; michael@0: // Need to randomize, because apparently no one clears our cache michael@0: var randomPath1 = "/redirect-no-store/" + Math.random(); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "randomURI1", function() { michael@0: return "http://localhost:" + httpserver.identity.primaryPort + randomPath1; michael@0: }); michael@0: michael@0: var randomPath2 = "/redirect-expires-past/" + Math.random(); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "randomURI2", function() { michael@0: return "http://localhost:" + httpserver.identity.primaryPort + randomPath2; michael@0: }); michael@0: michael@0: function make_channel(url, callback, ctx) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: return ios.newChannel(url, "", null); michael@0: } michael@0: michael@0: const responseBody = "response body"; michael@0: michael@0: var redirectHandler_NoStore_calls = 0; michael@0: function redirectHandler_NoStore(metadata, response) michael@0: { michael@0: response.setStatusLine(metadata.httpVersion, 302, "Found"); michael@0: response.setHeader("Location", "http://localhost:" + michael@0: httpserver.identity.primaryPort + "/content", false); michael@0: response.setHeader("Cache-control", "no-store"); michael@0: ++redirectHandler_NoStore_calls; michael@0: return; michael@0: } michael@0: michael@0: var redirectHandler_ExpiresInPast_calls = 0; michael@0: function redirectHandler_ExpiresInPast(metadata, response) michael@0: { michael@0: response.setStatusLine(metadata.httpVersion, 302, "Found"); michael@0: response.setHeader("Location", "http://localhost:" + michael@0: httpserver.identity.primaryPort + "/content", false); michael@0: response.setHeader("Expires", "-1"); michael@0: ++redirectHandler_ExpiresInPast_calls; michael@0: return; michael@0: } michael@0: michael@0: function contentHandler(metadata, response) michael@0: { michael@0: response.setHeader("Content-Type", "text/plain"); michael@0: response.bodyOutputStream.write(responseBody, responseBody.length); michael@0: } michael@0: michael@0: function check_response(path, request, buffer, expectedExpiration, continuation) michael@0: { michael@0: do_check_eq(buffer, responseBody); michael@0: michael@0: // Entry is always there, old cache wrapping code does session->SetDoomEntriesIfExpired(false), michael@0: // just check it's not persisted or is expired (dep on the test). michael@0: asyncOpenCacheEntry(path, "disk", Ci.nsICacheStorage.OPEN_READONLY, null, function(status, entry) { michael@0: do_check_eq(status, 0); michael@0: michael@0: // Expired entry is on disk, no-store entry is in memory michael@0: do_check_eq(entry.persistent, expectedExpiration); michael@0: michael@0: // Do the request again and check the server handler is called appropriately michael@0: var chan = make_channel(path); michael@0: chan.asyncOpen(new ChannelListener(function(request, buffer) { michael@0: do_check_eq(buffer, responseBody); michael@0: michael@0: if (expectedExpiration) { michael@0: // Handler had to be called second time michael@0: do_check_eq(redirectHandler_ExpiresInPast_calls, 2); michael@0: } michael@0: else { michael@0: // Handler had to be called second time (no-store forces validate), michael@0: // and we are just in memory michael@0: do_check_eq(redirectHandler_NoStore_calls, 2); michael@0: do_check_true(!entry.persistent); michael@0: } michael@0: michael@0: continuation(); michael@0: }, null), null); michael@0: }); michael@0: } michael@0: michael@0: function run_test_no_store() michael@0: { michael@0: var chan = make_channel(randomURI1); michael@0: chan.asyncOpen(new ChannelListener(function(request, buffer) { michael@0: // Cache-control: no-store response should only be found in the memory cache. michael@0: check_response(randomURI1, request, buffer, false, run_test_expires_past); michael@0: }, null), null); michael@0: } michael@0: michael@0: function run_test_expires_past() michael@0: { michael@0: var chan = make_channel(randomURI2); michael@0: chan.asyncOpen(new ChannelListener(function(request, buffer) { michael@0: // Expires: -1 response should not be found in any cache. michael@0: check_response(randomURI2, request, buffer, true, finish_test); michael@0: }, null), null); michael@0: } michael@0: michael@0: function finish_test() michael@0: { michael@0: httpserver.stop(do_test_finished); 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(randomPath1, redirectHandler_NoStore); michael@0: httpserver.registerPathHandler(randomPath2, redirectHandler_ExpiresInPast); michael@0: httpserver.registerPathHandler("/content", contentHandler); michael@0: httpserver.start(-1); michael@0: michael@0: run_test_no_store(); michael@0: do_test_pending(); michael@0: }