1 Cu.import("resource://testing-common/httpd.js");
5 var observers_called = "";
6 var handlers_called = "";
10 QueryInterface: function (aIID) {
11 if (aIID.equals(Ci.nsISupports) ||
12 aIID.equals(Ci.nsIObserver))
14 throw Cr.NS_ERROR_NO_INTERFACE;
17 observe: function(subject, topic, data) {
18 if (observers_called.length)
19 observers_called += ",";
21 observers_called += topic;
26 onStartRequest: function (request, ctx) {
30 onDataAvailable: function (request, ctx, stream, offset, count) {
31 buffer = buffer.concat(read_stream(stream, count));
34 onStopRequest: function (request, ctx, status) {
35 do_check_eq(status, Cr.NS_OK);
36 do_check_eq(buffer, "0123456789");
37 do_check_eq(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("/bug482601/only_from_cache", bug482601_only_from_cache);
51 var obs = Cc["@mozilla.org/observer-service;1"].getService();
52 obs = obs.QueryInterface(Ci.nsIObserverService);
53 obs.addObserver(observer, "http-on-examine-response", false);
54 obs.addObserver(observer, "http-on-examine-merged-response", false);
55 obs.addObserver(observer, "http-on-examine-cached-response", false);
57 do_timeout(0, do_test);
62 if (test_nr < tests.length) {
66 do_check_eq(handlers_called, "nocache,partial,cached");
67 httpserv.stop(do_test_finished);
71 var tests = [test_nocache,
74 test_only_from_cache];
76 var results = ["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"];
81 function makeChan(url) {
82 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
83 var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
87 function storeCache(aCacheEntry, aResponseHeads, aContent) {
88 aCacheEntry.setMetaDataElement("request-method", "GET");
89 aCacheEntry.setMetaDataElement("response-head", aResponseHeads);
90 aCacheEntry.setMetaDataElement("charset", "ISO-8859-1");
92 var oStream = aCacheEntry.openOutputStream(0);
93 var written = oStream.write(aContent, aContent.length);
94 if (written != aContent.length) {
95 do_throw("oStream.write has not written all data!\n" +
96 " Expected: " + written + "\n" +
97 " Actual: " + aContent.length + "\n");
103 function test_nocache() {
104 observers_called = "";
106 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
107 "/bug482601/nocache");
108 chan.asyncOpen(listener, null);
111 function test_partial() {
112 asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
113 "/bug482601/partial",
114 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
118 function test_partial2(status, entry) {
119 do_check_eq(status, Cr.NS_OK);
121 "HTTP/1.1 200 OK\r\n" +
122 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
123 "Server: httpd.js\r\n" +
124 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
125 "Accept-Ranges: bytes\r\n" +
126 "Content-Length: 10\r\n" +
127 "Content-Type: text/plain\r\n",
130 observers_called = "";
132 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
133 "/bug482601/partial");
134 chan.asyncOpen(listener, null);
137 function test_cached() {
138 asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
140 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
144 function test_cached2(status, entry) {
145 do_check_eq(status, Cr.NS_OK);
147 "HTTP/1.1 200 OK\r\n" +
148 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
149 "Server: httpd.js\r\n" +
150 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
151 "Accept-Ranges: bytes\r\n" +
152 "Content-Length: 10\r\n" +
153 "Content-Type: text/plain\r\n",
156 observers_called = "";
158 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
159 "/bug482601/cached");
160 chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
161 chan.asyncOpen(listener, null);
164 function test_only_from_cache() {
165 asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
166 "/bug482601/only_from_cache",
167 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
168 test_only_from_cache2);
171 function test_only_from_cache2(status, entry) {
172 do_check_eq(status, Cr.NS_OK);
174 "HTTP/1.1 200 OK\r\n" +
175 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
176 "Server: httpd.js\r\n" +
177 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
178 "Accept-Ranges: bytes\r\n" +
179 "Content-Length: 10\r\n" +
180 "Content-Type: text/plain\r\n",
183 observers_called = "";
185 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
186 "/bug482601/only_from_cache");
187 chan.loadFlags = Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE;
188 chan.asyncOpen(listener, null);
194 // /bug482601/nocache
195 function bug482601_nocache(metadata, response) {
196 response.setHeader("Content-Type", "text/plain", false);
197 var body = "0123456789";
198 response.bodyOutputStream.write(body, body.length);
199 handlers_called += "nocache";
202 // /bug482601/partial
203 function bug482601_partial(metadata, response) {
204 do_check_true(metadata.hasHeader("If-Range"));
205 do_check_eq(metadata.getHeader("If-Range"),
206 "Thu, 1 Jan 2009 00:00:00 GMT");
207 do_check_true(metadata.hasHeader("Range"));
208 do_check_eq(metadata.getHeader("Range"), "bytes=4-");
210 response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
211 response.setHeader("Content-Range", "bytes 4-9/10", false);
212 response.setHeader("Content-Type", "text/plain", false);
213 response.setHeader("Last-Modified", "Thu, 1 Jan 2009 00:00:00 GMT");
216 response.bodyOutputStream.write(body, body.length);
217 handlers_called += ",partial";
221 function bug482601_cached(metadata, response) {
222 do_check_true(metadata.hasHeader("If-Modified-Since"));
223 do_check_eq(metadata.getHeader("If-Modified-Since"),
224 "Thu, 1 Jan 2009 00:00:00 GMT");
226 response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
227 handlers_called += ",cached";
230 // /bug482601/only_from_cache
231 function bug482601_only_from_cache(metadata, response) {
232 do_throw("This should not be reached");