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/. */
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({
19 loadUsingSystemPrincipal
: true,
20 }).QueryInterface(Ci
.nsIHttpChannel
);
21 chan
.loadFlags
= Ci
.nsIChannel
.LOAD_INITIAL_DOCUMENT_URI
;
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
));
34 function registerSimplePathHandler(server
, path
) {
35 return server
.registerPathHandler(path
, (req
, resp
) => {
41 function regiisterServerNamePathHandler(server
, path
) {
42 return server
.registerPathHandler(path
, (req
, resp
) => {
44 resp
.end(global
.server_name
);
48 add_task(async
function test_dual_stack() {
49 let httpserv
= new HttpServer();
51 httpserv
.registerPathHandler("/", function handler(metadata
, response
) {
52 response
.setHeader("Content-Length", `${content.length}`);
53 response
.bodyOutputStream
.write(content
, content
.length
);
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
));
67 add_task(async
function test_http() {
68 let server
= new NodeHTTPServer();
70 registerCleanupFunction(async () => {
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
));
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
));
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);
92 add_task(async
function test_https() {
93 let certdb
= Cc
["@mozilla.org/security/x509certdb;1"].getService(
96 addCertFromFile(certdb
, "http2-ca.pem", "CTu,u,u");
98 let server
= new NodeHTTPSServer();
100 registerCleanupFunction(async () => {
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");
121 add_task(async
function test_http2() {
122 let certdb
= Cc
["@mozilla.org/security/x509certdb;1"].getService(
125 addCertFromFile(certdb
, "http2-ca.pem", "CTu,u,u");
127 let server
= new NodeHTTP2Server();
128 await server
.start();
129 registerCleanupFunction(async () => {
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");
150 add_task(async
function test_http1_proxy() {
151 let certdb
= Cc
["@mozilla.org/security/x509certdb;1"].getService(
154 addCertFromFile(certdb
, "http2-ca.pem", "CTu,u,u");
156 let proxy
= new NodeHTTPProxyServer();
158 registerCleanupFunction(async () => {
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
],
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`),
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);
186 req1
.QueryInterface(Ci
.nsIHttpChannel
).protocolVersion
,
187 server
.constructor.name
== "NodeHTTP2Server" ? "h2" : "http/1.1"
195 add_task(async
function test_https_proxy() {
196 let certdb
= Cc
["@mozilla.org/security/x509certdb;1"].getService(
199 addCertFromFile(certdb
, "http2-ca.pem", "CTu,u,u");
200 addCertFromFile(certdb
, "proxy-ca.pem", "CTu,u,u");
202 let proxy
= new NodeHTTPSProxyServer();
204 registerCleanupFunction(async () => {
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
],
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`),
227 equal(req1
.status
, Cr
.NS_OK
);
228 equal(req1
.QueryInterface(Ci
.nsIHttpChannel
).responseStatus
, 200);
229 equal(buff
, server
.constructor.name
);
236 add_task(async
function test_http2_proxy() {
237 let certdb
= Cc
["@mozilla.org/security/x509certdb;1"].getService(
240 addCertFromFile(certdb
, "http2-ca.pem", "CTu,u,u");
241 addCertFromFile(certdb
, "proxy-ca.pem", "CTu,u,u");
243 let proxy
= new NodeHTTP2ProxyServer();
245 registerCleanupFunction(async () => {
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
],
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`),
267 equal(req1
.status
, Cr
.NS_OK
);
268 equal(req1
.QueryInterface(Ci
.nsIHttpChannel
).responseStatus
, 200);
269 equal(buff
, server
.constructor.name
);
276 add_task(async
function test_proxy_with_redirects() {
277 let certdb
= Cc
["@mozilla.org/security/x509certdb;1"].getService(
280 addCertFromFile(certdb
, "http2-ca.pem", "CTu,u,u");
284 NodeHTTPSProxyServer
,
285 NodeHTTP2ProxyServer
,
287 for (let p
of proxies
) {
290 registerCleanupFunction(async () => {
294 await
with_node_servers(
295 [NodeHTTPServer
, NodeHTTPSServer
, NodeHTTP2Server
],
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, {
305 resp
.end(global
.server_name
);
307 await server
.registerPathHandler("/test", (req
, resp
) => {
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
);
319 notEqual(req
.proxyInfo
.type
, "direct");