Bug 1845311 - [Part 2] Use ChromeUtils.defineLazyGetter in more places r=arai,webcomp...
[gecko.git] / netwerk / test / unit / test_loadgroup_cancel.js
blob9d5779437f935d910edeaa09ff9e2fc9f2b2b085
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 "use strict";
7 function makeChan(url) {
8   return NetUtil.newChannel({
9     uri: url,
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);
26     response.finish();
27   });
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
32 // canceled.
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(
42     Ci.nsILoadGroup
43   );
45   let loadListener = {
46     onStartRequest: aRequest => {
47       info("onStartRequest");
48     },
49     onStopRequest: (aRequest, aStatusCode) => {
50       equal(
51         aStatusCode,
52         Cr.NS_ERROR_ABORT,
53         "aStatusCode must be the cancellation code"
54       );
55       equal(
56         aRequest.status,
57         Cr.NS_ERROR_ABORT,
58         "aRequest.status must be the cancellation code"
59       );
60     },
61     QueryInterface: ChromeUtils.generateQI([
62       "nsIRequestObserver",
63       "nsISupportsWeakReference",
64     ]),
65   };
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));
77   let promises = [
78     new Promise(resolve => {
79       chan1.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE));
80     }),
81     new Promise(resolve => {
82       chan2.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE));
83     }),
84     new Promise(resolve => {
85       chan3.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE));
86     }),
87   ];
89   loadGroup.cancel(Cr.NS_ERROR_ABORT);
91   await Promise.all(promises);
93   await new Promise(resolve => {
94     http_server.stop(resolve);
95   });
96 });