123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- /**
- * Module dependencies.
- */
- var fs = require('fs');
- var url = require('url');
- var http = require('http');
- var https = require('https');
- var assert = require('assert');
- var Proxy = require('proxy');
- var Semver = require('semver');
- var version = new Semver(process.version);
- 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);
- });
- 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);
- console.log(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();
- });
- });
- });
- 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();
- });
- });
- });
- if (version.compare('0.11.3') < 0 || version.compare('0.11.8') >= 0) {
- // This test is disabled on node >= 0.11.3 && < 0.11.8, since it segfaults :(
- // See: https://github.com/joyent/node/issues/6204
- 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();
- });
- });
- });
- } else {
- it('should work over an HTTPS proxy');
- }
- });
- });
|