Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_proxy-slow-upload.js
blob1a406890e155e15dcd347a3fa0e0daa767064fd7
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/. */
5 "use strict";
7 /* import-globals-from head_cache.js */
8 /* import-globals-from head_cookies.js */
9 /* import-globals-from head_channels.js */
10 /* import-globals-from head_servers.js */
12 const SIZE = 4096;
13 const CONTENT = "x".repeat(SIZE);
15 add_task(async function test_slow_upload() {
16   let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
17     Ci.nsIX509CertDB
18   );
19   addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
20   addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
22   let proxies = [
23     NodeHTTPProxyServer,
24     NodeHTTPSProxyServer,
25     NodeHTTP2ProxyServer,
26   ];
27   for (let p of proxies) {
28     let proxy = new p();
29     await proxy.start();
30     registerCleanupFunction(async () => {
31       await proxy.stop();
32     });
34     await with_node_servers(
35       [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
36       async server => {
37         info(`Testing ${p.name} with ${server.constructor.name}`);
38         await server.execute(
39           `global.server_name = "${server.constructor.name}";`
40         );
41         await server.registerPathHandler("/test", (req, resp) => {
42           let content = "";
43           req.on("data", data => {
44             global.data_count = (global.data_count || 0) + 1;
45             content += data;
46           });
47           req.on("end", () => {
48             resp.writeHead(200);
49             resp.end(content);
50           });
51         });
53         let sstream = Cc[
54           "@mozilla.org/io/string-input-stream;1"
55         ].createInstance(Ci.nsIStringInputStream);
56         sstream.data = CONTENT;
58         let mime = Cc[
59           "@mozilla.org/network/mime-input-stream;1"
60         ].createInstance(Ci.nsIMIMEInputStream);
61         mime.addHeader("Content-Type", "multipart/form-data; boundary=zzzzz");
62         mime.setData(sstream);
64         let tq = Cc["@mozilla.org/network/throttlequeue;1"].createInstance(
65           Ci.nsIInputChannelThrottleQueue
66         );
67         // Make sure the request takes more than one read.
68         tq.init(100 + SIZE / 2, 100 + SIZE / 2);
70         let chan = NetUtil.newChannel({
71           uri: `${server.origin()}/test`,
72           loadUsingSystemPrincipal: true,
73         }).QueryInterface(Ci.nsIHttpChannel);
75         let tic = chan.QueryInterface(Ci.nsIThrottledInputChannel);
76         tic.throttleQueue = tq;
78         chan
79           .QueryInterface(Ci.nsIUploadChannel)
80           .setUploadStream(mime, "", mime.available());
81         chan.requestMethod = "POST";
83         let { req, buff } = await new Promise(resolve => {
84           chan.asyncOpen(
85             new ChannelListener(
86               (req1, buff1) => resolve({ req: req1, buff: buff1 }),
87               null,
88               CL_ALLOW_UNKNOWN_CL
89             )
90           );
91         });
92         equal(req.status, Cr.NS_OK);
93         equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
94         ok(buff == CONTENT, "Content must match");
95         ok(!!req.QueryInterface(Ci.nsIProxiedChannel).proxyInfo);
96         greater(
97           await server.execute(`global.data_count`),
98           1,
99           "Content should have been streamed to the server in several chunks"
100         );
101       }
102     );
103     await proxy.stop();
104   }