test_throttlechannel.js (1324B)
1 // Test nsIThrottledInputChannel interface. 2 "use strict"; 3 4 const { HttpServer } = ChromeUtils.importESModule( 5 "resource://testing-common/httpd.sys.mjs" 6 ); 7 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); 13 } 14 15 function make_channel(url) { 16 return NetUtil.newChannel({ 17 uri: url, 18 loadUsingSystemPrincipal: true, 19 }).QueryInterface(Ci.nsIHttpChannel); 20 } 21 22 function run_test() { 23 let httpserver = new HttpServer(); 24 httpserver.start(-1); 25 const PORT = httpserver.identity.primaryPort; 26 27 httpserver.registerPathHandler("/testdir", test_handler); 28 29 let channel = make_channel("http://localhost:" + PORT + "/testdir"); 30 31 let tq = Cc["@mozilla.org/network/throttlequeue;1"].createInstance( 32 Ci.nsIInputChannelThrottleQueue 33 ); 34 tq.init(1000, 1000); 35 36 let tic = channel.QueryInterface(Ci.nsIThrottledInputChannel); 37 tic.throttleQueue = tq; 38 39 channel.asyncOpen( 40 new ChannelListener(() => { 41 Assert.greater( 42 tq.bytesProcessed(), 43 0, 44 "throttled queue processed some bytes" 45 ); 46 47 httpserver.stop(do_test_finished); 48 }) 49 ); 50 51 do_test_pending(); 52 }