1 // Test nsIThrottledInputChannel interface.
4 const { HttpServer } = ChromeUtils.importESModule(
5 "resource://testing-common/httpd.sys.mjs"
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({
18 loadUsingSystemPrincipal: true,
19 }).QueryInterface(Ci.nsIHttpChannel);
23 let httpserver = new HttpServer();
24 httpserver.registerPathHandler("/testdir", test_handler);
27 const PORT = httpserver.identity.primaryPort;
30 let sstream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
31 Ci.nsIStringInputStream
33 sstream.data = "x".repeat(size);
35 let mime = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(
38 mime.addHeader("Content-Type", "multipart/form-data; boundary=zzzzz");
39 mime.setData(sstream);
41 let tq = Cc["@mozilla.org/network/throttlequeue;1"].createInstance(
42 Ci.nsIInputChannelThrottleQueue
44 // Make sure the request takes more than one read.
45 tq.init(100 + size / 2, 100 + size / 2);
47 let channel = make_channel("http://localhost:" + PORT + "/testdir");
49 .QueryInterface(Ci.nsIUploadChannel)
50 .setUploadStream(mime, "", mime.available());
51 channel.requestMethod = "POST";
53 let tic = channel.QueryInterface(Ci.nsIThrottledInputChannel);
54 tic.throttleQueue = tq;
56 let startTime = Date.now();
58 new ChannelListener(() => {
59 ok(Date.now() - startTime > 1000, "request took more than one second");
61 httpserver.stop(do_test_finished);