123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- /**
- * Module dependencies.
- */
-
- var fs = require('fs');
- var url = require('url');
- var http = require('http');
- var https = require('https');
- var assert = require('assert');
- var getRawBody = require('raw-body');
- 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();
- 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 allow a `sandbox` to be passed in', function (done) {
- this.slow(1000);
-
- function FindProxyForURL(url, host) {
- throw new Error(foo() + bar());
- }
-
- function foo () {
- return 'hi';
- }
-
- function asyncBar(fn) {
- setTimeout(function () {
- fn(null, 'fooooo');
- }, 200);
- }
- asyncBar.async = true;
-
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString());
- var agent = new PacProxyAgent(uri, {
- sandbox: {
- foo: foo,
- bar: asyncBar
- }
- });
-
- var opts = url.parse('http://localhost:' + httpPort + '/test');
- opts.agent = agent;
-
- var req = http.get(opts);
- req.once('error', function (err) {
- assert.equal(err.message, 'hifooooo');
- done();
- });
- });
-
- 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 localhost:PORT;"
- }
-
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyPort));
- var agent = new PacProxyAgent(uri);
-
- var opts = url.parse('http://localhost:' + httpPort + '/test');
- opts.agent = agent;
-
- var req = http.get(opts, function (res) {
- getRawBody(res, 'utf8', function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf);
- assert.equal('localhost:' + 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 localhost: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://localhost:' + httpPort + '/test');
- opts.agent = agent;
-
- var req = http.get(opts, function (res) {
- getRawBody(res, 'utf8', function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf);
- assert.equal('localhost:' + 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 localhost:PORT;"
- }
-
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', socksPort));
- var agent = new PacProxyAgent(uri);
-
- var opts = url.parse('http://localhost:' + httpPort + '/test');
- opts.agent = agent;
-
- var req = http.get(opts, function (res) {
- getRawBody(res, 'utf8', function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf);
- assert.equal('localhost:' + 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 localhost:PORT;"
- }
-
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyPort));
- var agent = new PacProxyAgent(uri);
-
- var opts = url.parse('https://localhost:' + httpsPort + '/test');
- opts.agent = agent;
- opts.rejectUnauthorized = false;
-
- var req = https.get(opts, function (res) {
- getRawBody(res, 'utf8', function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf);
- assert.equal('localhost:' + 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 localhost:PORT;"
- }
-
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyHttpsPort));
- var agent = new PacProxyAgent(uri, {
- rejectUnauthorized: false
- });
-
- var opts = url.parse('https://localhost:' + httpsPort + '/test');
- opts.agent = agent;
- opts.rejectUnauthorized = false;
-
- var req = https.get(opts, function (res) {
- getRawBody(res, 'utf8', function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf);
- assert.equal('localhost:' + 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 localhost:PORT;"
- }
-
- var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', socksPort));
- var agent = new PacProxyAgent(uri);
-
- var opts = url.parse('https://localhost:' + httpsPort + '/test');
- opts.agent = agent;
- opts.rejectUnauthorized = false;
-
- var req = https.get(opts, function (res) {
- getRawBody(res, 'utf8', function (err, buf) {
- if (err) return done(err);
- var data = JSON.parse(buf);
- assert.equal('localhost:' + httpsPort, data.host);
- assert(gotReq);
- done();
- });
- });
- req.once('error', done);
- });
- });
-
- });
|