test_httpsuspend.js (2064B)
1 // This file ensures that suspending a channel directly after opening it 2 // suspends future notifications correctly. 3 4 "use strict"; 5 6 const { HttpServer } = ChromeUtils.importESModule( 7 "resource://testing-common/httpd.sys.mjs" 8 ); 9 10 ChromeUtils.defineLazyGetter(this, "URL", function () { 11 return "http://localhost:" + httpserv.identity.primaryPort; 12 }); 13 14 const MIN_TIME_DIFFERENCE = 3000; 15 const RESUME_DELAY = 5000; 16 17 var listener = { 18 _lastEvent: 0, 19 _gotData: false, 20 21 QueryInterface: ChromeUtils.generateQI([ 22 "nsIStreamListener", 23 "nsIRequestObserver", 24 ]), 25 26 onStartRequest(request) { 27 this._lastEvent = Date.now(); 28 request.QueryInterface(Ci.nsIRequest); 29 30 // Insert a delay between this and the next callback to ensure message buffering 31 // works correctly 32 request.suspend(); 33 request.suspend(); 34 do_timeout(RESUME_DELAY, function () { 35 request.resume(); 36 }); 37 do_timeout(RESUME_DELAY + 1000, function () { 38 request.resume(); 39 }); 40 }, 41 42 onDataAvailable(request, stream, offset, count) { 43 Assert.greaterOrEqual(Date.now() - this._lastEvent, MIN_TIME_DIFFERENCE); 44 read_stream(stream, count); 45 46 // Ensure that suspending and resuming inside a callback works correctly 47 request.suspend(); 48 request.suspend(); 49 request.resume(); 50 request.resume(); 51 52 this._gotData = true; 53 }, 54 55 onStopRequest() { 56 Assert.ok(this._gotData); 57 httpserv.stop(do_test_finished); 58 }, 59 }; 60 61 function makeChan(url) { 62 return NetUtil.newChannel({ 63 uri: url, 64 loadUsingSystemPrincipal: true, 65 }).QueryInterface(Ci.nsIHttpChannel); 66 } 67 68 var httpserv = null; 69 70 function run_test() { 71 httpserv = new HttpServer(); 72 httpserv.registerPathHandler("/woo", data); 73 httpserv.start(-1); 74 75 var chan = makeChan(URL + "/woo"); 76 chan.QueryInterface(Ci.nsIRequest); 77 chan.asyncOpen(listener); 78 79 do_test_pending(); 80 } 81 82 function data(metadata, response) { 83 let httpbody = "0123456789"; 84 response.setHeader("Content-Type", "text/plain", false); 85 response.bodyOutputStream.write(httpbody, httpbody.length); 86 }