test_bug482601.js (6878B)
1 "use strict"; 2 3 const { HttpServer } = ChromeUtils.importESModule( 4 "resource://testing-common/httpd.sys.mjs" 5 ); 6 7 var httpserv = null; 8 var test_nr = 0; 9 var observers_called = ""; 10 var handlers_called = ""; 11 var buffer = ""; 12 13 var observer = { 14 QueryInterface: ChromeUtils.generateQI(["nsIObserver"]), 15 16 observe(subject, topic) { 17 if (observers_called.length) { 18 observers_called += ","; 19 } 20 21 observers_called += topic; 22 }, 23 }; 24 25 var listener = { 26 onStartRequest() { 27 buffer = ""; 28 }, 29 30 onDataAvailable(request, stream, offset, count) { 31 buffer = buffer.concat(read_stream(stream, count)); 32 }, 33 34 onStopRequest(request, status) { 35 Assert.equal(status, Cr.NS_OK); 36 Assert.equal(buffer, "0123456789"); 37 Assert.equal(observers_called, results[test_nr]); 38 test_nr++; 39 do_timeout(0, do_test); 40 }, 41 }; 42 43 function run_test() { 44 httpserv = new HttpServer(); 45 httpserv.registerPathHandler("/bug482601/nocache", bug482601_nocache); 46 httpserv.registerPathHandler("/bug482601/partial", bug482601_partial); 47 httpserv.registerPathHandler("/bug482601/cached", bug482601_cached); 48 httpserv.registerPathHandler( 49 "/bug482601/only_from_cache", 50 bug482601_only_from_cache 51 ); 52 httpserv.start(-1); 53 54 var obs = Cc["@mozilla.org/observer-service;1"].getService(); 55 obs = obs.QueryInterface(Ci.nsIObserverService); 56 obs.addObserver(observer, "http-on-examine-response"); 57 obs.addObserver(observer, "http-on-examine-merged-response"); 58 obs.addObserver(observer, "http-on-examine-cached-response"); 59 60 do_timeout(0, do_test); 61 do_test_pending(); 62 } 63 64 function do_test() { 65 if (test_nr < tests.length) { 66 tests[test_nr](); 67 } else { 68 Assert.equal(handlers_called, "nocache,partial,cached"); 69 httpserv.stop(do_test_finished); 70 } 71 } 72 73 var tests = [test_nocache, test_partial, test_cached, test_only_from_cache]; 74 75 var results = [ 76 "http-on-examine-response", 77 "http-on-examine-response,http-on-examine-merged-response", 78 "http-on-examine-response,http-on-examine-merged-response", 79 "http-on-examine-cached-response", 80 ]; 81 82 function makeChan(url) { 83 return NetUtil.newChannel({ 84 uri: url, 85 loadUsingSystemPrincipal: true, 86 }).QueryInterface(Ci.nsIHttpChannel); 87 } 88 89 function storeCache(aCacheEntry, aResponseHeads, aContent) { 90 aCacheEntry.setMetaDataElement("request-method", "GET"); 91 aCacheEntry.setMetaDataElement("response-head", aResponseHeads); 92 aCacheEntry.setMetaDataElement("charset", "ISO-8859-1"); 93 94 var oStream = aCacheEntry.openOutputStream(0, aContent.length); 95 var written = oStream.write(aContent, aContent.length); 96 if (written != aContent.length) { 97 do_throw( 98 "oStream.write has not written all data!\n" + 99 " Expected: " + 100 written + 101 "\n" + 102 " Actual: " + 103 aContent.length + 104 "\n" 105 ); 106 } 107 oStream.close(); 108 } 109 110 function test_nocache() { 111 observers_called = ""; 112 113 var chan = makeChan( 114 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/nocache" 115 ); 116 chan.asyncOpen(listener); 117 } 118 119 function test_partial() { 120 asyncOpenCacheEntry( 121 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/partial", 122 "disk", 123 Ci.nsICacheStorage.OPEN_NORMALLY, 124 null, 125 test_partial2 126 ); 127 } 128 129 function test_partial2(status, entry) { 130 Assert.equal(status, Cr.NS_OK); 131 storeCache( 132 entry, 133 "HTTP/1.1 200 OK\r\n" + 134 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" + 135 "Server: httpd.js\r\n" + 136 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" + 137 "Accept-Ranges: bytes\r\n" + 138 "Content-Length: 10\r\n" + 139 "Content-Type: text/plain\r\n", 140 "0123" 141 ); 142 143 observers_called = ""; 144 145 var chan = makeChan( 146 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/partial" 147 ); 148 chan.asyncOpen(listener); 149 } 150 151 function test_cached() { 152 asyncOpenCacheEntry( 153 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/cached", 154 "disk", 155 Ci.nsICacheStorage.OPEN_NORMALLY, 156 null, 157 test_cached2 158 ); 159 } 160 161 function test_cached2(status, entry) { 162 Assert.equal(status, Cr.NS_OK); 163 storeCache( 164 entry, 165 "HTTP/1.1 200 OK\r\n" + 166 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" + 167 "Server: httpd.js\r\n" + 168 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" + 169 "Accept-Ranges: bytes\r\n" + 170 "Content-Length: 10\r\n" + 171 "Content-Type: text/plain\r\n", 172 "0123456789" 173 ); 174 175 observers_called = ""; 176 177 var chan = makeChan( 178 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/cached" 179 ); 180 chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS; 181 chan.asyncOpen(listener); 182 } 183 184 function test_only_from_cache() { 185 asyncOpenCacheEntry( 186 "http://localhost:" + 187 httpserv.identity.primaryPort + 188 "/bug482601/only_from_cache", 189 "disk", 190 Ci.nsICacheStorage.OPEN_NORMALLY, 191 null, 192 test_only_from_cache2 193 ); 194 } 195 196 function test_only_from_cache2(status, entry) { 197 Assert.equal(status, Cr.NS_OK); 198 storeCache( 199 entry, 200 "HTTP/1.1 200 OK\r\n" + 201 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" + 202 "Server: httpd.js\r\n" + 203 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" + 204 "Accept-Ranges: bytes\r\n" + 205 "Content-Length: 10\r\n" + 206 "Content-Type: text/plain\r\n", 207 "0123456789" 208 ); 209 210 observers_called = ""; 211 212 var chan = makeChan( 213 "http://localhost:" + 214 httpserv.identity.primaryPort + 215 "/bug482601/only_from_cache" 216 ); 217 chan.loadFlags = Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE; 218 chan.asyncOpen(listener); 219 } 220 221 // PATHS 222 223 // /bug482601/nocache 224 function bug482601_nocache(metadata, response) { 225 response.setHeader("Content-Type", "text/plain", false); 226 var body = "0123456789"; 227 response.bodyOutputStream.write(body, body.length); 228 handlers_called += "nocache"; 229 } 230 231 // /bug482601/partial 232 function bug482601_partial(metadata, response) { 233 Assert.ok(metadata.hasHeader("If-Range")); 234 Assert.equal(metadata.getHeader("If-Range"), "Thu, 1 Jan 2009 00:00:00 GMT"); 235 Assert.ok(metadata.hasHeader("Range")); 236 Assert.equal(metadata.getHeader("Range"), "bytes=4-"); 237 238 response.setStatusLine(metadata.httpVersion, 206, "Partial Content"); 239 response.setHeader("Content-Range", "bytes 4-9/10", false); 240 response.setHeader("Content-Type", "text/plain", false); 241 response.setHeader("Last-Modified", "Thu, 1 Jan 2009 00:00:00 GMT"); 242 243 var body = "456789"; 244 response.bodyOutputStream.write(body, body.length); 245 handlers_called += ",partial"; 246 } 247 248 // /bug482601/cached 249 function bug482601_cached(metadata, response) { 250 Assert.ok(metadata.hasHeader("If-Modified-Since")); 251 Assert.equal( 252 metadata.getHeader("If-Modified-Since"), 253 "Thu, 1 Jan 2009 00:00:00 GMT" 254 ); 255 256 response.setStatusLine(metadata.httpVersion, 304, "Not Modified"); 257 handlers_called += ",cached"; 258 } 259 260 // /bug482601/only_from_cache 261 function bug482601_only_from_cache() { 262 do_throw("This should not be reached"); 263 }