3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
9 var observers_called = "";
10 var handlers_called = "";
14 QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
16 observe(subject, topic, data) {
17 if (observers_called.length) {
18 observers_called += ",";
21 observers_called += topic;
26 onStartRequest(request) {
30 onDataAvailable(request, stream, offset, count) {
31 buffer = buffer.concat(read_stream(stream, count));
34 onStopRequest(request, status) {
35 Assert.equal(status, Cr.NS_OK);
36 Assert.equal(buffer, "0123456789");
37 Assert.equal(observers_called, results[test_nr]);
39 do_timeout(0, do_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
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");
60 do_timeout(0, do_test);
65 if (test_nr < tests.length) {
68 Assert.equal(handlers_called, "nocache,partial,cached");
69 httpserv.stop(do_test_finished);
73 var tests = [test_nocache, test_partial, test_cached, test_only_from_cache];
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",
82 function makeChan(url) {
83 return NetUtil.newChannel({
85 loadUsingSystemPrincipal: true,
86 }).QueryInterface(Ci.nsIHttpChannel);
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");
94 var oStream = aCacheEntry.openOutputStream(0, aContent.length);
95 var written = oStream.write(aContent, aContent.length);
96 if (written != aContent.length) {
98 "oStream.write has not written all data!\n" +
111 function test_nocache() {
112 observers_called = "";
115 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/nocache"
117 chan.asyncOpen(listener);
120 function test_partial() {
122 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/partial",
124 Ci.nsICacheStorage.OPEN_NORMALLY,
130 function test_partial2(status, entry) {
131 Assert.equal(status, Cr.NS_OK);
134 "HTTP/1.1 200 OK\r\n" +
135 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
136 "Server: httpd.js\r\n" +
137 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
138 "Accept-Ranges: bytes\r\n" +
139 "Content-Length: 10\r\n" +
140 "Content-Type: text/plain\r\n",
144 observers_called = "";
147 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/partial"
149 chan.asyncOpen(listener);
152 function test_cached() {
154 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/cached",
156 Ci.nsICacheStorage.OPEN_NORMALLY,
162 function test_cached2(status, entry) {
163 Assert.equal(status, Cr.NS_OK);
166 "HTTP/1.1 200 OK\r\n" +
167 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
168 "Server: httpd.js\r\n" +
169 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
170 "Accept-Ranges: bytes\r\n" +
171 "Content-Length: 10\r\n" +
172 "Content-Type: text/plain\r\n",
176 observers_called = "";
179 "http://localhost:" + httpserv.identity.primaryPort + "/bug482601/cached"
181 chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
182 chan.asyncOpen(listener);
185 function test_only_from_cache() {
187 "http://localhost:" +
188 httpserv.identity.primaryPort +
189 "/bug482601/only_from_cache",
191 Ci.nsICacheStorage.OPEN_NORMALLY,
193 test_only_from_cache2
197 function test_only_from_cache2(status, entry) {
198 Assert.equal(status, Cr.NS_OK);
201 "HTTP/1.1 200 OK\r\n" +
202 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
203 "Server: httpd.js\r\n" +
204 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
205 "Accept-Ranges: bytes\r\n" +
206 "Content-Length: 10\r\n" +
207 "Content-Type: text/plain\r\n",
211 observers_called = "";
214 "http://localhost:" +
215 httpserv.identity.primaryPort +
216 "/bug482601/only_from_cache"
218 chan.loadFlags = Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE;
219 chan.asyncOpen(listener);
224 // /bug482601/nocache
225 function bug482601_nocache(metadata, response) {
226 response.setHeader("Content-Type", "text/plain", false);
227 var body = "0123456789";
228 response.bodyOutputStream.write(body, body.length);
229 handlers_called += "nocache";
232 // /bug482601/partial
233 function bug482601_partial(metadata, response) {
234 Assert.ok(metadata.hasHeader("If-Range"));
235 Assert.equal(metadata.getHeader("If-Range"), "Thu, 1 Jan 2009 00:00:00 GMT");
236 Assert.ok(metadata.hasHeader("Range"));
237 Assert.equal(metadata.getHeader("Range"), "bytes=4-");
239 response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
240 response.setHeader("Content-Range", "bytes 4-9/10", false);
241 response.setHeader("Content-Type", "text/plain", false);
242 response.setHeader("Last-Modified", "Thu, 1 Jan 2009 00:00:00 GMT");
245 response.bodyOutputStream.write(body, body.length);
246 handlers_called += ",partial";
250 function bug482601_cached(metadata, response) {
251 Assert.ok(metadata.hasHeader("If-Modified-Since"));
253 metadata.getHeader("If-Modified-Since"),
254 "Thu, 1 Jan 2009 00:00:00 GMT"
257 response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
258 handlers_called += ",cached";
261 // /bug482601/only_from_cache
262 function bug482601_only_from_cache(metadata, response) {
263 do_throw("This should not be reached");