You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 4.3KB

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