Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 2 | |
michael@0 | 3 | const VALUE_HDR_NAME = "X-HTTP-VALUE-HEADER"; |
michael@0 | 4 | const VARY_HDR_NAME = "X-HTTP-VARY-HEADER"; |
michael@0 | 5 | const CACHECTRL_HDR_NAME = "X-CACHE-CONTROL-HEADER"; |
michael@0 | 6 | |
michael@0 | 7 | var httpserver = null; |
michael@0 | 8 | |
michael@0 | 9 | function make_channel(flags, vary, value) { |
michael@0 | 10 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 11 | getService(Ci.nsIIOService); |
michael@0 | 12 | var chan = ios.newChannel("http://localhost:" + |
michael@0 | 13 | httpserver.identity.primaryPort + |
michael@0 | 14 | "/bug633743", null, null); |
michael@0 | 15 | return chan.QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | function Test(flags, varyHdr, sendValue, expectValue, cacheHdr) { |
michael@0 | 19 | this._flags = flags; |
michael@0 | 20 | this._varyHdr = varyHdr; |
michael@0 | 21 | this._sendVal = sendValue; |
michael@0 | 22 | this._expectVal = expectValue; |
michael@0 | 23 | this._cacheHdr = cacheHdr; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | Test.prototype = { |
michael@0 | 27 | _buffer: "", |
michael@0 | 28 | _flags: null, |
michael@0 | 29 | _varyHdr: null, |
michael@0 | 30 | _sendVal: null, |
michael@0 | 31 | _expectVal: null, |
michael@0 | 32 | _cacheHdr: null, |
michael@0 | 33 | |
michael@0 | 34 | QueryInterface: function(iid) { |
michael@0 | 35 | if (iid.equals(Ci.nsIStreamListener) || |
michael@0 | 36 | iid.equals(Ci.nsIRequestObserver) || |
michael@0 | 37 | iid.equals(Ci.nsISupports)) |
michael@0 | 38 | return this; |
michael@0 | 39 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 40 | }, |
michael@0 | 41 | |
michael@0 | 42 | onStartRequest: function(request, context) { }, |
michael@0 | 43 | |
michael@0 | 44 | onDataAvailable: function(request, context, stream, offset, count) { |
michael@0 | 45 | this._buffer = this._buffer.concat(read_stream(stream, count)); |
michael@0 | 46 | }, |
michael@0 | 47 | |
michael@0 | 48 | onStopRequest: function(request, context, status) { |
michael@0 | 49 | do_check_eq(this._buffer, this._expectVal); |
michael@0 | 50 | do_timeout(0, run_next_test); |
michael@0 | 51 | }, |
michael@0 | 52 | |
michael@0 | 53 | run: function() { |
michael@0 | 54 | var channel = make_channel(); |
michael@0 | 55 | channel.loadFlags = this._flags; |
michael@0 | 56 | channel.setRequestHeader(VALUE_HDR_NAME, this._sendVal, false); |
michael@0 | 57 | channel.setRequestHeader(VARY_HDR_NAME, this._varyHdr, false); |
michael@0 | 58 | if (this._cacheHdr) |
michael@0 | 59 | channel.setRequestHeader(CACHECTRL_HDR_NAME, this._cacheHdr, false); |
michael@0 | 60 | |
michael@0 | 61 | channel.asyncOpen(this, null); |
michael@0 | 62 | } |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | var gTests = [ |
michael@0 | 66 | // Test LOAD_FROM_CACHE: Load cache-entry |
michael@0 | 67 | new Test(Ci.nsIRequest.LOAD_NORMAL, |
michael@0 | 68 | "entity-initial", // hdr-value used to vary |
michael@0 | 69 | "request1", // echoed by handler |
michael@0 | 70 | "request1" // value expected to receive in channel |
michael@0 | 71 | ), |
michael@0 | 72 | // Verify that it was cached |
michael@0 | 73 | new Test(Ci.nsIRequest.LOAD_NORMAL, |
michael@0 | 74 | "entity-initial", // hdr-value used to vary |
michael@0 | 75 | "fresh value with LOAD_NORMAL", // echoed by handler |
michael@0 | 76 | "request1" // value expected to receive in channel |
michael@0 | 77 | ), |
michael@0 | 78 | // Load same entity with LOAD_FROM_CACHE-flag |
michael@0 | 79 | new Test(Ci.nsIRequest.LOAD_FROM_CACHE, |
michael@0 | 80 | "entity-initial", // hdr-value used to vary |
michael@0 | 81 | "fresh value with LOAD_FROM_CACHE", // echoed by handler |
michael@0 | 82 | "request1" // value expected to receive in channel |
michael@0 | 83 | ), |
michael@0 | 84 | // Load different entity with LOAD_FROM_CACHE-flag |
michael@0 | 85 | new Test(Ci.nsIRequest.LOAD_FROM_CACHE, |
michael@0 | 86 | "entity-l-f-c", // hdr-value used to vary |
michael@0 | 87 | "request2", // echoed by handler |
michael@0 | 88 | "request2" // value expected to receive in channel |
michael@0 | 89 | ), |
michael@0 | 90 | // Verify that new value was cached |
michael@0 | 91 | new Test(Ci.nsIRequest.LOAD_NORMAL, |
michael@0 | 92 | "entity-l-f-c", // hdr-value used to vary |
michael@0 | 93 | "fresh value with LOAD_NORMAL", // echoed by handler |
michael@0 | 94 | "request2" // value expected to receive in channel |
michael@0 | 95 | ), |
michael@0 | 96 | |
michael@0 | 97 | // Test VALIDATE_NEVER: Note previous cache-entry |
michael@0 | 98 | new Test(Ci.nsIRequest.VALIDATE_NEVER, |
michael@0 | 99 | "entity-v-n", // hdr-value used to vary |
michael@0 | 100 | "request3", // echoed by handler |
michael@0 | 101 | "request3" // value expected to receive in channel |
michael@0 | 102 | ), |
michael@0 | 103 | // Verify that cache-entry was replaced |
michael@0 | 104 | new Test(Ci.nsIRequest.LOAD_NORMAL, |
michael@0 | 105 | "entity-v-n", // hdr-value used to vary |
michael@0 | 106 | "fresh value with LOAD_NORMAL", // echoed by handler |
michael@0 | 107 | "request3" // value expected to receive in channel |
michael@0 | 108 | ), |
michael@0 | 109 | |
michael@0 | 110 | // Test combination VALIDATE_NEVER && no-store: Load new cache-entry |
michael@0 | 111 | new Test(Ci.nsIRequest.LOAD_NORMAL, |
michael@0 | 112 | "entity-2",// hdr-value used to vary |
michael@0 | 113 | "request4", // echoed by handler |
michael@0 | 114 | "request4", // value expected to receive in channel |
michael@0 | 115 | "no-store" // set no-store on response |
michael@0 | 116 | ), |
michael@0 | 117 | // Ensure we validate without IMS header in this case (verified in handler) |
michael@0 | 118 | new Test(Ci.nsIRequest.VALIDATE_NEVER, |
michael@0 | 119 | "entity-2-v-n",// hdr-value used to vary |
michael@0 | 120 | "request5", // echoed by handler |
michael@0 | 121 | "request5" // value expected to receive in channel |
michael@0 | 122 | ), |
michael@0 | 123 | |
michael@0 | 124 | // Test VALIDATE-ALWAYS: Load new entity |
michael@0 | 125 | new Test(Ci.nsIRequest.LOAD_NORMAL, |
michael@0 | 126 | "entity-3",// hdr-value used to vary |
michael@0 | 127 | "request6", // echoed by handler |
michael@0 | 128 | "request6", // value expected to receive in channel |
michael@0 | 129 | "no-cache" // set no-cache on response |
michael@0 | 130 | ), |
michael@0 | 131 | // Ensure we don't send IMS header also in this case (verified in handler) |
michael@0 | 132 | new Test(Ci.nsIRequest.VALIDATE_ALWAYS, |
michael@0 | 133 | "entity-3-v-a",// hdr-value used to vary |
michael@0 | 134 | "request7", // echoed by handler |
michael@0 | 135 | "request7" // value expected to receive in channel |
michael@0 | 136 | ), |
michael@0 | 137 | ]; |
michael@0 | 138 | |
michael@0 | 139 | function run_next_test() |
michael@0 | 140 | { |
michael@0 | 141 | if (gTests.length == 0) { |
michael@0 | 142 | httpserver.stop(do_test_finished); |
michael@0 | 143 | return; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | var test = gTests.shift(); |
michael@0 | 147 | test.run(); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | function handler(metadata, response) { |
michael@0 | 151 | |
michael@0 | 152 | // None of the tests above should send an IMS |
michael@0 | 153 | do_check_false(metadata.hasHeader("If-Modified-Since")); |
michael@0 | 154 | |
michael@0 | 155 | // Pick up requested value to echo |
michael@0 | 156 | var hdr = "default value"; |
michael@0 | 157 | try { |
michael@0 | 158 | hdr = metadata.getHeader(VALUE_HDR_NAME); |
michael@0 | 159 | } catch(ex) { } |
michael@0 | 160 | |
michael@0 | 161 | // Pick up requested cache-control header-value |
michael@0 | 162 | var cctrlVal = "max-age=10000"; |
michael@0 | 163 | try { |
michael@0 | 164 | cctrlVal = metadata.getHeader(CACHECTRL_HDR_NAME); |
michael@0 | 165 | } catch(ex) { } |
michael@0 | 166 | |
michael@0 | 167 | response.setStatusLine(metadata.httpVersion, 200, "OK"); |
michael@0 | 168 | response.setHeader("Content-Type", "text/plain", false); |
michael@0 | 169 | response.setHeader("Cache-Control", cctrlVal, false); |
michael@0 | 170 | response.setHeader("Vary", VARY_HDR_NAME, false); |
michael@0 | 171 | response.setHeader("Last-Modified", "Tue, 15 Nov 1994 12:45:26 GMT", false); |
michael@0 | 172 | response.bodyOutputStream.write(hdr, hdr.length); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | function run_test() { |
michael@0 | 176 | |
michael@0 | 177 | // clear the cache |
michael@0 | 178 | evict_cache_entries(); |
michael@0 | 179 | |
michael@0 | 180 | httpserver = new HttpServer(); |
michael@0 | 181 | httpserver.registerPathHandler("/bug633743", handler); |
michael@0 | 182 | httpserver.start(-1); |
michael@0 | 183 | |
michael@0 | 184 | run_next_test(); |
michael@0 | 185 | do_test_pending(); |
michael@0 | 186 | } |