Bug 1807268 - Re-enable verifyShowClipboardSuggestionsToggleTest UI test r=jajohnson
[gecko.git] / netwerk / test / unit / test_progress_no_proxy_and_proxy.js
blobedaf9b389d6d96b395de27036040ebf6b9520e82
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict";
6 // This test can be merged with test_progress.js once HTTP/3 tests are
7 // enabled on all plaforms.
9 var last = 0;
10 var max = 0;
11 var using_proxy = false;
13 const RESPONSE_LENGTH = 3000000;
14 const STATUS_RECEIVING_FROM = 0x4b0006;
16 const TYPE_ONSTATUS = 1;
17 const TYPE_ONPROGRESS = 2;
18 const TYPE_ONSTARTREQUEST = 3;
19 const TYPE_ONDATAAVAILABLE = 4;
20 const TYPE_ONSTOPREQUEST = 5;
22 var ProgressCallback = function () {};
24 ProgressCallback.prototype = {
25   _listener: null,
26   _got_onstartrequest: false,
27   _got_onstatus_after_onstartrequest: false,
28   _last_callback_handled: null,
29   statusArg: "",
30   finish: null,
32   QueryInterface: ChromeUtils.generateQI([
33     "nsIProgressEventSink",
34     "nsIStreamListener",
35     "nsIRequestObserver",
36   ]),
38   getInterface(iid) {
39     if (
40       iid.equals(Ci.nsIProgressEventSink) ||
41       iid.equals(Ci.nsIStreamListener) ||
42       iid.equals(Ci.nsIRequestObserver)
43     ) {
44       return this;
45     }
46     throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
47   },
49   onStartRequest(request) {
50     Assert.equal(this._last_callback_handled, TYPE_ONSTATUS);
51     this._got_onstartrequest = true;
52     this._last_callback_handled = TYPE_ONSTARTREQUEST;
54     this._listener = new ChannelListener(checkRequest, request);
55     this._listener.onStartRequest(request);
56   },
58   onDataAvailable(request, data, offset, count) {
59     Assert.equal(this._last_callback_handled, TYPE_ONPROGRESS);
60     this._last_callback_handled = TYPE_ONDATAAVAILABLE;
62     this._listener.onDataAvailable(request, data, offset, count);
63   },
65   onStopRequest(request, status) {
66     Assert.equal(this._last_callback_handled, TYPE_ONDATAAVAILABLE);
67     Assert.ok(this._got_onstatus_after_onstartrequest);
68     this._last_callback_handled = TYPE_ONSTOPREQUEST;
70     this._listener.onStopRequest(request, status);
71     delete this._listener;
73     check_http_info(request, this.expected_httpVersion, using_proxy);
75     this.finish();
76   },
78   onProgress(request, progress, progressMax) {
79     Assert.equal(this._last_callback_handled, TYPE_ONSTATUS);
80     this._last_callback_handled = TYPE_ONPROGRESS;
82     Assert.equal(this.mStatus, STATUS_RECEIVING_FROM);
83     last = progress;
84     max = progressMax;
85   },
87   onStatus(request, status, statusArg) {
88     if (!this._got_onstartrequest) {
89       // Ensure that all messages before onStartRequest are onStatus
90       if (this._last_callback_handled) {
91         Assert.equal(this._last_callback_handled, TYPE_ONSTATUS);
92       }
93     } else if (this._last_callback_handled == TYPE_ONSTARTREQUEST) {
94       this._got_onstatus_after_onstartrequest = true;
95     } else {
96       Assert.equal(this._last_callback_handled, TYPE_ONDATAAVAILABLE);
97     }
98     this._last_callback_handled = TYPE_ONSTATUS;
100     Assert.equal(statusArg, this.statusArg);
101     this.mStatus = status;
102   },
104   mStatus: 0,
107 function chanPromise(uri, statusArg, version) {
108   return new Promise(resolve => {
109     var chan = makeHTTPChannel(uri, using_proxy);
110     chan.requestMethod = "GET";
111     let listener = new ProgressCallback();
112     listener.statusArg = statusArg;
113     chan.notificationCallbacks = listener;
114     listener.expected_httpVersion = version;
115     listener.finish = resolve;
116     chan.asyncOpen(listener);
117   });
120 function checkRequest() {
121   Assert.equal(last, RESPONSE_LENGTH);
122   Assert.equal(max, RESPONSE_LENGTH);
125 async function check_progress(server) {
126   info(`Testing ${server.constructor.name}`);
127   await server.registerPathHandler("/test", (req, resp) => {
128     // Generate a post.
129     function generateContent(size) {
130       return "0".repeat(size);
131     }
133     resp.writeHead(200, {
134       "content-type": "application/json",
135       "content-length": "3000000",
136     });
137     resp.end(generateContent(3000000));
138   });
139   await chanPromise(
140     `${server.origin()}/test`,
141     `${server.domain()}`,
142     server.version()
143   );
146 add_task(async function setup() {
147   let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
148     Ci.nsIX509CertDB
149   );
150   addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
151   addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
154 add_task(async function test_http_1_and_2() {
155   await with_node_servers(
156     [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
157     check_progress
158   );
161 add_task(async function test_http_proxy() {
162   using_proxy = true;
163   let proxy = new NodeHTTPProxyServer();
164   await proxy.start();
165   await with_node_servers(
166     [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
167     check_progress
168   );
169   await proxy.stop();
170   using_proxy = false;
173 add_task(async function test_https_proxy() {
174   using_proxy = true;
175   let proxy = new NodeHTTPSProxyServer();
176   await proxy.start();
177   await with_node_servers(
178     [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
179     check_progress
180   );
181   await proxy.stop();
182   using_proxy = false;
185 add_task(async function test_http2_proxy() {
186   using_proxy = true;
187   let proxy = new NodeHTTP2ProxyServer();
188   await proxy.start();
189   await with_node_servers(
190     [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
191     check_progress
192   );
193   await proxy.stop();
194   using_proxy = false;
197 add_task(async function test_http3() {
198   await http3_setup_tests("h3-29");
199   await chanPromise(
200     "https://foo.example.com/" + RESPONSE_LENGTH,
201     "foo.example.com",
202     "h3-29"
203   );
204   http3_clear_prefs();