test.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 toBuffer = require('stream-to-buffer');
  10. var Proxy = require('proxy');
  11. var socks = require('socksv5');
  12. var ProxyAgent = require('../');
  13. describe('ProxyAgent', function () {
  14. // target servers
  15. var httpServer, httpPort;
  16. var httpsServer, httpsPort;
  17. // proxy servers
  18. var socksServer, socksPort;
  19. var proxyServer, proxyPort;
  20. var proxyHttpsServer, proxyHttpsPort;
  21. before(function (done) {
  22. // setup target HTTP server
  23. httpServer = http.createServer();
  24. httpServer.listen(function () {
  25. httpPort = httpServer.address().port;
  26. done();
  27. });
  28. });
  29. before(function (done) {
  30. // setup target SSL HTTPS server
  31. var options = {
  32. key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
  33. cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
  34. };
  35. httpsServer = https.createServer(options);
  36. httpsServer.listen(function () {
  37. httpsPort = httpsServer.address().port;
  38. done();
  39. });
  40. });
  41. before(function (done) {
  42. // setup SOCKS proxy server
  43. socksServer = socks.createServer(function(info, accept, deny) {
  44. accept();
  45. });
  46. socksServer.listen(function() {
  47. socksPort = socksServer.address().port;
  48. done();
  49. });
  50. socksServer.useAuth(socks.auth.None());
  51. });
  52. before(function (done) {
  53. // setup HTTP proxy server
  54. proxyServer = Proxy();
  55. proxyServer.listen(function () {
  56. proxyPort = proxyServer.address().port;
  57. done();
  58. });
  59. });
  60. before(function (done) {
  61. // setup SSL HTTPS proxy server
  62. var options = {
  63. key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
  64. cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
  65. };
  66. proxyHttpsServer = Proxy(https.createServer(options));
  67. proxyHttpsServer.listen(function () {
  68. proxyHttpsPort = proxyHttpsServer.address().port;
  69. done();
  70. });
  71. });
  72. after(function (done) {
  73. socksServer.once('close', function () { done(); });
  74. socksServer.close();
  75. });
  76. after(function (done) {
  77. httpServer.once('close', function () { done(); });
  78. httpServer.close();
  79. });
  80. after(function (done) {
  81. httpsServer.once('close', function () { done(); });
  82. httpsServer.close();
  83. });
  84. after(function (done) {
  85. proxyServer.once('close', function () { done(); });
  86. proxyServer.close();
  87. });
  88. after(function (done) {
  89. proxyHttpsServer.once('close', function () { done(); });
  90. proxyHttpsServer.close();
  91. });
  92. it('should export a "function"', function () {
  93. assert.equal('function', typeof ProxyAgent);
  94. });
  95. describe('constructor', function () {
  96. it('should throw a TypeError if no "proxy" argument is given', function () {
  97. assert.throws(function () {
  98. new ProxyAgent();
  99. }, TypeError);
  100. });
  101. it('should throw a TypeError if no "protocol" is given', function () {
  102. assert.throws(function () {
  103. ProxyAgent({ host: 'foo.com', port: 3128 });
  104. }, function (e) {
  105. return 'TypeError' === e.name &&
  106. /must specify a string "protocol"/.test(e.message) &&
  107. /\bhttp\b/.test(e.message) &&
  108. /\bhttps\b/.test(e.message) &&
  109. /\bsocks\b/.test(e.message);
  110. });
  111. });
  112. it('should throw a TypeError for unsupported proxy protocols', function () {
  113. assert.throws(function () {
  114. ProxyAgent('bad://foo.com:8888');
  115. }, function (e) {
  116. return 'TypeError' === e.name &&
  117. /unsupported proxy protocol/.test(e.message);
  118. });
  119. });
  120. });
  121. describe('"http" module', function () {
  122. describe('over "http" proxy', function () {
  123. it('should work', function (done) {
  124. httpServer.once('request', function (req, res) {
  125. res.end(JSON.stringify(req.headers));
  126. });
  127. var uri = 'http://127.0.0.1:' + proxyPort;
  128. var agent = new ProxyAgent(uri);
  129. var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
  130. opts.agent = agent;
  131. var req = http.get(opts, function (res) {
  132. toBuffer(res, function (err, buf) {
  133. if (err) return done(err);
  134. var data = JSON.parse(buf.toString('utf8'));
  135. assert.equal('127.0.0.1:' + httpPort, data.host);
  136. assert('via' in data);
  137. done();
  138. });
  139. });
  140. req.once('error', done);
  141. });
  142. });
  143. describe('over "https" proxy', function () {
  144. it('should work', function (done) {
  145. httpServer.once('request', function (req, res) {
  146. res.end(JSON.stringify(req.headers));
  147. });
  148. var uri = 'https://127.0.0.1:' + proxyHttpsPort;
  149. var proxy = url.parse(uri);
  150. proxy.rejectUnauthorized = false;
  151. var agent = new ProxyAgent(proxy);
  152. var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
  153. opts.agent = agent;
  154. var req = http.get(opts, function (res) {
  155. toBuffer(res, function (err, buf) {
  156. if (err) return done(err);
  157. var data = JSON.parse(buf.toString('utf8'));
  158. assert.equal('127.0.0.1:' + httpPort, data.host);
  159. assert('via' in data);
  160. done();
  161. });
  162. });
  163. req.once('error', done);
  164. });
  165. });
  166. describe('over "socks" proxy', function () {
  167. it('should work', function (done) {
  168. httpServer.once('request', function (req, res) {
  169. res.end(JSON.stringify(req.headers));
  170. });
  171. var uri = 'socks://127.0.0.1:' + socksPort;
  172. var agent = new ProxyAgent(uri);
  173. var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
  174. opts.agent = agent;
  175. var req = http.get(opts, function (res) {
  176. toBuffer(res, function (err, buf) {
  177. if (err) return done(err);
  178. var data = JSON.parse(buf.toString('utf8'));
  179. assert.equal('127.0.0.1:' + httpPort, data.host);
  180. done();
  181. });
  182. });
  183. req.once('error', done);
  184. });
  185. });
  186. });
  187. describe('"https" module', function () {
  188. describe('over "http" proxy', function () {
  189. it('should work', function (done) {
  190. httpsServer.once('request', function (req, res) {
  191. res.end(JSON.stringify(req.headers));
  192. });
  193. var uri = 'http://127.0.0.1:' + proxyPort;
  194. var agent = new ProxyAgent(uri);
  195. var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
  196. opts.agent = agent;
  197. opts.rejectUnauthorized = false;
  198. var req = https.get(opts, function (res) {
  199. toBuffer(res, function (err, buf) {
  200. if (err) return done(err);
  201. var data = JSON.parse(buf.toString('utf8'));
  202. assert.equal('127.0.0.1:' + httpsPort, data.host);
  203. done();
  204. });
  205. });
  206. req.once('error', done);
  207. });
  208. });
  209. describe('over "https" proxy', function () {
  210. it('should work', function (done) {
  211. var gotReq = false;
  212. httpsServer.once('request', function (req, res) {
  213. gotReq = true;
  214. res.end(JSON.stringify(req.headers));
  215. });
  216. var agent = new ProxyAgent({
  217. protocol: 'https:',
  218. host: '127.0.0.1',
  219. port: proxyHttpsPort,
  220. rejectUnauthorized: false
  221. });
  222. var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
  223. opts.agent = agent;
  224. opts.rejectUnauthorized = false;
  225. var req = https.get(opts, function (res) {
  226. toBuffer(res, function (err, buf) {
  227. if (err) return done(err);
  228. var data = JSON.parse(buf.toString('utf8'));
  229. assert.equal('127.0.0.1:' + httpsPort, data.host);
  230. assert(gotReq);
  231. done();
  232. });
  233. });
  234. req.once('error', done);
  235. });
  236. });
  237. describe('over "socks" proxy', function () {
  238. it('should work', function (done) {
  239. var gotReq = false;
  240. httpsServer.once('request', function (req, res) {
  241. gotReq = true;
  242. res.end(JSON.stringify(req.headers));
  243. });
  244. var uri = 'socks://127.0.0.1:' + socksPort;
  245. var agent = new ProxyAgent(uri);
  246. var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
  247. opts.agent = agent;
  248. opts.rejectUnauthorized = false;
  249. var req = https.get(opts, function (res) {
  250. toBuffer(res, function (err, buf) {
  251. if (err) return done(err);
  252. var data = JSON.parse(buf.toString('utf8'));
  253. assert.equal('127.0.0.1:' + httpsPort, data.host);
  254. assert(gotReq);
  255. done();
  256. });
  257. });
  258. req.once('error', done);
  259. });
  260. });
  261. });
  262. });