• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2/**
3 * Module dependencies.
4 */
5
6var fs = require('fs');
7var url = require('url');
8var http = require('http');
9var https = require('https');
10var assert = require('assert');
11var socks = require('socksv5');
12var getRawBody = require('raw-body');
13var SocksProxyAgent = require('../');
14
15describe('SocksProxyAgent', function () {
16  var httpServer, httpPort;
17  var httpsServer, httpsPort;
18  var socksServer, socksPort;
19
20  before(function (done) {
21    // setup SOCKS proxy server
22    socksServer = socks.createServer(function(info, accept, deny) {
23      accept();
24    });
25    socksServer.listen(0, '127.0.0.1', function() {
26      socksPort = socksServer.address().port;
27      //console.log('SOCKS server listening on port %d', socksPort);
28      done();
29    });
30    socksServer.useAuth(socks.auth.None());
31    //socksServer.useAuth(socks.auth.UserPassword(function(user, password, cb) {
32    //  cb(user === 'nodejs' && password === 'rules!');
33    //}));
34  });
35
36  before(function (done) {
37    // setup target HTTP server
38    httpServer = http.createServer();
39    httpServer.listen(function () {
40      httpPort = httpServer.address().port;
41      done();
42    });
43  });
44
45  before(function (done) {
46    // setup target SSL HTTPS server
47    var options = {
48      key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
49      cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
50    };
51    httpsServer = https.createServer(options);
52    httpsServer.listen(function () {
53      httpsPort = httpsServer.address().port;
54      done();
55    });
56  });
57
58  after(function (done) {
59    socksServer.once('close', function () { done(); });
60    socksServer.close();
61  });
62
63  after(function (done) {
64    httpServer.once('close', function () { done(); });
65    httpServer.close();
66  });
67
68  after(function (done) {
69    httpsServer.once('close', function () { done(); });
70    httpsServer.close();
71  });
72
73  describe('constructor', function () {
74    it('should throw an Error if no "proxy" argument is given', function () {
75      assert.throws(function () {
76        new SocksProxyAgent();
77      });
78    });
79    it('should accept a "string" proxy argument', function () {
80      var agent = new SocksProxyAgent('socks://127.0.0.1:' + socksPort);
81      assert.equal('127.0.0.1', agent.proxy.host);
82      assert.equal(socksPort, agent.proxy.port);
83    });
84    it('should accept a `url.parse()` result object argument', function () {
85      var opts = url.parse('socks://127.0.0.1:' + socksPort);
86      var agent = new SocksProxyAgent(opts);
87      assert.equal('127.0.0.1', agent.proxy.host);
88      assert.equal(socksPort, agent.proxy.port);
89    });
90  });
91
92  describe('"http" module', function () {
93    it('should work against an HTTP endpoint', function (done) {
94      httpServer.once('request', function (req, res) {
95        assert.equal('/foo', req.url);
96        res.statusCode = 404;
97        res.end(JSON.stringify(req.headers));
98      });
99
100      var agent = new SocksProxyAgent('socks://127.0.0.1:' + socksPort);
101      var opts = url.parse('http://127.0.0.1:' + httpPort + '/foo');
102      opts.agent = agent;
103      opts.headers = { foo: 'bar' };
104      var req = http.get(opts, function (res) {
105        assert.equal(404, res.statusCode);
106        getRawBody(res, 'utf8', function (err, buf) {
107          if (err) return done(err);
108          var data = JSON.parse(buf);
109          assert.equal('bar', data.foo);
110          done();
111        });
112      });
113      req.once('error', done);
114    });
115  });
116
117  describe('"https" module', function () {
118    it('should work against an HTTPS endpoint', function (done) {
119      httpsServer.once('request', function (req, res) {
120        assert.equal('/foo', req.url);
121        res.statusCode = 404;
122        res.end(JSON.stringify(req.headers));
123      });
124
125      var agent = new SocksProxyAgent('socks://127.0.0.1:' + socksPort);
126      var opts = url.parse('https://127.0.0.1:' + httpsPort + '/foo');
127      opts.agent = agent;
128      opts.rejectUnauthorized = false;
129
130      opts.headers = { foo: 'bar' };
131      var req = https.get(opts, function (res) {
132        assert.equal(404, res.statusCode);
133        getRawBody(res, 'utf8', function (err, buf) {
134          if (err) return done(err);
135          var data = JSON.parse(buf);
136          assert.equal('bar', data.foo);
137          done();
138        });
139      });
140      req.once('error', done);
141    });
142  });
143
144});
145