From 4296770b6a0e631e3f8e7bd6cfd41ac8e91a3ec4 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 25 Oct 2019 12:48:00 -0700 Subject: [PATCH] Prettier --- index.d.ts | 36 +-- index.js | 384 ++++++++++++++-------------- test/test.js | 687 +++++++++++++++++++++++++++------------------------ 3 files changed, 569 insertions(+), 538 deletions(-) diff --git a/index.d.ts b/index.d.ts index d21a974f..cec35d85 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,22 +1,22 @@ declare module 'https-proxy-agent' { - import * as https from 'https' + import * as https from 'https'; - namespace HttpsProxyAgent { - interface HttpsProxyAgentOptions { - host: string - port: number | string - secureProxy?: boolean - headers?: { - [key: string]: string - } - [key: string]: any - } - } - - // HttpsProxyAgent doesnt *actually* extend https.Agent, but for my purposes I want it to pretend that it does - class HttpsProxyAgent extends https.Agent { - constructor(opts: HttpsProxyAgent.HttpsProxyAgentOptions | string) - } + namespace HttpsProxyAgent { + interface HttpsProxyAgentOptions { + host: string; + port: number | string; + secureProxy?: boolean; + headers?: { + [key: string]: string; + }; + [key: string]: any; + } + } - export = HttpsProxyAgent + // HttpsProxyAgent doesnt *actually* extend https.Agent, but for my purposes I want it to pretend that it does + class HttpsProxyAgent extends https.Agent { + constructor(opts: HttpsProxyAgent.HttpsProxyAgentOptions | string); + } + + export = HttpsProxyAgent; } diff --git a/index.js b/index.js index 2791aea8..aeb624db 100644 --- a/index.js +++ b/index.js @@ -24,40 +24,42 @@ module.exports = HttpsProxyAgent; */ function HttpsProxyAgent(opts) { - if (!(this instanceof HttpsProxyAgent)) return new HttpsProxyAgent(opts); - if ('string' == typeof opts) opts = url.parse(opts); - if (!opts) - throw new Error( - 'an HTTP(S) proxy server `host` and `port` must be specified!' - ); - debug('creating new HttpsProxyAgent instance: %o', opts); - Agent.call(this, opts); - - var proxy = Object.assign({}, opts); - - // if `true`, then connect to the proxy server over TLS. defaults to `false`. - this.secureProxy = proxy.protocol ? /^https:?$/i.test(proxy.protocol) : false; - - // prefer `hostname` over `host`, and set the `port` if needed - proxy.host = proxy.hostname || proxy.host; - proxy.port = +proxy.port || (this.secureProxy ? 443 : 80); - - // ALPN is supported by Node.js >= v5. - // attempt to negotiate http/1.1 for proxy servers that support http/2 - if (this.secureProxy && !('ALPNProtocols' in proxy)) { - proxy.ALPNProtocols = ['http 1.1'] - } - - if (proxy.host && proxy.path) { - // if both a `host` and `path` are specified then it's most likely the - // result of a `url.parse()` call... we need to remove the `path` portion so - // that `net.connect()` doesn't attempt to open that as a unix socket file. - delete proxy.path; - delete proxy.pathname; - } - - this.proxy = proxy; - this.defaultPort = 443; + if (!(this instanceof HttpsProxyAgent)) return new HttpsProxyAgent(opts); + if ('string' == typeof opts) opts = url.parse(opts); + if (!opts) + throw new Error( + 'an HTTP(S) proxy server `host` and `port` must be specified!' + ); + debug('creating new HttpsProxyAgent instance: %o', opts); + Agent.call(this, opts); + + var proxy = Object.assign({}, opts); + + // if `true`, then connect to the proxy server over TLS. defaults to `false`. + this.secureProxy = proxy.protocol + ? /^https:?$/i.test(proxy.protocol) + : false; + + // prefer `hostname` over `host`, and set the `port` if needed + proxy.host = proxy.hostname || proxy.host; + proxy.port = +proxy.port || (this.secureProxy ? 443 : 80); + + // ALPN is supported by Node.js >= v5. + // attempt to negotiate http/1.1 for proxy servers that support http/2 + if (this.secureProxy && !('ALPNProtocols' in proxy)) { + proxy.ALPNProtocols = ['http 1.1']; + } + + if (proxy.host && proxy.path) { + // if both a `host` and `path` are specified then it's most likely the + // result of a `url.parse()` call... we need to remove the `path` portion so + // that `net.connect()` doesn't attempt to open that as a unix socket file. + delete proxy.path; + delete proxy.pathname; + } + + this.proxy = proxy; + this.defaultPort = 443; } inherits(HttpsProxyAgent, Agent); @@ -68,161 +70,161 @@ inherits(HttpsProxyAgent, Agent); */ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { - var proxy = this.proxy; - - // create a socket connection to the proxy server - var socket; - if (this.secureProxy) { - socket = tls.connect(proxy); - } else { - socket = net.connect(proxy); - } - - // we need to buffer any HTTP traffic that happens with the proxy before we get - // the CONNECT response, so that if the response is anything other than an "200" - // response code, then we can re-play the "data" events on the socket once the - // HTTP parser is hooked up... - var buffers = []; - var buffersLength = 0; - - function read() { - var b = socket.read(); - if (b) ondata(b); - else socket.once('readable', read); - } - - function cleanup() { - socket.removeListener('end', onend); - socket.removeListener('error', onerror); - socket.removeListener('close', onclose); - socket.removeListener('readable', read); - } - - function onclose(err) { - debug('onclose had error %o', err); - } - - function onend() { - debug('onend'); - } - - function onerror(err) { - cleanup(); - fn(err); - } - - function ondata(b) { - buffers.push(b); - buffersLength += b.length; - var buffered = Buffer.concat(buffers, buffersLength); - var str = buffered.toString('ascii'); - - if (!~str.indexOf('\r\n\r\n')) { - // keep buffering - debug('have not received end of HTTP headers yet...'); - read(); - return; - } - - var firstLine = str.substring(0, str.indexOf('\r\n')); - var statusCode = +firstLine.split(' ')[1]; - debug('got proxy server response: %o', firstLine); - - if (200 == statusCode) { - // 200 Connected status code! - var sock = socket; - - // nullify the buffered data since we won't be needing it - buffers = buffered = null; - - if (opts.secureEndpoint) { - // since the proxy is connecting to an SSL server, we have - // to upgrade this socket connection to an SSL connection - debug( - 'upgrading proxy-connected socket to TLS connection: %o', - opts.host - ); - opts.socket = socket; - opts.servername = opts.servername || opts.host; - opts.host = null; - opts.hostname = null; - opts.port = null; - sock = tls.connect(opts); - } - - cleanup(); - req.once('socket', resume); - fn(null, sock); - } else { - // some other status code that's not 200... need to re-play the HTTP header - // "data" events onto the socket once the HTTP machinery is attached so - // that the node core `http` can parse and handle the error status code - cleanup(); - - // the original socket is closed, and a "fake socket" EventEmitter is - // returned instead, so that the proxy doesn't get the HTTP request - // written to it (which may contain `Authorization` headers or other - // sensitive data). - // - // See: https://hackerone.com/reports/541502 - socket.destroy(); - socket = new events.EventEmitter(); - - // save a reference to the concat'd Buffer for the `onsocket` callback - buffers = buffered; - - // need to wait for the "socket" event to re-play the "data" events - req.once('socket', onsocket); - - fn(null, socket); - } - } - - function onsocket(socket) { - debug('replaying proxy buffer for failed request'); - - // replay the "buffers" Buffer onto the `socket`, since at this point - // the HTTP module machinery has been hooked up for the user - if (socket.listenerCount('data') > 0) { - socket.emit('data', buffers); - } else { - // never? - throw new Error('should not happen...'); - } - - // nullify the cached Buffer instance - buffers = null; - } - - socket.on('error', onerror); - socket.on('close', onclose); - socket.on('end', onend); - - read(); - - var hostname = opts.host + ':' + opts.port; - var msg = 'CONNECT ' + hostname + ' HTTP/1.1\r\n'; - - var headers = Object.assign({}, proxy.headers); - if (proxy.auth) { - headers['Proxy-Authorization'] = - 'Basic ' + Buffer.from(proxy.auth).toString('base64'); - } - - // the Host header should only include the port - // number when it is a non-standard port - var host = opts.host; - if (!isDefaultPort(opts.port, opts.secureEndpoint)) { - host += ':' + opts.port; - } - headers['Host'] = host; - - headers['Connection'] = 'close'; - Object.keys(headers).forEach(function(name) { - msg += name + ': ' + headers[name] + '\r\n'; - }); - - socket.write(msg + '\r\n'); + var proxy = this.proxy; + + // create a socket connection to the proxy server + var socket; + if (this.secureProxy) { + socket = tls.connect(proxy); + } else { + socket = net.connect(proxy); + } + + // we need to buffer any HTTP traffic that happens with the proxy before we get + // the CONNECT response, so that if the response is anything other than an "200" + // response code, then we can re-play the "data" events on the socket once the + // HTTP parser is hooked up... + var buffers = []; + var buffersLength = 0; + + function read() { + var b = socket.read(); + if (b) ondata(b); + else socket.once('readable', read); + } + + function cleanup() { + socket.removeListener('end', onend); + socket.removeListener('error', onerror); + socket.removeListener('close', onclose); + socket.removeListener('readable', read); + } + + function onclose(err) { + debug('onclose had error %o', err); + } + + function onend() { + debug('onend'); + } + + function onerror(err) { + cleanup(); + fn(err); + } + + function ondata(b) { + buffers.push(b); + buffersLength += b.length; + var buffered = Buffer.concat(buffers, buffersLength); + var str = buffered.toString('ascii'); + + if (!~str.indexOf('\r\n\r\n')) { + // keep buffering + debug('have not received end of HTTP headers yet...'); + read(); + return; + } + + var firstLine = str.substring(0, str.indexOf('\r\n')); + var statusCode = +firstLine.split(' ')[1]; + debug('got proxy server response: %o', firstLine); + + if (200 == statusCode) { + // 200 Connected status code! + var sock = socket; + + // nullify the buffered data since we won't be needing it + buffers = buffered = null; + + if (opts.secureEndpoint) { + // since the proxy is connecting to an SSL server, we have + // to upgrade this socket connection to an SSL connection + debug( + 'upgrading proxy-connected socket to TLS connection: %o', + opts.host + ); + opts.socket = socket; + opts.servername = opts.servername || opts.host; + opts.host = null; + opts.hostname = null; + opts.port = null; + sock = tls.connect(opts); + } + + cleanup(); + req.once('socket', resume); + fn(null, sock); + } else { + // some other status code that's not 200... need to re-play the HTTP header + // "data" events onto the socket once the HTTP machinery is attached so + // that the node core `http` can parse and handle the error status code + cleanup(); + + // the original socket is closed, and a "fake socket" EventEmitter is + // returned instead, so that the proxy doesn't get the HTTP request + // written to it (which may contain `Authorization` headers or other + // sensitive data). + // + // See: https://hackerone.com/reports/541502 + socket.destroy(); + socket = new events.EventEmitter(); + + // save a reference to the concat'd Buffer for the `onsocket` callback + buffers = buffered; + + // need to wait for the "socket" event to re-play the "data" events + req.once('socket', onsocket); + + fn(null, socket); + } + } + + function onsocket(socket) { + debug('replaying proxy buffer for failed request'); + + // replay the "buffers" Buffer onto the `socket`, since at this point + // the HTTP module machinery has been hooked up for the user + if (socket.listenerCount('data') > 0) { + socket.emit('data', buffers); + } else { + // never? + throw new Error('should not happen...'); + } + + // nullify the cached Buffer instance + buffers = null; + } + + socket.on('error', onerror); + socket.on('close', onclose); + socket.on('end', onend); + + read(); + + var hostname = opts.host + ':' + opts.port; + var msg = 'CONNECT ' + hostname + ' HTTP/1.1\r\n'; + + var headers = Object.assign({}, proxy.headers); + if (proxy.auth) { + headers['Proxy-Authorization'] = + 'Basic ' + Buffer.from(proxy.auth).toString('base64'); + } + + // the Host header should only include the port + // number when it is a non-standard port + var host = opts.host; + if (!isDefaultPort(opts.port, opts.secureEndpoint)) { + host += ':' + opts.port; + } + headers['Host'] = host; + + headers['Connection'] = 'close'; + Object.keys(headers).forEach(function(name) { + msg += name + ': ' + headers[name] + '\r\n'; + }); + + socket.write(msg + '\r\n'); }; /** @@ -233,9 +235,9 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { */ function resume(socket) { - socket.resume(); + socket.resume(); } function isDefaultPort(port, secure) { - return Boolean((!secure && port === 80) || (secure && port === 443)); + return Boolean((!secure && port === 80) || (secure && port === 443)); } diff --git a/test/test.js b/test/test.js index b3684958..513aae0a 100644 --- a/test/test.js +++ b/test/test.js @@ -1,4 +1,3 @@ - /** * Module dependencies. */ @@ -11,332 +10,362 @@ var assert = require('assert'); var Proxy = require('proxy'); var HttpsProxyAgent = require('../'); -describe('HttpsProxyAgent', function () { - - var server; - var serverPort; - - var sslServer; - var sslServerPort; - - var proxy; - var proxyPort; - - var sslProxy; - var sslProxyPort; - - before(function (done) { - // setup target HTTP server - server = http.createServer(); - server.listen(function () { - serverPort = server.address().port; - done(); - }); - }); - - before(function (done) { - // setup HTTP proxy server - proxy = Proxy(); - proxy.listen(function () { - proxyPort = proxy.address().port; - done(); - }); - }); - - before(function (done) { - // setup target HTTPS server - var options = { - key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'), - cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem') - }; - sslServer = https.createServer(options); - sslServer.listen(function () { - sslServerPort = sslServer.address().port; - done(); - }); - }); - - before(function (done) { - // setup SSL HTTP proxy server - var options = { - key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'), - cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem') - }; - sslProxy = Proxy(https.createServer(options)); - sslProxy.listen(function () { - sslProxyPort = sslProxy.address().port; - done(); - }); - }); - - // shut down test HTTP server - after(function (done) { - server.once('close', function () { done(); }); - server.close(); - }); - - after(function (done) { - proxy.once('close', function () { done(); }); - proxy.close(); - }); - - after(function (done) { - sslServer.once('close', function () { done(); }); - sslServer.close(); - }); - - after(function (done) { - sslProxy.once('close', function () { done(); }); - sslProxy.close(); - }); - - describe('constructor', function () { - it('should throw an Error if no "proxy" argument is given', function () { - assert.throws(function () { - new HttpsProxyAgent(); - }); - }); - it('should accept a "string" proxy argument', function () { - var agent = new HttpsProxyAgent('http://127.0.0.1:' + proxyPort); - assert.equal('127.0.0.1', agent.proxy.host); - assert.equal(proxyPort, agent.proxy.port); - }); - it('should accept a `url.parse()` result object argument', function () { - var opts = url.parse('http://127.0.0.1:' + proxyPort); - var agent = new HttpsProxyAgent(opts); - 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 }); - assert.equal(false, agent.secureProxy); - }); - it('should be `false` when "http:" protocol is used', function () { - var agent = new HttpsProxyAgent({ port: proxyPort, protocol: 'http:' }); - assert.equal(false, agent.secureProxy); - }); - it('should be `true` when "https:" protocol is used', function () { - var agent = new HttpsProxyAgent({ port: proxyPort, protocol: 'https:' }); - assert.equal(true, agent.secureProxy); - }); - it('should be `true` when "https" protocol is used', function () { - var agent = new HttpsProxyAgent({ port: proxyPort, protocol: 'https' }); - assert.equal(true, agent.secureProxy); - }); - }); - }); - - describe('"http" module', function () { - - beforeEach(function () { - delete proxy.authenticate; - }); - - it('should work over an HTTP proxy', function (done) { - server.once('request', function (req, res) { - res.end(JSON.stringify(req.headers)); - }); - - var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; - var agent = new HttpsProxyAgent(proxy); - - var opts = url.parse('http://127.0.0.1:' + serverPort); - opts.agent = agent; - - var req = http.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:' + serverPort, data.host); - done(); - }); - }); - req.once('error', done); - }); - it('should work over an HTTPS proxy', function (done) { - server.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); - - var opts = url.parse('http://127.0.0.1:' + serverPort); - opts.agent = agent; - - http.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:' + serverPort, data.host); - done(); - }); - }); - }); - it('should receive the 407 authorization code on the `http.ClientResponse`', function (done) { - // set a proxy authentication function for this test - proxy.authenticate = function (req, fn) { - // reject all requests - fn(null, false); - }; - - var proxyUri = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; - var agent = new HttpsProxyAgent(proxyUri); - - var opts = {}; - // `host` and `port` don't really matter since the proxy will reject anyways - opts.host = '127.0.0.1'; - opts.port = 80; - opts.agent = agent; - - var req = http.get(opts, function (res) { - assert.equal(407, res.statusCode); - assert('proxy-authenticate' in res.headers); - done(); - }); - }); - it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', function (done) { - // port 4 is a reserved, but "unassigned" port - var proxyUri = 'http://127.0.0.1:4'; - var agent = new HttpsProxyAgent(proxyUri); - - var opts = url.parse('http://nodejs.org'); - opts.agent = agent; - - var req = http.get(opts); - req.once('error', function (err) { - assert.equal('ECONNREFUSED', err.code); - req.abort(); - done(); - }); - }); - - it('should allow custom proxy "headers"', function (done) { - server.once('connect', function (req, socket, head) { - assert.equal('CONNECT', req.method); - assert.equal('bar', req.headers.foo); - socket.destroy(); - done(); - }); - - var uri = 'http://127.0.0.1:' + serverPort; - var proxyOpts = url.parse(uri); - proxyOpts.headers = { - 'Foo': 'bar' - }; - var agent = new HttpsProxyAgent(proxyOpts); - - var opts = {}; - // `host` and `port` don't really matter since the proxy will reject anyways - opts.host = '127.0.0.1'; - opts.port = 80; - opts.agent = agent; - - http.get(opts); - }); - - }); - - describe('"https" module', function () { - it('should work over an HTTP proxy', function (done) { - sslServer.once('request', function (req, res) { - res.end(JSON.stringify(req.headers)); - }); - - var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; - var agent = new HttpsProxyAgent(proxy); - - var opts = url.parse('https://127.0.0.1:' + sslServerPort); - opts.rejectUnauthorized = false; - opts.agent = agent; - - 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:' + sslServerPort, data.host); - done(); - }); - }); - }); - - it('should work over an HTTPS proxy', 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); - - 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:' + sslServerPort, data.host); - done(); - }); - }); - }); - - 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(); - }); - }); - }); - - }); - +describe('HttpsProxyAgent', function() { + var server; + var serverPort; + + var sslServer; + var sslServerPort; + + var proxy; + var proxyPort; + + var sslProxy; + var sslProxyPort; + + before(function(done) { + // setup target HTTP server + server = http.createServer(); + server.listen(function() { + serverPort = server.address().port; + done(); + }); + }); + + before(function(done) { + // setup HTTP proxy server + proxy = Proxy(); + proxy.listen(function() { + proxyPort = proxy.address().port; + done(); + }); + }); + + before(function(done) { + // setup target HTTPS server + var options = { + key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'), + cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem') + }; + sslServer = https.createServer(options); + sslServer.listen(function() { + sslServerPort = sslServer.address().port; + done(); + }); + }); + + before(function(done) { + // setup SSL HTTP proxy server + var options = { + key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'), + cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem') + }; + sslProxy = Proxy(https.createServer(options)); + sslProxy.listen(function() { + sslProxyPort = sslProxy.address().port; + done(); + }); + }); + + // shut down test HTTP server + after(function(done) { + server.once('close', function() { + done(); + }); + server.close(); + }); + + after(function(done) { + proxy.once('close', function() { + done(); + }); + proxy.close(); + }); + + after(function(done) { + sslServer.once('close', function() { + done(); + }); + sslServer.close(); + }); + + after(function(done) { + sslProxy.once('close', function() { + done(); + }); + sslProxy.close(); + }); + + describe('constructor', function() { + it('should throw an Error if no "proxy" argument is given', function() { + assert.throws(function() { + new HttpsProxyAgent(); + }); + }); + it('should accept a "string" proxy argument', function() { + var agent = new HttpsProxyAgent('http://127.0.0.1:' + proxyPort); + assert.equal('127.0.0.1', agent.proxy.host); + assert.equal(proxyPort, agent.proxy.port); + }); + it('should accept a `url.parse()` result object argument', function() { + var opts = url.parse('http://127.0.0.1:' + proxyPort); + var agent = new HttpsProxyAgent(opts); + 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 }); + assert.equal(false, agent.secureProxy); + }); + it('should be `false` when "http:" protocol is used', function() { + var agent = new HttpsProxyAgent({ + port: proxyPort, + protocol: 'http:' + }); + assert.equal(false, agent.secureProxy); + }); + it('should be `true` when "https:" protocol is used', function() { + var agent = new HttpsProxyAgent({ + port: proxyPort, + protocol: 'https:' + }); + assert.equal(true, agent.secureProxy); + }); + it('should be `true` when "https" protocol is used', function() { + var agent = new HttpsProxyAgent({ + port: proxyPort, + protocol: 'https' + }); + assert.equal(true, agent.secureProxy); + }); + }); + }); + + describe('"http" module', function() { + beforeEach(function() { + delete proxy.authenticate; + }); + + it('should work over an HTTP proxy', function(done) { + server.once('request', function(req, res) { + res.end(JSON.stringify(req.headers)); + }); + + var proxy = + process.env.HTTP_PROXY || + process.env.http_proxy || + 'http://127.0.0.1:' + proxyPort; + var agent = new HttpsProxyAgent(proxy); + + var opts = url.parse('http://127.0.0.1:' + serverPort); + opts.agent = agent; + + var req = http.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:' + serverPort, data.host); + done(); + }); + }); + req.once('error', done); + }); + it('should work over an HTTPS proxy', function(done) { + server.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); + + var opts = url.parse('http://127.0.0.1:' + serverPort); + opts.agent = agent; + + http.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:' + serverPort, data.host); + done(); + }); + }); + }); + it('should receive the 407 authorization code on the `http.ClientResponse`', function(done) { + // set a proxy authentication function for this test + proxy.authenticate = function(req, fn) { + // reject all requests + fn(null, false); + }; + + var proxyUri = + process.env.HTTP_PROXY || + process.env.http_proxy || + 'http://127.0.0.1:' + proxyPort; + var agent = new HttpsProxyAgent(proxyUri); + + var opts = {}; + // `host` and `port` don't really matter since the proxy will reject anyways + opts.host = '127.0.0.1'; + opts.port = 80; + opts.agent = agent; + + var req = http.get(opts, function(res) { + assert.equal(407, res.statusCode); + assert('proxy-authenticate' in res.headers); + done(); + }); + }); + it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', function(done) { + // port 4 is a reserved, but "unassigned" port + var proxyUri = 'http://127.0.0.1:4'; + var agent = new HttpsProxyAgent(proxyUri); + + var opts = url.parse('http://nodejs.org'); + opts.agent = agent; + + var req = http.get(opts); + req.once('error', function(err) { + assert.equal('ECONNREFUSED', err.code); + req.abort(); + done(); + }); + }); + + it('should allow custom proxy "headers"', function(done) { + server.once('connect', function(req, socket, head) { + assert.equal('CONNECT', req.method); + assert.equal('bar', req.headers.foo); + socket.destroy(); + done(); + }); + + var uri = 'http://127.0.0.1:' + serverPort; + var proxyOpts = url.parse(uri); + proxyOpts.headers = { + Foo: 'bar' + }; + var agent = new HttpsProxyAgent(proxyOpts); + + var opts = {}; + // `host` and `port` don't really matter since the proxy will reject anyways + opts.host = '127.0.0.1'; + opts.port = 80; + opts.agent = agent; + + http.get(opts); + }); + }); + + describe('"https" module', function() { + it('should work over an HTTP proxy', function(done) { + sslServer.once('request', function(req, res) { + res.end(JSON.stringify(req.headers)); + }); + + var proxy = + process.env.HTTP_PROXY || + process.env.http_proxy || + 'http://127.0.0.1:' + proxyPort; + var agent = new HttpsProxyAgent(proxy); + + var opts = url.parse('https://127.0.0.1:' + sslServerPort); + opts.rejectUnauthorized = false; + opts.agent = agent; + + 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:' + sslServerPort, data.host); + done(); + }); + }); + }); + + it('should work over an HTTPS proxy', 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); + + 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:' + sslServerPort, data.host); + done(); + }); + }); + }); + + 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(); + }); + }); + }); + }); });