test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. done();
  76. });
  77. after(function (done) {
  78. //httpServer.once('close', function () { done(); });
  79. httpServer.close();
  80. done();
  81. });
  82. after(function (done) {
  83. //httpsServer.once('close', function () { done(); });
  84. httpsServer.close();
  85. done();
  86. });
  87. after(function (done) {
  88. //proxyServer.once('close', function () { done(); });
  89. proxyServer.close();
  90. done();
  91. });
  92. after(function (done) {
  93. //proxyHttpsServer.once('close', function () { done(); });
  94. proxyHttpsServer.close();
  95. done();
  96. });
  97. it('should export a "function"', function () {
  98. assert.equal('function', typeof ProxyAgent);
  99. });
  100. describe('constructor', function () {
  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 "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://localhost:' + proxyPort;
  128. var agent = new ProxyAgent(uri);
  129. var opts = url.parse('http://localhost:' + 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('localhost:' + httpPort, data.host);
  136. assert('via' in data);
  137. done();
  138. });
  139. });
  140. req.once('error', done);
  141. });
  142. });
  143. describe('over "http" proxy from env', function () {
  144. it('should work', function (done) {
  145. httpServer.once('request', function (req, res) {
  146. res.end(JSON.stringify(req.headers));
  147. });
  148. process.env.HTTP_PROXY = 'http://localhost:' + proxyPort;
  149. var agent = new ProxyAgent();
  150. var opts = url.parse('http://localhost:' + httpPort + '/test');
  151. opts.agent = agent;
  152. var req = http.get(opts, function (res) {
  153. toBuffer(res, function (err, buf) {
  154. if (err) return done(err);
  155. var data = JSON.parse(buf.toString('utf8'));
  156. assert.equal('localhost:' + httpPort, data.host);
  157. assert('via' in data);
  158. done();
  159. });
  160. });
  161. req.once('error', done);
  162. });
  163. });
  164. describe('with no proxy from env', function () {
  165. it('should work', function (done) {
  166. httpServer.once('request', function (req, res) {
  167. res.end(JSON.stringify(req.headers));
  168. });
  169. process.env.NO_PROXY = '*';
  170. var agent = new ProxyAgent();
  171. var opts = url.parse('http://localhost:' + httpPort + '/test');
  172. opts.agent = agent;
  173. var req = http.get(opts, function (res) {
  174. toBuffer(res, function (err, buf) {
  175. if (err) return done(err);
  176. var data = JSON.parse(buf.toString('utf8'));
  177. assert.equal('localhost:' + httpPort, data.host);
  178. assert(!('via' in data));
  179. done();
  180. });
  181. });
  182. req.once('error', done);
  183. });
  184. });
  185. describe('over "https" proxy', function () {
  186. it('should work', function (done) {
  187. httpServer.once('request', function (req, res) {
  188. res.end(JSON.stringify(req.headers));
  189. });
  190. var uri = 'https://localhost:' + proxyHttpsPort;
  191. var proxy = url.parse(uri);
  192. proxy.rejectUnauthorized = false;
  193. var agent = new ProxyAgent(proxy);
  194. var opts = url.parse('http://localhost:' + httpPort + '/test');
  195. opts.agent = agent;
  196. var req = http.get(opts, function (res) {
  197. toBuffer(res, function (err, buf) {
  198. if (err) return done(err);
  199. var data = JSON.parse(buf.toString('utf8'));
  200. assert.equal('localhost:' + httpPort, data.host);
  201. assert('via' in data);
  202. done();
  203. });
  204. });
  205. req.once('error', done);
  206. });
  207. });
  208. describe('over "socks" proxy', function () {
  209. it('should work', function (done) {
  210. httpServer.once('request', function (req, res) {
  211. res.end(JSON.stringify(req.headers));
  212. });
  213. var uri = 'socks://localhost:' + socksPort;
  214. var agent = new ProxyAgent(uri);
  215. var opts = url.parse('http://localhost:' + httpPort + '/test');
  216. opts.agent = agent;
  217. var req = http.get(opts, function (res) {
  218. toBuffer(res, function (err, buf) {
  219. if (err) return done(err);
  220. var data = JSON.parse(buf.toString('utf8'));
  221. assert.equal('localhost:' + httpPort, data.host);
  222. done();
  223. });
  224. });
  225. req.once('error', done);
  226. });
  227. });
  228. });
  229. describe('"https" module', function () {
  230. describe('over "http" proxy', function () {
  231. it('should work', function (done) {
  232. httpsServer.once('request', function (req, res) {
  233. res.end(JSON.stringify(req.headers));
  234. });
  235. var uri = 'http://localhost:' + proxyPort;
  236. var agent = new ProxyAgent(uri);
  237. var opts = url.parse('https://localhost:' + httpsPort + '/test');
  238. opts.agent = agent;
  239. opts.rejectUnauthorized = false;
  240. var req = https.get(opts, function (res) {
  241. toBuffer(res, function (err, buf) {
  242. if (err) return done(err);
  243. var data = JSON.parse(buf.toString('utf8'));
  244. assert.equal('localhost:' + httpsPort, data.host);
  245. done();
  246. });
  247. });
  248. req.once('error', done);
  249. });
  250. });
  251. describe('over "https" proxy', function () {
  252. it('should work', function (done) {
  253. var gotReq = false;
  254. httpsServer.once('request', function (req, res) {
  255. gotReq = true;
  256. res.end(JSON.stringify(req.headers));
  257. });
  258. var agent = new ProxyAgent({
  259. protocol: 'https:',
  260. host: 'localhost',
  261. port: proxyHttpsPort,
  262. rejectUnauthorized: false
  263. });
  264. var opts = url.parse('https://localhost:' + httpsPort + '/test');
  265. opts.agent = agent;
  266. opts.rejectUnauthorized = false;
  267. var req = https.get(opts, function (res) {
  268. toBuffer(res, function (err, buf) {
  269. if (err) return done(err);
  270. var data = JSON.parse(buf.toString('utf8'));
  271. assert.equal('localhost:' + httpsPort, data.host);
  272. assert(gotReq);
  273. done();
  274. });
  275. });
  276. req.once('error', done);
  277. });
  278. });
  279. describe('over "socks" proxy', function () {
  280. it('should work', function (done) {
  281. var gotReq = false;
  282. httpsServer.once('request', function (req, res) {
  283. gotReq = true;
  284. res.end(JSON.stringify(req.headers));
  285. });
  286. var uri = 'socks://localhost:' + socksPort;
  287. var agent = new ProxyAgent(uri);
  288. var opts = url.parse('https://localhost:' + httpsPort + '/test');
  289. opts.agent = agent;
  290. opts.rejectUnauthorized = false;
  291. var req = https.get(opts, function (res) {
  292. toBuffer(res, function (err, buf) {
  293. if (err) return done(err);
  294. var data = JSON.parse(buf.toString('utf8'));
  295. assert.equal('localhost:' + httpsPort, data.host);
  296. assert(gotReq);
  297. done();
  298. });
  299. });
  300. req.once('error', done);
  301. });
  302. });
  303. });
  304. });