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
) {
17 if (observers_called
.length
) {
18 observers_called
+= ",";
21 observers_called
+= topic
;
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" +
110 function test_nocache() {
111 observers_called
= "";
114 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/bug482601/nocache"
116 chan
.asyncOpen(listener
);
119 function test_partial() {
121 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/bug482601/partial",
123 Ci
.nsICacheStorage
.OPEN_NORMALLY
,
129 function test_partial2(status
, entry
) {
130 Assert
.equal(status
, Cr
.NS_OK
);
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",
143 observers_called
= "";
146 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/bug482601/partial"
148 chan
.asyncOpen(listener
);
151 function test_cached() {
153 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/bug482601/cached",
155 Ci
.nsICacheStorage
.OPEN_NORMALLY
,
161 function test_cached2(status
, entry
) {
162 Assert
.equal(status
, Cr
.NS_OK
);
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",
175 observers_called
= "";
178 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/bug482601/cached"
180 chan
.loadFlags
= Ci
.nsIRequest
.VALIDATE_ALWAYS
;
181 chan
.asyncOpen(listener
);
184 function test_only_from_cache() {
186 "http://localhost:" +
187 httpserv
.identity
.primaryPort
+
188 "/bug482601/only_from_cache",
190 Ci
.nsICacheStorage
.OPEN_NORMALLY
,
192 test_only_from_cache2
196 function test_only_from_cache2(status
, entry
) {
197 Assert
.equal(status
, Cr
.NS_OK
);
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",
210 observers_called
= "";
213 "http://localhost:" +
214 httpserv
.identity
.primaryPort
+
215 "/bug482601/only_from_cache"
217 chan
.loadFlags
= Ci
.nsICachingChannel
.LOAD_ONLY_FROM_CACHE
;
218 chan
.asyncOpen(listener
);
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";
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-");
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");
244 response
.bodyOutputStream
.write(body
, body
.length
);
245 handlers_called
+= ",partial";
249 function bug482601_cached(metadata
, response
) {
250 Assert
.ok(metadata
.hasHeader("If-Modified-Since"));
252 metadata
.getHeader("If-Modified-Since"),
253 "Thu, 1 Jan 2009 00:00:00 GMT"
256 response
.setStatusLine(metadata
.httpVersion
, 304, "Not Modified");
257 handlers_called
+= ",cached";
260 // /bug482601/only_from_cache
261 function bug482601_only_from_cache() {
262 do_throw("This should not be reached");