test.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 Proxy = require('proxy');
  10. var HttpsProxyAgent = require('../');
  11. describe('HttpsProxyAgent', function () {
  12. var server;
  13. var serverPort;
  14. var sslServer;
  15. var sslServerPort;
  16. var proxy;
  17. var proxyPort;
  18. var sslProxy;
  19. var sslProxyPort;
  20. before(function (done) {
  21. // setup target HTTP server
  22. server = http.createServer();
  23. server.listen(function () {
  24. serverPort = server.address().port;
  25. done();
  26. });
  27. });
  28. before(function (done) {
  29. // setup HTTP proxy server
  30. proxy = Proxy();
  31. proxy.listen(function () {
  32. proxyPort = proxy.address().port;
  33. done();
  34. });
  35. });
  36. before(function (done) {
  37. // setup target HTTPS server
  38. var options = {
  39. key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
  40. cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
  41. };
  42. sslServer = https.createServer(options);
  43. sslServer.listen(function () {
  44. sslServerPort = sslServer.address().port;
  45. done();
  46. });
  47. });
  48. before(function (done) {
  49. // setup SSL HTTP proxy server
  50. var options = {
  51. key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
  52. cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
  53. };
  54. sslProxy = Proxy(https.createServer(options));
  55. sslProxy.listen(function () {
  56. sslProxyPort = sslProxy.address().port;
  57. done();
  58. });
  59. });
  60. // shut down test HTTP server
  61. after(function (done) {
  62. server.once('close', function () { done(); });
  63. server.close();
  64. });
  65. after(function (done) {
  66. proxy.once('close', function () { done(); });
  67. proxy.close();
  68. });
  69. after(function (done) {
  70. sslServer.once('close', function () { done(); });
  71. sslServer.close();
  72. });
  73. after(function (done) {
  74. sslProxy.once('close', function () { done(); });
  75. sslProxy.close();
  76. });
  77. describe('constructor', function () {
  78. it('should throw an Error if no "proxy" argument is given', function () {
  79. assert.throws(function () {
  80. new HttpsProxyAgent();
  81. });
  82. });
  83. it('should accept a "string" proxy argument', function () {
  84. var agent = new HttpsProxyAgent('http://127.0.0.1:' + proxyPort);
  85. assert.equal('127.0.0.1', agent.proxy.host);
  86. assert.equal(proxyPort, agent.proxy.port);
  87. });
  88. it('should accept a `url.parse()` result object argument', function () {
  89. var opts = url.parse('http://127.0.0.1:' + proxyPort);
  90. var agent = new HttpsProxyAgent(opts);
  91. assert.equal('127.0.0.1', agent.proxy.host);
  92. assert.equal(proxyPort, agent.proxy.port);
  93. });
  94. it('should set a `defaultPort` property', function () {
  95. var opts = url.parse("http://127.0.0.1:" + proxyPort);
  96. var agent = new HttpsProxyAgent(opts);
  97. assert.equal(443, agent.defaultPort);
  98. });
  99. describe('secureProxy', function () {
  100. it('should default to `false`', function () {
  101. var agent = new HttpsProxyAgent({ port: proxyPort });
  102. assert.equal(false, agent.secureProxy);
  103. });
  104. it('should be `false` when "http:" protocol is used', function () {
  105. var agent = new HttpsProxyAgent({ port: proxyPort, protocol: 'http:' });
  106. assert.equal(false, agent.secureProxy);
  107. });
  108. it('should be `true` when "https:" protocol is used', function () {
  109. var agent = new HttpsProxyAgent({ port: proxyPort, protocol: 'https:' });
  110. assert.equal(true, agent.secureProxy);
  111. });
  112. it('should be `true` when "https" protocol is used', function () {
  113. var agent = new HttpsProxyAgent({ port: proxyPort, protocol: 'https' });
  114. assert.equal(true, agent.secureProxy);
  115. });
  116. });
  117. });
  118. describe('"http" module', function () {
  119. beforeEach(function () {
  120. delete proxy.authenticate;
  121. });
  122. it('should work over an HTTP proxy', function (done) {
  123. server.once('request', function (req, res) {
  124. res.end(JSON.stringify(req.headers));
  125. });
  126. var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
  127. var agent = new HttpsProxyAgent(proxy);
  128. var opts = url.parse('http://127.0.0.1:' + serverPort);
  129. opts.agent = agent;
  130. var req = http.get(opts, function (res) {
  131. var data = '';
  132. res.setEncoding('utf8');
  133. res.on('data', function (b) {
  134. data += b;
  135. });
  136. res.on('end', function () {
  137. data = JSON.parse(data);
  138. assert.equal('127.0.0.1:' + serverPort, data.host);
  139. done();
  140. });
  141. });
  142. req.once('error', done);
  143. });
  144. it('should work over an HTTPS proxy', function (done) {
  145. server.once('request', function (req, res) {
  146. res.end(JSON.stringify(req.headers));
  147. });
  148. var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://127.0.0.1:' + sslProxyPort;
  149. proxy = url.parse(proxy);
  150. proxy.rejectUnauthorized = false;
  151. var agent = new HttpsProxyAgent(proxy);
  152. var opts = url.parse('http://127.0.0.1:' + serverPort);
  153. opts.agent = agent;
  154. http.get(opts, function (res) {
  155. var data = '';
  156. res.setEncoding('utf8');
  157. res.on('data', function (b) {
  158. data += b;
  159. });
  160. res.on('end', function () {
  161. data = JSON.parse(data);
  162. assert.equal('127.0.0.1:' + serverPort, data.host);
  163. done();
  164. });
  165. });
  166. });
  167. it('should receive the 407 authorization code on the `http.ClientResponse`', function (done) {
  168. // set a proxy authentication function for this test
  169. proxy.authenticate = function (req, fn) {
  170. // reject all requests
  171. fn(null, false);
  172. };
  173. var proxyUri = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
  174. var agent = new HttpsProxyAgent(proxyUri);
  175. var opts = {};
  176. // `host` and `port` don't really matter since the proxy will reject anyways
  177. opts.host = '127.0.0.1';
  178. opts.port = 80;
  179. opts.agent = agent;
  180. var req = http.get(opts, function (res) {
  181. assert.equal(407, res.statusCode);
  182. assert('proxy-authenticate' in res.headers);
  183. done();
  184. });
  185. });
  186. it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', function (done) {
  187. // port 4 is a reserved, but "unassigned" port
  188. var proxyUri = 'http://127.0.0.1:4';
  189. var agent = new HttpsProxyAgent(proxyUri);
  190. var opts = url.parse('http://nodejs.org');
  191. opts.agent = agent;
  192. var req = http.get(opts);
  193. req.once('error', function (err) {
  194. assert.equal('ECONNREFUSED', err.code);
  195. req.abort();
  196. done();
  197. });
  198. });
  199. it('should allow custom proxy "headers"', function (done) {
  200. server.once('connect', function (req, socket, head) {
  201. assert.equal('CONNECT', req.method);
  202. assert.equal('bar', req.headers.foo);
  203. socket.destroy();
  204. done();
  205. });
  206. var uri = 'http://127.0.0.1:' + serverPort;
  207. var proxyOpts = url.parse(uri);
  208. proxyOpts.headers = {
  209. 'Foo': 'bar'
  210. };
  211. var agent = new HttpsProxyAgent(proxyOpts);
  212. var opts = {};
  213. // `host` and `port` don't really matter since the proxy will reject anyways
  214. opts.host = '127.0.0.1';
  215. opts.port = 80;
  216. opts.agent = agent;
  217. http.get(opts);
  218. });
  219. });
  220. describe('"https" module', function () {
  221. it('should work over an HTTP proxy', function (done) {
  222. sslServer.once('request', function (req, res) {
  223. res.end(JSON.stringify(req.headers));
  224. });
  225. var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
  226. var agent = new HttpsProxyAgent(proxy);
  227. var opts = url.parse('https://127.0.0.1:' + sslServerPort);
  228. opts.rejectUnauthorized = false;
  229. opts.agent = agent;
  230. https.get(opts, function (res) {
  231. var data = '';
  232. res.setEncoding('utf8');
  233. res.on('data', function (b) {
  234. data += b;
  235. });
  236. res.on('end', function () {
  237. data = JSON.parse(data);
  238. assert.equal('127.0.0.1:' + sslServerPort, data.host);
  239. done();
  240. });
  241. });
  242. });
  243. it('should work over an HTTPS proxy', function (done) {
  244. sslServer.once('request', function (req, res) {
  245. res.end(JSON.stringify(req.headers));
  246. });
  247. var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://127.0.0.1:' + sslProxyPort;
  248. proxy = url.parse(proxy);
  249. proxy.rejectUnauthorized = false;
  250. var agent = new HttpsProxyAgent(proxy);
  251. var opts = url.parse('https://127.0.0.1:' + sslServerPort);
  252. opts.agent = agent;
  253. opts.rejectUnauthorized = false;
  254. https.get(opts, function (res) {
  255. var data = '';
  256. res.setEncoding('utf8');
  257. res.on('data', function (b) {
  258. data += b;
  259. });
  260. res.on('end', function () {
  261. data = JSON.parse(data);
  262. assert.equal('127.0.0.1:' + sslServerPort, data.host);
  263. done();
  264. });
  265. });
  266. });
  267. it('should not send a port number for the default port', function (done) {
  268. sslServer.once('request', function (req, res) {
  269. res.end(JSON.stringify(req.headers));
  270. });
  271. var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || "https://127.0.0.1:" + sslProxyPort;
  272. proxy = url.parse(proxy);
  273. proxy.rejectUnauthorized = false;
  274. var agent = new HttpsProxyAgent(proxy);
  275. agent.defaultPort = sslServerPort;
  276. var opts = url.parse("https://127.0.0.1:" + sslServerPort);
  277. opts.agent = agent;
  278. opts.rejectUnauthorized = false;
  279. https.get(opts, function(res) {
  280. var data = "";
  281. res.setEncoding("utf8");
  282. res.on("data", function(b) {
  283. data += b;
  284. });
  285. res.on("end", function() {
  286. data = JSON.parse(data);
  287. assert.equal("127.0.0.1", data.host);
  288. done();
  289. });
  290. });
  291. });
  292. });
  293. });