Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_httpsuspend.js
blob581caf906e6c9be1c2a69e85d1fb17760f382c36
1 // This file ensures that suspending a channel directly after opening it
2 // suspends future notifications correctly.
4 "use strict";
6 const { HttpServer } = ChromeUtils.importESModule(
7   "resource://testing-common/httpd.sys.mjs"
8 );
10 ChromeUtils.defineLazyGetter(this, "URL", function () {
11   return "http://localhost:" + httpserv.identity.primaryPort;
12 });
14 const MIN_TIME_DIFFERENCE = 3000;
15 const RESUME_DELAY = 5000;
17 var listener = {
18   _lastEvent: 0,
19   _gotData: false,
21   QueryInterface: ChromeUtils.generateQI([
22     "nsIStreamListener",
23     "nsIRequestObserver",
24   ]),
26   onStartRequest(request) {
27     this._lastEvent = Date.now();
28     request.QueryInterface(Ci.nsIRequest);
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   },
42   onDataAvailable(request, stream, offset, count) {
43     Assert.ok(Date.now() - this._lastEvent >= MIN_TIME_DIFFERENCE);
44     read_stream(stream, count);
46     // Ensure that suspending and resuming inside a callback works correctly
47     request.suspend();
48     request.suspend();
49     request.resume();
50     request.resume();
52     this._gotData = true;
53   },
55   onStopRequest(request, status) {
56     Assert.ok(this._gotData);
57     httpserv.stop(do_test_finished);
58   },
61 function makeChan(url) {
62   return NetUtil.newChannel({
63     uri: url,
64     loadUsingSystemPrincipal: true,
65   }).QueryInterface(Ci.nsIHttpChannel);
68 var httpserv = null;
70 function run_test() {
71   httpserv = new HttpServer();
72   httpserv.registerPathHandler("/woo", data);
73   httpserv.start(-1);
75   var chan = makeChan(URL + "/woo");
76   chan.QueryInterface(Ci.nsIRequest);
77   chan.asyncOpen(listener);
79   do_test_pending();
82 function data(metadata, response) {
83   let httpbody = "0123456789";
84   response.setHeader("Content-Type", "text/plain", false);
85   response.bodyOutputStream.write(httpbody, httpbody.length);