test.js 8.6 KB

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