From 84a121078c67351e5ca7bef9c16e2d547a1e9840 Mon Sep 17 00:00:00 2001 From: Jan Michael Auer Date: Thu, 29 Mar 2018 10:00:54 +0200 Subject: [PATCH] Add `defaultPort` field (#43) * Add defaultPort to the Agent * Add tests for defaultPort --- index.js | 1 + test/test.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/index.js b/index.js index 4e007128..0a2fdabe 100644 --- a/index.js +++ b/index.js @@ -56,6 +56,7 @@ function HttpsProxyAgent(opts) { } this.proxy = proxy; + this.defaultPort = 443; } inherits(HttpsProxyAgent, Agent); diff --git a/test/test.js b/test/test.js index 9017701f..b3684958 100644 --- a/test/test.js +++ b/test/test.js @@ -107,6 +107,11 @@ describe('HttpsProxyAgent', function () { assert.equal('127.0.0.1', agent.proxy.host); assert.equal(proxyPort, agent.proxy.port); }); + it('should set a `defaultPort` property', function () { + var opts = url.parse("http://127.0.0.1:" + proxyPort); + var agent = new HttpsProxyAgent(opts); + assert.equal(443, agent.defaultPort); + }); describe('secureProxy', function () { it('should default to `false`', function () { var agent = new HttpsProxyAgent({ port: proxyPort }); @@ -303,6 +308,35 @@ describe('HttpsProxyAgent', function () { }); }); + it('should not send a port number for the default port', function (done) { + sslServer.once('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + }); + + var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || "https://127.0.0.1:" + sslProxyPort; + proxy = url.parse(proxy); + proxy.rejectUnauthorized = false; + var agent = new HttpsProxyAgent(proxy); + agent.defaultPort = sslServerPort; + + var opts = url.parse("https://127.0.0.1:" + sslServerPort); + opts.agent = agent; + opts.rejectUnauthorized = false; + + https.get(opts, function(res) { + var data = ""; + res.setEncoding("utf8"); + res.on("data", function(b) { + data += b; + }); + res.on("end", function() { + data = JSON.parse(data); + assert.equal("127.0.0.1", data.host); + done(); + }); + }); + }); + }); });