summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_proxy-slow-upload.js
blob: 4bfbe56f10b7e1143c434c5698b89efe8227c213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/* import-globals-from head_cache.js */
/* import-globals-from head_cookies.js */
/* import-globals-from head_channels.js */
/* import-globals-from head_servers.js */

const SIZE = 4096;
const CONTENT = "x".repeat(SIZE);

add_task(async function test_slow_upload() {
  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
    Ci.nsIX509CertDB
  );
  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
  addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");

  let proxies = [
    NodeHTTPProxyServer,
    NodeHTTPSProxyServer,
    NodeHTTP2ProxyServer,
  ];
  for (let p of proxies) {
    let proxy = new p();
    await proxy.start();
    registerCleanupFunction(async () => {
      await proxy.stop();
    });

    await with_node_servers(
      [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
      async server => {
        info(`Testing ${p.name} with ${server.constructor.name}`);
        await server.execute(
          `global.server_name = "${server.constructor.name}";`
        );
        await server.registerPathHandler("/test", (req, resp) => {
          let content = "";
          req.on("data", data => {
            global.data_count = (global.data_count || 0) + 1;
            content += data;
          });
          req.on("end", () => {
            resp.writeHead(200);
            resp.end(content);
          });
        });

        let sstream = Cc[
          "@mozilla.org/io/string-input-stream;1"
        ].createInstance(Ci.nsIStringInputStream);
        sstream.data = CONTENT;

        let mime = Cc[
          "@mozilla.org/network/mime-input-stream;1"
        ].createInstance(Ci.nsIMIMEInputStream);
        mime.addHeader("Content-Type", "multipart/form-data; boundary=zzzzz");
        mime.setData(sstream);

        let tq = Cc["@mozilla.org/network/throttlequeue;1"].createInstance(
          Ci.nsIInputChannelThrottleQueue
        );
        // Make sure the request takes more than one read.
        tq.init(100 + SIZE / 2, 100 + SIZE / 2);

        let chan = NetUtil.newChannel({
          uri: `${server.origin()}/test`,
          loadUsingSystemPrincipal: true,
        }).QueryInterface(Ci.nsIHttpChannel);

        let tic = chan.QueryInterface(Ci.nsIThrottledInputChannel);
        tic.throttleQueue = tq;

        chan
          .QueryInterface(Ci.nsIUploadChannel)
          .setUploadStream(mime, "", mime.available());
        chan.requestMethod = "POST";

        let { req, buff } = await new Promise(resolve => {
          chan.asyncOpen(
            new ChannelListener(
              (req, buff) => resolve({ req, buff }),
              null,
              CL_ALLOW_UNKNOWN_CL
            )
          );
        });
        equal(req.status, Cr.NS_OK);
        equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
        ok(buff == CONTENT, "Content must match");
        ok(!!req.QueryInterface(Ci.nsIProxiedChannel).proxyInfo);
        greater(
          await server.execute(`global.data_count`),
          1,
          "Content should have been streamed to the server in several chunks"
        );
      }
    );
    await proxy.stop();
  }
});