Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_servers.js
blob1aee369f5c5082cb57f1747da275f94f4a03602e
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 { HttpServer } = ChromeUtils.importESModule(
13 "resource://testing-common/httpd.sys.mjs"
16 function makeChan(uri) {
17 let chan = NetUtil.newChannel({
18 uri,
19 loadUsingSystemPrincipal: true,
20 }).QueryInterface(Ci.nsIHttpChannel);
21 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
22 return chan;
25 function channelOpenPromise(chan, flags) {
26 return new Promise(resolve => {
27 function finish(req, buffer) {
28 resolve([req, buffer]);
30 chan.asyncOpen(new ChannelListener(finish, null, flags));
31 });
34 function registerSimplePathHandler(server, path) {
35 return server.registerPathHandler(path, (req, resp) => {
36 resp.writeHead(200);
37 resp.end("done");
38 });
41 function regiisterServerNamePathHandler(server, path) {
42 return server.registerPathHandler(path, (req, resp) => {
43 resp.writeHead(200);
44 resp.end(global.server_name);
45 });
48 add_task(async function test_dual_stack() {
49 let httpserv = new HttpServer();
50 let content = "ok";
51 httpserv.registerPathHandler("/", function handler(metadata, response) {
52 response.setHeader("Content-Length", `${content.length}`);
53 response.bodyOutputStream.write(content, content.length);
54 });
55 httpserv.start_dualStack(-1);
57 let chan = makeChan(`http://127.0.0.1:${httpserv.identity.primaryPort}/`);
58 let [, response] = await channelOpenPromise(chan);
59 Assert.equal(response, content);
61 chan = makeChan(`http://[::1]:${httpserv.identity.primaryPort}/`);
62 [, response] = await channelOpenPromise(chan);
63 Assert.equal(response, content);
64 await new Promise(resolve => httpserv.stop(resolve));
65 });
67 add_task(async function test_http() {
68 let server = new NodeHTTPServer();
69 await server.start();
70 registerCleanupFunction(async () => {
71 await server.stop();
72 });
73 let chan = makeChan(`http://localhost:${server.port()}/test`);
74 let req = await new Promise(resolve => {
75 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
76 });
77 equal(req.status, Cr.NS_OK);
78 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 404);
79 await registerSimplePathHandler(server, "/test");
80 chan = makeChan(`http://localhost:${server.port()}/test`);
81 req = await new Promise(resolve => {
82 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
83 });
84 equal(req.status, Cr.NS_OK);
85 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
86 equal(req.QueryInterface(Ci.nsIHttpChannel).protocolVersion, "http/1.1");
87 equal(req.QueryInterface(Ci.nsIHttpChannelInternal).isProxyUsed, false);
89 await server.stop();
90 });
92 add_task(async function test_https() {
93 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
94 Ci.nsIX509CertDB
96 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
98 let server = new NodeHTTPSServer();
99 await server.start();
100 registerCleanupFunction(async () => {
101 await server.stop();
103 let chan = makeChan(`https://localhost:${server.port()}/test`);
104 let req = await new Promise(resolve => {
105 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
107 equal(req.status, Cr.NS_OK);
108 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 404);
109 await registerSimplePathHandler(server, "/test");
110 chan = makeChan(`https://localhost:${server.port()}/test`);
111 req = await new Promise(resolve => {
112 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
114 equal(req.status, Cr.NS_OK);
115 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
116 equal(req.QueryInterface(Ci.nsIHttpChannel).protocolVersion, "http/1.1");
118 await server.stop();
121 add_task(async function test_http2() {
122 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
123 Ci.nsIX509CertDB
125 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
127 let server = new NodeHTTP2Server();
128 await server.start();
129 registerCleanupFunction(async () => {
130 await server.stop();
132 let chan = makeChan(`https://localhost:${server.port()}/test`);
133 let req = await new Promise(resolve => {
134 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
136 equal(req.status, Cr.NS_OK);
137 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 404);
138 await registerSimplePathHandler(server, "/test");
139 chan = makeChan(`https://localhost:${server.port()}/test`);
140 req = await new Promise(resolve => {
141 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
143 equal(req.status, Cr.NS_OK);
144 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
145 equal(req.QueryInterface(Ci.nsIHttpChannel).protocolVersion, "h2");
147 await server.stop();
150 add_task(async function test_http1_proxy() {
151 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
152 Ci.nsIX509CertDB
154 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
156 let proxy = new NodeHTTPProxyServer();
157 await proxy.start();
158 registerCleanupFunction(async () => {
159 await proxy.stop();
162 let chan = makeChan(`http://localhost:${proxy.port()}/test`);
163 let req = await new Promise(resolve => {
164 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
166 equal(req.status, Cr.NS_OK);
167 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 405);
169 await with_node_servers(
170 [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
171 async server => {
172 await server.execute(
173 `global.server_name = "${server.constructor.name}";`
175 await regiisterServerNamePathHandler(server, "/test");
176 let [req1, buff] = await channelOpenPromise(
177 makeChan(`${server.origin()}/test`),
178 CL_ALLOW_UNKNOWN_CL
180 equal(req1.status, Cr.NS_OK);
181 equal(req1.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
182 equal(buff, server.constructor.name);
183 //Bug 1792187: Check if proxy is set to true when a proxy is used.
184 equal(req1.QueryInterface(Ci.nsIHttpChannelInternal).isProxyUsed, true);
185 equal(
186 req1.QueryInterface(Ci.nsIHttpChannel).protocolVersion,
187 server.constructor.name == "NodeHTTP2Server" ? "h2" : "http/1.1"
192 await proxy.stop();
195 add_task(async function test_https_proxy() {
196 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
197 Ci.nsIX509CertDB
199 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
200 addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
202 let proxy = new NodeHTTPSProxyServer();
203 await proxy.start();
204 registerCleanupFunction(async () => {
205 await proxy.stop();
208 let chan = makeChan(`https://localhost:${proxy.port()}/test`);
209 let req = await new Promise(resolve => {
210 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
212 equal(req.status, Cr.NS_OK);
213 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 405);
215 await with_node_servers(
216 [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
217 async server => {
218 await server.execute(
219 `global.server_name = "${server.constructor.name}";`
221 await regiisterServerNamePathHandler(server, "/test");
223 let [req1, buff] = await channelOpenPromise(
224 makeChan(`${server.origin()}/test`),
225 CL_ALLOW_UNKNOWN_CL
227 equal(req1.status, Cr.NS_OK);
228 equal(req1.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
229 equal(buff, server.constructor.name);
233 await proxy.stop();
236 add_task(async function test_http2_proxy() {
237 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
238 Ci.nsIX509CertDB
240 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
241 addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
243 let proxy = new NodeHTTP2ProxyServer();
244 await proxy.start();
245 registerCleanupFunction(async () => {
246 await proxy.stop();
249 let chan = makeChan(`https://localhost:${proxy.port()}/test`);
250 let req = await new Promise(resolve => {
251 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
253 equal(req.status, Cr.NS_OK);
254 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 405);
256 await with_node_servers(
257 [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
258 async server => {
259 await server.execute(
260 `global.server_name = "${server.constructor.name}";`
262 await regiisterServerNamePathHandler(server, "/test");
263 let [req1, buff] = await channelOpenPromise(
264 makeChan(`${server.origin()}/test`),
265 CL_ALLOW_UNKNOWN_CL
267 equal(req1.status, Cr.NS_OK);
268 equal(req1.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
269 equal(buff, server.constructor.name);
273 await proxy.stop();
276 add_task(async function test_proxy_with_redirects() {
277 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
278 Ci.nsIX509CertDB
280 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
282 let proxies = [
283 NodeHTTPProxyServer,
284 NodeHTTPSProxyServer,
285 NodeHTTP2ProxyServer,
287 for (let p of proxies) {
288 let proxy = new p();
289 await proxy.start();
290 registerCleanupFunction(async () => {
291 await proxy.stop();
294 await with_node_servers(
295 [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
296 async server => {
297 info(`Testing ${p.name} with ${server.constructor.name}`);
298 await server.execute(
299 `global.server_name = "${server.constructor.name}";`
301 await server.registerPathHandler("/redirect", (req, resp) => {
302 resp.writeHead(302, {
303 Location: "/test",
305 resp.end(global.server_name);
307 await server.registerPathHandler("/test", (req, resp) => {
308 resp.writeHead(200);
309 resp.end(global.server_name);
312 let chan = makeChan(`${server.origin()}/redirect`);
313 let [req, buff] = await channelOpenPromise(chan, CL_ALLOW_UNKNOWN_CL);
314 equal(req.status, Cr.NS_OK);
315 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
316 equal(buff, server.constructor.name);
317 req.QueryInterface(Ci.nsIProxiedChannel);
318 ok(!!req.proxyInfo);
319 notEqual(req.proxyInfo.type, "direct");
322 await proxy.stop();