123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /**
- * 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 PacProxyAgent = require('../');
- describe('PacProxyAgent', 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();
- });
- after(function (done) {
- httpServer.once('close', function () { done(); });
- httpServer.close();
- });
- after(function (done) {
- httpsServer.once('close', function () { done(); });
- httpsServer.close();
- });
- after(function (done) {
- proxyServer.once('close', function () { done(); });
- proxyServer.close();
- });
- after(function (done) {
- proxyHttpsServer.once('close', function () { done(); });
- proxyHttpsServer.close();
- });
- describe('constructor', function () {
- it('should throw an Error if no "proxy" argument is given', function () {
- assert.throws(function () {
- new PacProxyAgent();
- });
- });
- it('should accept a "string" proxy argument', function () {
- var agent = new PacProxyAgent('pac+ftp://example.com/proxy.pac');
- assert.equal('ftp://example.com/proxy.pac', agent.uri);
- });
- it('should accept a `url.parse()` result object argument', function () {
- var opts = url.parse('pac+ftp://example.com/proxy.pac');
- var agent = new PacProxyAgent(opts);
- assert.equal('ftp://example.com/proxy.pac', agent.uri);
- });
- it('should accept a `uri` on the options object', function () {
- var agent = new PacProxyAgent({ uri: 'pac+ftp://example.com/proxy.pac' });
- assert.equal('ftp://example.com/proxy.pac', agent.uri);
- });
- });
- describe('"http" module', function () {
- it('should work over an HTTP proxy', function (done) {
- httpServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- function FindProxyForURL(url, host) {
- return "PROXY 127.0.0.1:PORT;"
- }
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyPort));
- var agent = new PacProxyAgent(uri);
- var opts = url.parse('http://127.0.0.1:' + 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('127.0.0.1:' + httpPort, data.host);
- assert('via' in data);
- done();
- });
- });
- req.once('error', done);
- });
- it('should work over an HTTPS proxy', function (done) {
- httpServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- function FindProxyForURL(url, host) {
- return "HTTPS 127.0.0.1:PORT;"
- }
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyHttpsPort));
- var proxy = url.parse(uri);
- proxy.rejectUnauthorized = false;
- var agent = new PacProxyAgent(proxy);
- var opts = url.parse('http://127.0.0.1:' + 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('127.0.0.1:' + httpPort, data.host);
- assert('via' in data);
- done();
- });
- });
- req.once('error', done);
- });
- it('should work over a SOCKS proxy', function (done) {
- httpServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- function FindProxyForURL(url, host) {
- return "SOCKS 127.0.0.1:PORT;"
- }
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', socksPort));
- var agent = new PacProxyAgent(uri);
- var opts = url.parse('http://127.0.0.1:' + 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('127.0.0.1:' + httpPort, data.host);
- done();
- });
- });
- req.once('error', done);
- });
- });
- describe('"https" module', function () {
- it('should work over an HTTP proxy', function (done) {
- httpsServer.once('request', function (req, res) {
- res.end(JSON.stringify(req.headers));
- });
- function FindProxyForURL(url, host) {
- return "PROXY 127.0.0.1:PORT;"
- }
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyPort));
- var agent = new PacProxyAgent(uri);
- var opts = url.parse('https://127.0.0.1:' + 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('127.0.0.1:' + httpsPort, data.host);
- done();
- });
- });
- req.once('error', done);
- });
- it('should work over an HTTPS proxy', function (done) {
- var gotReq = false;
- httpsServer.once('request', function (req, res) {
- gotReq = true;
- res.end(JSON.stringify(req.headers));
- });
- function FindProxyForURL(url, host) {
- return "HTTPS 127.0.0.1:PORT;"
- }
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyHttpsPort));
- var agent = new PacProxyAgent(uri, {
- rejectUnauthorized: false
- });
- var opts = url.parse('https://127.0.0.1:' + 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('127.0.0.1:' + httpsPort, data.host);
- assert(gotReq);
- done();
- });
- });
- req.once('error', done);
- });
- it('should work over a SOCKS proxy', function (done) {
- var gotReq = false;
- httpsServer.once('request', function (req, res) {
- gotReq = true;
- res.end(JSON.stringify(req.headers));
- });
- function FindProxyForURL(url, host) {
- return "SOCKS 127.0.0.1:PORT;"
- }
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', socksPort));
- var agent = new PacProxyAgent(uri);
- var opts = url.parse('https://127.0.0.1:' + 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('127.0.0.1:' + httpsPort, data.host);
- assert(gotReq);
- done();
- });
- });
- req.once('error', done);
- });
- });
- });
|