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