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/. */
7 function makeChan(url
) {
8 return NetUtil
.newChannel({
10 loadUsingSystemPrincipal
: true,
11 }).QueryInterface(Ci
.nsIHttpChannel
);
14 const { HttpServer
} = ChromeUtils
.importESModule(
15 "resource://testing-common/httpd.sys.mjs"
18 function request_handler(metadata
, response
) {
19 response
.processAsync();
20 do_timeout(500, () => {
21 const body
= "some body once told me...";
22 response
.setStatusLine(metadata
.httpVersion
, 200, "Ok");
23 response
.setHeader("Content-Type", "text/plain", false);
24 response
.setHeader("Content-Length", "" + body
.length
, false);
25 response
.bodyOutputStream
.write(body
, body
.length
);
30 // This test checks that when canceling a loadgroup by the time the loadgroup's
31 // groupObserver is sent OnStopRequest for a request, that request has been
33 add_task(async
function test_cancelledInOnStop() {
34 let http_server
= new HttpServer();
35 http_server
.registerPathHandler("/test1", request_handler
);
36 http_server
.registerPathHandler("/test2", request_handler
);
37 http_server
.registerPathHandler("/test3", request_handler
);
38 http_server
.start(-1);
39 const port
= http_server
.identity
.primaryPort
;
41 let loadGroup
= Cc
["@mozilla.org/network/load-group;1"].createInstance(
46 onStartRequest
: () => {
47 info("onStartRequest");
49 onStopRequest
: (aRequest
, aStatusCode
) => {
53 "aStatusCode must be the cancellation code"
58 "aRequest.status must be the cancellation code"
61 QueryInterface
: ChromeUtils
.generateQI([
63 "nsISupportsWeakReference",
66 loadGroup
.groupObserver
= loadListener
;
68 let chan1
= makeChan(`http://localhost:${port}/test1`);
69 chan1
.loadGroup
= loadGroup
;
70 let chan2
= makeChan(`http://localhost:${port}/test2`);
71 chan2
.loadGroup
= loadGroup
;
72 let chan3
= makeChan(`http://localhost:${port}/test3`);
73 chan3
.loadGroup
= loadGroup
;
75 await
new Promise(resolve
=> do_timeout(500, resolve
));
78 new Promise(resolve
=> {
79 chan1
.asyncOpen(new ChannelListener(resolve
, null, CL_EXPECT_FAILURE
));
81 new Promise(resolve
=> {
82 chan2
.asyncOpen(new ChannelListener(resolve
, null, CL_EXPECT_FAILURE
));
84 new Promise(resolve
=> {
85 chan3
.asyncOpen(new ChannelListener(resolve
, null, CL_EXPECT_FAILURE
));
89 loadGroup
.cancel(Cr
.NS_ERROR_ABORT
);
91 await Promise
.all(promises
);
93 await
new Promise(resolve
=> {
94 http_server
.stop(resolve
);