Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
1 Cu.import("resource://testing-common/httpd.js");
3 var httpserv = null;
4 var test_nr = 0;
5 var observers_called = "";
6 var handlers_called = "";
7 var buffer = "";
9 var observer = {
10 QueryInterface: function (aIID) {
11 if (aIID.equals(Ci.nsISupports) ||
12 aIID.equals(Ci.nsIObserver))
13 return this;
14 throw Cr.NS_ERROR_NO_INTERFACE;
15 },
17 observe: function(subject, topic, data) {
18 if (observers_called.length)
19 observers_called += ",";
21 observers_called += topic;
22 }
23 };
25 var listener = {
26 onStartRequest: function (request, ctx) {
27 buffer = "";
28 },
30 onDataAvailable: function (request, ctx, stream, offset, count) {
31 buffer = buffer.concat(read_stream(stream, count));
32 },
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]);
38 test_nr++;
39 do_timeout(0, do_test);
40 }
41 };
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("/bug482601/only_from_cache", bug482601_only_from_cache);
49 httpserv.start(-1);
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);
58 do_test_pending();
59 }
61 function do_test() {
62 if (test_nr < tests.length) {
63 tests[test_nr]();
64 }
65 else {
66 do_check_eq(handlers_called, "nocache,partial,cached");
67 httpserv.stop(do_test_finished);
68 }
69 }
71 var tests = [test_nocache,
72 test_partial,
73 test_cached,
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);
84 return chan;
85 }
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");
98 }
99 oStream.close();
100 aCacheEntry.close();
101 }
103 function test_nocache() {
104 observers_called = "";
106 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
107 "/bug482601/nocache");
108 chan.asyncOpen(listener, null);
109 }
111 function test_partial() {
112 asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
113 "/bug482601/partial",
114 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
115 test_partial2);
116 }
118 function test_partial2(status, entry) {
119 do_check_eq(status, Cr.NS_OK);
120 storeCache(entry,
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",
128 "0123");
130 observers_called = "";
132 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
133 "/bug482601/partial");
134 chan.asyncOpen(listener, null);
135 }
137 function test_cached() {
138 asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
139 "/bug482601/cached",
140 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
141 test_cached2);
142 }
144 function test_cached2(status, entry) {
145 do_check_eq(status, Cr.NS_OK);
146 storeCache(entry,
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",
154 "0123456789");
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);
162 }
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);
169 }
171 function test_only_from_cache2(status, entry) {
172 do_check_eq(status, Cr.NS_OK);
173 storeCache(entry,
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",
181 "0123456789");
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);
189 }
192 // PATHS
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";
200 }
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");
215 var body = "456789";
216 response.bodyOutputStream.write(body, body.length);
217 handlers_called += ",partial";
218 }
220 // /bug482601/cached
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";
228 }
230 // /bug482601/only_from_cache
231 function bug482601_only_from_cache(metadata, response) {
232 do_throw("This should not be reached");
233 }