Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_connection_based_auth.js
blob049478aebadd7a5aded90c1490454e9759e4adb1
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 function makeChan(uri) {
13 let chan = NetUtil.newChannel({
14 uri,
15 loadUsingSystemPrincipal: true,
16 }).QueryInterface(Ci.nsIHttpChannel);
17 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
18 return chan;
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));
27 });
30 add_task(async function test_connection_based_auth() {
31 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
32 Ci.nsIX509CertDB
34 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
35 addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
37 let proxy = new NodeHTTPSProxyServer();
38 await proxy.start();
40 await proxy.registerConnectHandler((req, clientSocket) => {
41 if (!req.headers["proxy-authorization"]) {
42 clientSocket.write(
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" +
48 "\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
63 // to connect to.
64 clientSocket.write(
65 "HTTP/1.1 404 Not Found\r\nProxy-agent: Node.js-Proxy\r\n\r\n"
67 } else {
68 clientSocket.write(
69 "HTTP/1.1 502 Error\r\nProxy-agent: Node.js-Proxy\r\n\r\n"
72 clientSocket.destroy();
73 });
74 return;
77 // We should not reach here.
78 clientSocket.write(
79 "HTTP/1.1 502 Error\r\nProxy-agent: Node.js-Proxy\r\n\r\n"
81 clientSocket.destroy();
82 });
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);
90 await proxy.stop();
91 });