1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // This file tests async handling of a channel suspended in
6 // notifying http-on-examine-merged-response observers.
7 // Note that this test is developed based on test_bug482601.js.
10 const { HttpServer
} = ChromeUtils
.importESModule(
11 "resource://testing-common/httpd.sys.mjs"
17 var observerCalled
= false;
18 var channelResumed
= false;
21 QueryInterface
: ChromeUtils
.generateQI(["nsIObserver"]),
23 observe(subject
, topic
) {
25 topic
=== "http-on-examine-merged-response" &&
26 subject
instanceof Ci
.nsIHttpChannel
28 var chan
= subject
.QueryInterface(Ci
.nsIHttpChannel
);
30 Assert
.equal(channelResumed
, false);
31 channelResumed
= true;
34 Assert
.equal(observerCalled
, false);
35 observerCalled
= true;
46 onDataAvailable(request
, stream
, offset
, count
) {
47 buffer
= buffer
.concat(read_stream(stream
, count
));
50 onStopRequest(request
, status
) {
51 Assert
.equal(status
, Cr
.NS_OK
);
52 Assert
.equal(buffer
, "0123456789");
53 Assert
.equal(channelResumed
, true);
54 Assert
.equal(observerCalled
, true);
56 do_timeout(0, do_test
);
61 httpserv
= new HttpServer();
62 httpserv
.registerPathHandler("/path/partial", path_partial
);
63 httpserv
.registerPathHandler("/path/cached", path_cached
);
66 Services
.obs
.addObserver(observer
, "http-on-examine-merged-response");
68 do_timeout(0, do_test
);
73 if (test_nr
< tests
.length
) {
76 Services
.obs
.removeObserver(observer
, "http-on-examine-merged-response");
77 httpserv
.stop(do_test_finished
);
81 var tests
= [test_partial
, test_cached
];
83 function makeChan(url
) {
84 return NetUtil
.newChannel({
86 loadUsingSystemPrincipal
: true,
87 }).QueryInterface(Ci
.nsIHttpChannel
);
90 function storeCache(aCacheEntry
, aResponseHeads
, aContent
) {
91 aCacheEntry
.setMetaDataElement("request-method", "GET");
92 aCacheEntry
.setMetaDataElement("response-head", aResponseHeads
);
93 aCacheEntry
.setMetaDataElement("charset", "ISO-8859-1");
95 var oStream
= aCacheEntry
.openOutputStream(0, aContent
.length
);
96 var written
= oStream
.write(aContent
, aContent
.length
);
97 if (written
!= aContent
.length
) {
99 "oStream.write has not written all data!\n" +
111 function test_partial() {
112 observerCalled
= false;
113 channelResumed
= false;
115 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/path/partial",
117 Ci
.nsICacheStorage
.OPEN_NORMALLY
,
123 function test_partial2(status
, entry
) {
124 Assert
.equal(status
, Cr
.NS_OK
);
127 "HTTP/1.1 200 OK\r\n" +
128 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
129 "Server: httpd.js\r\n" +
130 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
131 "Accept-Ranges: bytes\r\n" +
132 "Content-Length: 10\r\n" +
133 "Content-Type: text/plain\r\n",
137 observerCalled
= false;
140 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/path/partial"
142 chan
.asyncOpen(listener
);
145 function test_cached() {
146 observerCalled
= false;
147 channelResumed
= false;
149 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/path/cached",
151 Ci
.nsICacheStorage
.OPEN_NORMALLY
,
157 function test_cached2(status
, entry
) {
158 Assert
.equal(status
, Cr
.NS_OK
);
161 "HTTP/1.1 200 OK\r\n" +
162 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
163 "Server: httpd.js\r\n" +
164 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
165 "Accept-Ranges: bytes\r\n" +
166 "Content-Length: 10\r\n" +
167 "Content-Type: text/plain\r\n",
171 observerCalled
= false;
174 "http://localhost:" + httpserv
.identity
.primaryPort
+ "/path/cached"
176 chan
.loadFlags
= Ci
.nsIRequest
.VALIDATE_ALWAYS
;
177 chan
.asyncOpen(listener
);
183 function path_partial(metadata
, response
) {
184 Assert
.ok(metadata
.hasHeader("If-Range"));
185 Assert
.equal(metadata
.getHeader("If-Range"), "Thu, 1 Jan 2009 00:00:00 GMT");
186 Assert
.ok(metadata
.hasHeader("Range"));
187 Assert
.equal(metadata
.getHeader("Range"), "bytes=4-");
189 response
.setStatusLine(metadata
.httpVersion
, 206, "Partial Content");
190 response
.setHeader("Content-Range", "bytes 4-9/10", false);
191 response
.setHeader("Content-Type", "text/plain", false);
192 response
.setHeader("Last-Modified", "Thu, 1 Jan 2009 00:00:00 GMT");
195 response
.bodyOutputStream
.write(body
, body
.length
);
199 function path_cached(metadata
, response
) {
200 Assert
.ok(metadata
.hasHeader("If-Modified-Since"));
202 metadata
.getHeader("If-Modified-Since"),
203 "Thu, 1 Jan 2009 00:00:00 GMT"
206 response
.setStatusLine(metadata
.httpVersion
, 304, "Not Modified");