test.js 4.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

/**
 * Module dependencies.
 */

var fs = require('fs');
var url = require('url');
var http = require('http');
var https = require('https');
var assert = require('assert');
var socks = require('socksv5');
var getRawBody = require('raw-body');
var SocksProxyAgent = require('../');

describe('SocksProxyAgent', function () {
  var httpServer, httpPort;
  var httpsServer, httpsPort;
  var socksServer, socksPort;

  before(function (done) {
    // setup SOCKS proxy server
    socksServer = socks.createServer(function(info, accept, deny) {
      accept();
    });
    socksServer.listen(0, '127.0.0.1', function() {
      socksPort = socksServer.address().port;
      //console.log('SOCKS server listening on port %d', socksPort);
      done();
    });
    socksServer.useAuth(socks.auth.None());
    //socksServer.useAuth(socks.auth.UserPassword(function(user, password, cb) {
    //  cb(user === 'nodejs' && password === 'rules!');
    //}));
  });

  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();
    });
  });

  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();
  });

  describe('constructor', function () {
    it('should throw an Error if no "proxy" argument is given', function () {
      assert.throws(function () {
        new SocksProxyAgent();
      });
    });
    it('should accept a "string" proxy argument', function () {
      var agent = new SocksProxyAgent('socks://127.0.0.1:' + socksPort);
      assert.equal('127.0.0.1', agent.proxy.host);
      assert.equal(socksPort, agent.proxy.port);
    });
    it('should accept a `url.parse()` result object argument', function () {
      var opts = url.parse('socks://127.0.0.1:' + socksPort);
      var agent = new SocksProxyAgent(opts);
      assert.equal('127.0.0.1', agent.proxy.host);
      assert.equal(socksPort, agent.proxy.port);
    });
  });

  describe('"http" module', function () {
    it('should work against an HTTP endpoint', function (done) {
      httpServer.once('request', function (req, res) {
        assert.equal('/foo', req.url);
        res.statusCode = 404;
        res.end(JSON.stringify(req.headers));
      });

      var agent = new SocksProxyAgent('socks://127.0.0.1:' + socksPort);
      var opts = url.parse('http://127.0.0.1:' + httpPort + '/foo');
      opts.agent = agent;
      opts.headers = { foo: 'bar' };
      var req = http.get(opts, function (res) {
        assert.equal(404, res.statusCode);
        getRawBody(res, 'utf8', function (err, buf) {
          if (err) return done(err);
          var data = JSON.parse(buf);
          assert.equal('bar', data.foo);
          done();
        });
      });
      req.once('error', done);
    });
  });

  describe('"https" module', function () {
    it('should work against an HTTPS endpoint', function (done) {
      httpsServer.once('request', function (req, res) {
        assert.equal('/foo', req.url);
        res.statusCode = 404;
        res.end(JSON.stringify(req.headers));
      });

      var agent = new SocksProxyAgent('socks://127.0.0.1:' + socksPort);
      var opts = url.parse('https://127.0.0.1:' + httpsPort + '/foo');
      opts.agent = agent;
      opts.rejectUnauthorized = false;

      opts.headers = { foo: 'bar' };
      var req = https.get(opts, function (res) {
        assert.equal(404, res.statusCode);
        getRawBody(res, 'utf8', function (err, buf) {
          if (err) return done(err);
          var data = JSON.parse(buf);
          assert.equal('bar', data.foo);
          done();
        });
      });
      req.once('error', done);
    });
  });

});