summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_progress_no_proxy_and_proxy.js
blob: cb132360c8ab276ac11a9c15b3412eb7420023c1 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* 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";

// This test can be merged with test_progress.js once HTTP/3 tests are
// enabled on all plaforms.

var last = 0;
var max = 0;
var using_proxy = false;

const RESPONSE_LENGTH = 3000000;
const STATUS_RECEIVING_FROM = 0x4b0006;

const TYPE_ONSTATUS = 1;
const TYPE_ONPROGRESS = 2;
const TYPE_ONSTARTREQUEST = 3;
const TYPE_ONDATAAVAILABLE = 4;
const TYPE_ONSTOPREQUEST = 5;

var ProgressCallback = function () {};

ProgressCallback.prototype = {
  _listener: null,
  _got_onstartrequest: false,
  _got_onstatus_after_onstartrequest: false,
  _last_callback_handled: null,
  statusArg: "",
  finish: null,

  QueryInterface: ChromeUtils.generateQI([
    "nsIProgressEventSink",
    "nsIStreamListener",
    "nsIRequestObserver",
  ]),

  getInterface(iid) {
    if (
      iid.equals(Ci.nsIProgressEventSink) ||
      iid.equals(Ci.nsIStreamListener) ||
      iid.equals(Ci.nsIRequestObserver)
    ) {
      return this;
    }
    throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
  },

  onStartRequest(request) {
    Assert.equal(this._last_callback_handled, TYPE_ONSTATUS);
    this._got_onstartrequest = true;
    this._last_callback_handled = TYPE_ONSTARTREQUEST;

    this._listener = new ChannelListener(checkRequest, request);
    this._listener.onStartRequest(request);
  },

  onDataAvailable(request, data, offset, count) {
    Assert.equal(this._last_callback_handled, TYPE_ONPROGRESS);
    this._last_callback_handled = TYPE_ONDATAAVAILABLE;

    this._listener.onDataAvailable(request, data, offset, count);
  },

  onStopRequest(request, status) {
    Assert.equal(this._last_callback_handled, TYPE_ONDATAAVAILABLE);
    Assert.ok(this._got_onstatus_after_onstartrequest);
    this._last_callback_handled = TYPE_ONSTOPREQUEST;

    this._listener.onStopRequest(request, status);
    delete this._listener;

    check_http_info(request, this.expected_httpVersion, using_proxy);

    this.finish();
  },

  onProgress(request, progress, progressMax) {
    Assert.equal(this._last_callback_handled, TYPE_ONSTATUS);
    this._last_callback_handled = TYPE_ONPROGRESS;

    Assert.equal(this.mStatus, STATUS_RECEIVING_FROM);
    last = progress;
    max = progressMax;
  },

  onStatus(request, status, statusArg) {
    if (!this._got_onstartrequest) {
      // Ensure that all messages before onStartRequest are onStatus
      if (this._last_callback_handled) {
        Assert.equal(this._last_callback_handled, TYPE_ONSTATUS);
      }
    } else if (this._last_callback_handled == TYPE_ONSTARTREQUEST) {
      this._got_onstatus_after_onstartrequest = true;
    } else {
      Assert.equal(this._last_callback_handled, TYPE_ONDATAAVAILABLE);
    }
    this._last_callback_handled = TYPE_ONSTATUS;

    Assert.equal(statusArg, this.statusArg);
    this.mStatus = status;
  },

  mStatus: 0,
};

function chanPromise(uri, statusArg, version) {
  return new Promise(resolve => {
    var chan = makeHTTPChannel(uri, using_proxy);
    chan.requestMethod = "GET";
    let listener = new ProgressCallback();
    listener.statusArg = statusArg;
    chan.notificationCallbacks = listener;
    listener.expected_httpVersion = version;
    listener.finish = resolve;
    chan.asyncOpen(listener);
  });
}

function checkRequest(request, data, context) {
  Assert.equal(last, RESPONSE_LENGTH);
  Assert.equal(max, RESPONSE_LENGTH);
}

async function check_progress(server) {
  info(`Testing ${server.constructor.name}`);
  await server.registerPathHandler("/test", (req, resp) => {
    // Generate a post.
    function generateContent(size) {
      return "0".repeat(size);
    }

    resp.writeHead(200, {
      "content-type": "application/json",
      "content-length": "3000000",
    });
    resp.end(generateContent(3000000));
  });
  await chanPromise(
    `${server.origin()}/test`,
    `${server.domain()}`,
    server.version()
  );
}

add_task(async function setup() {
  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");
});

add_task(async function test_http_1_and_2() {
  await with_node_servers(
    [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
    check_progress
  );
});

add_task(async function test_http_proxy() {
  using_proxy = true;
  let proxy = new NodeHTTPProxyServer();
  await proxy.start();
  await with_node_servers(
    [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
    check_progress
  );
  await proxy.stop();
  using_proxy = false;
});

add_task(async function test_https_proxy() {
  using_proxy = true;
  let proxy = new NodeHTTPSProxyServer();
  await proxy.start();
  await with_node_servers(
    [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
    check_progress
  );
  await proxy.stop();
  using_proxy = false;
});

add_task(async function test_http2_proxy() {
  using_proxy = true;
  let proxy = new NodeHTTP2ProxyServer();
  await proxy.start();
  await with_node_servers(
    [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
    check_progress
  );
  await proxy.stop();
  using_proxy = false;
});

add_task(async function test_http3() {
  await http3_setup_tests("h3-29");
  await chanPromise(
    "https://foo.example.com/" + RESPONSE_LENGTH,
    "foo.example.com",
    "h3-29"
  );
  http3_clear_prefs();
});