Bug 1845311 - [Part 2] Use ChromeUtils.defineLazyGetter in more places r=arai,webcomp...
[gecko.git] / netwerk / test / unit / test_throttlechannel.js
blob5a0cfd1151c1901bfbbfbea19ccd6a1afaf7038c
1 // Test nsIThrottledInputChannel interface.
2 "use strict";
4 const { HttpServer } = ChromeUtils.importESModule(
5   "resource://testing-common/httpd.sys.mjs"
6 );
8 function test_handler(metadata, response) {
9   const originalBody = "the response";
10   response.setHeader("Content-Type", "text/html", false);
11   response.setStatusLine(metadata.httpVersion, 200, "OK");
12   response.bodyOutputStream.write(originalBody, originalBody.length);
15 function make_channel(url) {
16   return NetUtil.newChannel({
17     uri: url,
18     loadUsingSystemPrincipal: true,
19   }).QueryInterface(Ci.nsIHttpChannel);
22 function run_test() {
23   let httpserver = new HttpServer();
24   httpserver.start(-1);
25   const PORT = httpserver.identity.primaryPort;
27   httpserver.registerPathHandler("/testdir", test_handler);
29   let channel = make_channel("http://localhost:" + PORT + "/testdir");
31   let tq = Cc["@mozilla.org/network/throttlequeue;1"].createInstance(
32     Ci.nsIInputChannelThrottleQueue
33   );
34   tq.init(1000, 1000);
36   let tic = channel.QueryInterface(Ci.nsIThrottledInputChannel);
37   tic.throttleQueue = tq;
39   channel.asyncOpen(
40     new ChannelListener(() => {
41       ok(tq.bytesProcessed() > 0, "throttled queue processed some bytes");
43       httpserver.stop(do_test_finished);
44     })
45   );
47   do_test_pending();