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 function makeChan(uri
) {
13 let chan
= NetUtil
.newChannel({
15 loadUsingSystemPrincipal
: true,
16 }).QueryInterface(Ci
.nsIHttpChannel
);
17 chan
.loadFlags
= Ci
.nsIChannel
.LOAD_INITIAL_DOCUMENT_URI
;
21 function channelOpenPromise(chan
, flags
) {
22 return new Promise(resolve
=> {
23 function finish(req
, buffer
) {
24 resolve([req
, buffer
]);
26 chan
.asyncOpen(new ChannelListener(finish
, null, flags
));
30 add_task(async
function test_connection_based_auth() {
31 let certdb
= Cc
["@mozilla.org/security/x509certdb;1"].getService(
34 addCertFromFile(certdb
, "http2-ca.pem", "CTu,u,u");
35 addCertFromFile(certdb
, "proxy-ca.pem", "CTu,u,u");
37 let proxy
= new NodeHTTPSProxyServer();
40 await proxy
.registerConnectHandler((req
, clientSocket
) => {
41 if (!req
.headers
["proxy-authorization"]) {
43 "HTTP/1.1 407 Unauthorized\r\n" +
44 "Proxy-agent: Node.js-Proxy\r\n" +
45 "Connection: keep-alive\r\n" +
46 "Proxy-Authenticate: mock_auth\r\n" +
47 "Content-Length: 0\r\n" +
51 clientSocket
.on("data", data
=> {
52 let array
= data
.toString().split("\r\n");
53 let proxyAuthorization
= "";
54 for (let line
of array
) {
55 let pair
= line
.split(":").map(element
=> element
.trim());
56 if (pair
[0] === "Proxy-Authorization") {
57 proxyAuthorization
= pair
[1];
61 if (proxyAuthorization
=== "moz_test_credentials") {
62 // We don't return 200 OK here, because we don't have a server
65 "HTTP/1.1 404 Not Found\r\nProxy-agent: Node.js-Proxy\r\n\r\n"
69 "HTTP/1.1 502 Error\r\nProxy-agent: Node.js-Proxy\r\n\r\n"
72 clientSocket
.destroy();
77 // We should not reach here.
79 "HTTP/1.1 502 Error\r\nProxy-agent: Node.js-Proxy\r\n\r\n"
81 clientSocket
.destroy();
84 let chan
= makeChan(`https://example.ntlm.com/test`);
85 let [req
] = await
channelOpenPromise(chan
, CL_EXPECT_FAILURE
);
86 Assert
.equal(req
.status
, Cr
.NS_ERROR_UNKNOWN_HOST
);
87 req
.QueryInterface(Ci
.nsIProxiedChannel
);
88 Assert
.equal(req
.httpProxyConnectResponseCode
, 404);