123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- /**
- * Module dependencies.
- */
- var fs = require('fs');
- var url = require('url');
- var http = require('http');
- var https = require('https');
- var assert = require('assert');
- var toBuffer = require('stream-to-buffer');
- var Proxy = require('proxy');
- var socks = require('socksv5');
- var ProxyAgent = require('../');
- describe('ProxyAgent', function () {
- // target servers
- var httpServer, httpPort;
- var httpsServer, httpsPort;
- // proxy servers
- var socksServer, socksPort;
- var proxyServer, proxyPort;
- var proxyHttpsServer, proxyHttpsPort;
- before(function (done) {
- // setup target HTTP server
- httpServer = http.createServer();
- httpServer.listen(function () {
- httpPort = httpServer.address().port;
- done();
- });
- });
- before(function (done) {
- // setup target SSL HTTPS server
- var options = {
- key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
- cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
- };
- httpsServer = https.createServer(options);
- httpsServer.listen(function () {
- httpsPort = httpsServer.address().port;
- done();
- });
- });
- before(function (done) {
- // setup SOCKS proxy server
- socksServer = socks.createServer(function(info, accept, deny) {
- accept();
- });
- socksServer.listen(function() {
- socksPort = socksServer.address().port;
- done();
- });
- socksServer.useAuth(socks.auth.None());
- });
- before(function (done) {
- // setup HTTP proxy server
- proxyServer = Proxy();
- proxyServer.listen(function () {
- proxyPort = proxyServer.address().port;
- done();
- });
- });
- before(function (done) {
- // setup SSL HTTPS proxy server
- var options = {
- key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
- cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
- };
- proxyHttpsServer = Proxy(https.createServer(options));
- proxyHttpsServer.listen(function () {
- proxyHttpsPort = proxyHttpsServer.address().port;
- done();
- });
- });
- after(function (done) {
- //socksServer.once('close', function () { done(); });
- socksServer.close();
- done();
- });
- after(function (done) {
- //httpServer.once('close', function () { done(); });
- httpServer.close();
- done();
- });
- after(function (done) {
- //httpsServer.once('close', function () { done(); });
- httpsServer.close();
- done();
- });
- after(function (done) {
- //proxyServer.once('close', function () { done(); });
- proxyServer.close();
- done();
- });
- after(function (done) {
- //proxyHttpsServer.once('close', function () { done(); });
- proxyHttpsServer.close();
- done();
- });
- it('should export a "function"', function () {
- assert.equal('function', typeof ProxyAgent);
- });
- describe('constructor', function () {
- it('should throw a TypeError if no "protocol" is given', function () {
- assert.throws(function () {
- ProxyAgent({ host: 'foo.com', port: 3128 });
- }, function (e) {
- return 'TypeError' === e.name &&
- /must specify a "protocol"/.test(e.message) &&
- /\bhttp\b/.test(e.message) &&
- /\bhttps\b/.test(e.message) &&
- /\bsocks\b/.test(e.message);
- });
- });
- it('should throw a TypeError for unsupported proxy protocols', function () {
- assert.throws(function () {
- ProxyAgent('bad://foo.com:8888');
- }, function (e) {
- return 'TypeError' === e.name &&
- /unsupported proxy protocol/.test(e.message);
- });
- });
- });
- describe('"http" module', function () {
- describe('over "http" proxy', function () {
- it('should work', function (done) {
- httpServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- var uri = 'http://localhost:' + proxyPort;
- var agent = new ProxyAgent(uri);
- var opts = url.parse('http://localhost:' + httpPort + '/test');
- opts.agent = agent;
- var req = http.get(opts, function (res) {
- toBuffer(res, function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf.toString('utf8'));
- assert.equal('localhost:' + httpPort, data.host);
- assert('via' in data);
- done();
- });
- });
- req.once('error', done);
- });
- });
- describe('over "http" proxy from env', function () {
- it('should work', function (done) {
- httpServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- process.env.HTTP_PROXY = 'http://localhost:' + proxyPort;
- var agent = new ProxyAgent();
- var opts = url.parse('http://localhost:' + httpPort + '/test');
- opts.agent = agent;
- var req = http.get(opts, function (res) {
- toBuffer(res, function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf.toString('utf8'));
- assert.equal('localhost:' + httpPort, data.host);
- assert('via' in data);
- done();
- });
- });
- req.once('error', done);
- });
- });
- describe('with no proxy from env', function () {
- it('should work', function (done) {
- httpServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- process.env.NO_PROXY = '*';
- var agent = new ProxyAgent();
- var opts = url.parse('http://localhost:' + httpPort + '/test');
- opts.agent = agent;
- var req = http.get(opts, function (res) {
- toBuffer(res, function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf.toString('utf8'));
- assert.equal('localhost:' + httpPort, data.host);
- assert(!('via' in data));
- done();
- });
- });
- req.once('error', done);
- });
- });
- describe('over "https" proxy', function () {
- it('should work', function (done) {
- httpServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- var uri = 'https://localhost:' + proxyHttpsPort;
- var proxy = url.parse(uri);
- proxy.rejectUnauthorized = false;
- var agent = new ProxyAgent(proxy);
- var opts = url.parse('http://localhost:' + httpPort + '/test');
- opts.agent = agent;
- var req = http.get(opts, function (res) {
- toBuffer(res, function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf.toString('utf8'));
- assert.equal('localhost:' + httpPort, data.host);
- assert('via' in data);
- done();
- });
- });
- req.once('error', done);
- });
- });
- describe('over "socks" proxy', function () {
- it('should work', function (done) {
- httpServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- var uri = 'socks://localhost:' + socksPort;
- var agent = new ProxyAgent(uri);
- var opts = url.parse('http://localhost:' + httpPort + '/test');
- opts.agent = agent;
- var req = http.get(opts, function (res) {
- toBuffer(res, function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf.toString('utf8'));
- assert.equal('localhost:' + httpPort, data.host);
- done();
- });
- });
- req.once('error', done);
- });
- });
- });
- describe('"https" module', function () {
- describe('over "http" proxy', function () {
- it('should work', function (done) {
- httpsServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- var uri = 'http://localhost:' + proxyPort;
- var agent = new ProxyAgent(uri);
- var opts = url.parse('https://localhost:' + httpsPort + '/test');
- opts.agent = agent;
- opts.rejectUnauthorized = false;
- var req = https.get(opts, function (res) {
- toBuffer(res, function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf.toString('utf8'));
- assert.equal('localhost:' + httpsPort, data.host);
- done();
- });
- });
- req.once('error', done);
- });
- });
- describe('over "https" proxy', function () {
- it('should work', function (done) {
- var gotReq = false;
- httpsServer.once('request', function (req, res) {
- gotReq = true;
- res.end(JSON.stringify(req.headers));
- });
- var agent = new ProxyAgent({
- protocol: 'https:',
- host: 'localhost',
- port: proxyHttpsPort,
- rejectUnauthorized: false
- });
- var opts = url.parse('https://localhost:' + httpsPort + '/test');
- opts.agent = agent;
- opts.rejectUnauthorized = false;
- var req = https.get(opts, function (res) {
- toBuffer(res, function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf.toString('utf8'));
- assert.equal('localhost:' + httpsPort, data.host);
- assert(gotReq);
- done();
- });
- });
- req.once('error', done);
- });
- });
- describe('over "socks" proxy', function () {
- it('should work', function (done) {
- var gotReq = false;
- httpsServer.once('request', function (req, res) {
- gotReq = true;
- res.end(JSON.stringify(req.headers));
- });
- var uri = 'socks://localhost:' + socksPort;
- var agent = new ProxyAgent(uri);
- var opts = url.parse('https://localhost:' + httpsPort + '/test');
- opts.agent = agent;
- opts.rejectUnauthorized = false;
- var req = https.get(opts, function (res) {
- toBuffer(res, function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf.toString('utf8'));
- assert.equal('localhost:' + httpsPort, data.host);
- assert(gotReq);
- done();
- });
- });
- req.once('error', done);
- });
- });
- });
- });
|