Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 2 | |
michael@0 | 3 | /* |
michael@0 | 4 | - get 302 with Cache-control: no-store |
michael@0 | 5 | - check cache entry for the 302 response is cached only in memory device |
michael@0 | 6 | - get 302 with Expires: -1 |
michael@0 | 7 | - check cache entry for the 302 response is not cached at all |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | var httpserver = null; |
michael@0 | 11 | // Need to randomize, because apparently no one clears our cache |
michael@0 | 12 | var randomPath1 = "/redirect-no-store/" + Math.random(); |
michael@0 | 13 | |
michael@0 | 14 | XPCOMUtils.defineLazyGetter(this, "randomURI1", function() { |
michael@0 | 15 | return "http://localhost:" + httpserver.identity.primaryPort + randomPath1; |
michael@0 | 16 | }); |
michael@0 | 17 | |
michael@0 | 18 | var randomPath2 = "/redirect-expires-past/" + Math.random(); |
michael@0 | 19 | |
michael@0 | 20 | XPCOMUtils.defineLazyGetter(this, "randomURI2", function() { |
michael@0 | 21 | return "http://localhost:" + httpserver.identity.primaryPort + randomPath2; |
michael@0 | 22 | }); |
michael@0 | 23 | |
michael@0 | 24 | function make_channel(url, callback, ctx) { |
michael@0 | 25 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 26 | getService(Ci.nsIIOService); |
michael@0 | 27 | return ios.newChannel(url, "", null); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | const responseBody = "response body"; |
michael@0 | 31 | |
michael@0 | 32 | var redirectHandler_NoStore_calls = 0; |
michael@0 | 33 | function redirectHandler_NoStore(metadata, response) |
michael@0 | 34 | { |
michael@0 | 35 | response.setStatusLine(metadata.httpVersion, 302, "Found"); |
michael@0 | 36 | response.setHeader("Location", "http://localhost:" + |
michael@0 | 37 | httpserver.identity.primaryPort + "/content", false); |
michael@0 | 38 | response.setHeader("Cache-control", "no-store"); |
michael@0 | 39 | ++redirectHandler_NoStore_calls; |
michael@0 | 40 | return; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | var redirectHandler_ExpiresInPast_calls = 0; |
michael@0 | 44 | function redirectHandler_ExpiresInPast(metadata, response) |
michael@0 | 45 | { |
michael@0 | 46 | response.setStatusLine(metadata.httpVersion, 302, "Found"); |
michael@0 | 47 | response.setHeader("Location", "http://localhost:" + |
michael@0 | 48 | httpserver.identity.primaryPort + "/content", false); |
michael@0 | 49 | response.setHeader("Expires", "-1"); |
michael@0 | 50 | ++redirectHandler_ExpiresInPast_calls; |
michael@0 | 51 | return; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | function contentHandler(metadata, response) |
michael@0 | 55 | { |
michael@0 | 56 | response.setHeader("Content-Type", "text/plain"); |
michael@0 | 57 | response.bodyOutputStream.write(responseBody, responseBody.length); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | function check_response(path, request, buffer, expectedExpiration, continuation) |
michael@0 | 61 | { |
michael@0 | 62 | do_check_eq(buffer, responseBody); |
michael@0 | 63 | |
michael@0 | 64 | // Entry is always there, old cache wrapping code does session->SetDoomEntriesIfExpired(false), |
michael@0 | 65 | // just check it's not persisted or is expired (dep on the test). |
michael@0 | 66 | asyncOpenCacheEntry(path, "disk", Ci.nsICacheStorage.OPEN_READONLY, null, function(status, entry) { |
michael@0 | 67 | do_check_eq(status, 0); |
michael@0 | 68 | |
michael@0 | 69 | // Expired entry is on disk, no-store entry is in memory |
michael@0 | 70 | do_check_eq(entry.persistent, expectedExpiration); |
michael@0 | 71 | |
michael@0 | 72 | // Do the request again and check the server handler is called appropriately |
michael@0 | 73 | var chan = make_channel(path); |
michael@0 | 74 | chan.asyncOpen(new ChannelListener(function(request, buffer) { |
michael@0 | 75 | do_check_eq(buffer, responseBody); |
michael@0 | 76 | |
michael@0 | 77 | if (expectedExpiration) { |
michael@0 | 78 | // Handler had to be called second time |
michael@0 | 79 | do_check_eq(redirectHandler_ExpiresInPast_calls, 2); |
michael@0 | 80 | } |
michael@0 | 81 | else { |
michael@0 | 82 | // Handler had to be called second time (no-store forces validate), |
michael@0 | 83 | // and we are just in memory |
michael@0 | 84 | do_check_eq(redirectHandler_NoStore_calls, 2); |
michael@0 | 85 | do_check_true(!entry.persistent); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | continuation(); |
michael@0 | 89 | }, null), null); |
michael@0 | 90 | }); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | function run_test_no_store() |
michael@0 | 94 | { |
michael@0 | 95 | var chan = make_channel(randomURI1); |
michael@0 | 96 | chan.asyncOpen(new ChannelListener(function(request, buffer) { |
michael@0 | 97 | // Cache-control: no-store response should only be found in the memory cache. |
michael@0 | 98 | check_response(randomURI1, request, buffer, false, run_test_expires_past); |
michael@0 | 99 | }, null), null); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | function run_test_expires_past() |
michael@0 | 103 | { |
michael@0 | 104 | var chan = make_channel(randomURI2); |
michael@0 | 105 | chan.asyncOpen(new ChannelListener(function(request, buffer) { |
michael@0 | 106 | // Expires: -1 response should not be found in any cache. |
michael@0 | 107 | check_response(randomURI2, request, buffer, true, finish_test); |
michael@0 | 108 | }, null), null); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | function finish_test() |
michael@0 | 112 | { |
michael@0 | 113 | httpserver.stop(do_test_finished); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | function run_test() |
michael@0 | 117 | { |
michael@0 | 118 | do_get_profile(); |
michael@0 | 119 | |
michael@0 | 120 | httpserver = new HttpServer(); |
michael@0 | 121 | httpserver.registerPathHandler(randomPath1, redirectHandler_NoStore); |
michael@0 | 122 | httpserver.registerPathHandler(randomPath2, redirectHandler_ExpiresInPast); |
michael@0 | 123 | httpserver.registerPathHandler("/content", contentHandler); |
michael@0 | 124 | httpserver.start(-1); |
michael@0 | 125 | |
michael@0 | 126 | run_test_no_store(); |
michael@0 | 127 | do_test_pending(); |
michael@0 | 128 | } |