test.js 8.9 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 PacProxyAgent = require('../');
  13. describe('PacProxyAgent', 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. describe('constructor', function () {
  93. it('should throw an Error if no "proxy" argument is given', function () {
  94. assert.throws(function () {
  95. new PacProxyAgent();
  96. });
  97. });
  98. it('should accept a "string" proxy argument', function () {
  99. var agent = new PacProxyAgent('pac+ftp://example.com/proxy.pac');
  100. assert.equal('ftp://example.com/proxy.pac', agent.uri);
  101. });
  102. it('should accept a `url.parse()` result object argument', function () {
  103. var opts = url.parse('pac+ftp://example.com/proxy.pac');
  104. var agent = new PacProxyAgent(opts);
  105. assert.equal('ftp://example.com/proxy.pac', agent.uri);
  106. });
  107. it('should accept a `uri` on the options object', function () {
  108. var agent = new PacProxyAgent({ uri: 'pac+ftp://example.com/proxy.pac' });
  109. assert.equal('ftp://example.com/proxy.pac', agent.uri);
  110. });
  111. });
  112. describe('"http" module', function () {
  113. it('should work over an HTTP proxy', function (done) {
  114. httpServer.once('request', function (req, res) {
  115. res.end(JSON.stringify(req.headers));
  116. });
  117. function FindProxyForURL(url, host) {
  118. return "PROXY 127.0.0.1:PORT;"
  119. }
  120. var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyPort));
  121. var agent = new PacProxyAgent(uri);
  122. var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
  123. opts.agent = agent;
  124. var req = http.get(opts, function (res) {
  125. toBuffer(res, function (err, buf) {
  126. if (err) return done(err);
  127. var data = JSON.parse(buf.toString('utf8'));
  128. assert.equal('127.0.0.1:' + httpPort, data.host);
  129. assert('via' in data);
  130. done();
  131. });
  132. });
  133. req.once('error', done);
  134. });
  135. it('should work over an HTTPS proxy', function (done) {
  136. httpServer.once('request', function (req, res) {
  137. res.end(JSON.stringify(req.headers));
  138. });
  139. function FindProxyForURL(url, host) {
  140. return "HTTPS 127.0.0.1:PORT;"
  141. }
  142. var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyHttpsPort));
  143. var proxy = url.parse(uri);
  144. proxy.rejectUnauthorized = false;
  145. var agent = new PacProxyAgent(proxy);
  146. var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
  147. opts.agent = agent;
  148. var req = http.get(opts, function (res) {
  149. toBuffer(res, function (err, buf) {
  150. if (err) return done(err);
  151. var data = JSON.parse(buf.toString('utf8'));
  152. assert.equal('127.0.0.1:' + httpPort, data.host);
  153. assert('via' in data);
  154. done();
  155. });
  156. });
  157. req.once('error', done);
  158. });
  159. it('should work over a SOCKS proxy', function (done) {
  160. httpServer.once('request', function (req, res) {
  161. res.end(JSON.stringify(req.headers));
  162. });
  163. function FindProxyForURL(url, host) {
  164. return "SOCKS 127.0.0.1:PORT;"
  165. }
  166. var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', socksPort));
  167. var agent = new PacProxyAgent(uri);
  168. var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
  169. opts.agent = agent;
  170. var req = http.get(opts, function (res) {
  171. toBuffer(res, function (err, buf) {
  172. if (err) return done(err);
  173. var data = JSON.parse(buf.toString('utf8'));
  174. assert.equal('127.0.0.1:' + httpPort, data.host);
  175. done();
  176. });
  177. });
  178. req.once('error', done);
  179. });
  180. });
  181. describe('"https" module', function () {
  182. it('should work over an HTTP proxy', function (done) {
  183. httpsServer.once('request', function (req, res) {
  184. res.end(JSON.stringify(req.headers));
  185. });
  186. function FindProxyForURL(url, host) {
  187. return "PROXY 127.0.0.1:PORT;"
  188. }
  189. var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyPort));
  190. var agent = new PacProxyAgent(uri);
  191. var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
  192. opts.agent = agent;
  193. opts.rejectUnauthorized = false;
  194. var req = https.get(opts, function (res) {
  195. toBuffer(res, function (err, buf) {
  196. if (err) return done(err);
  197. var data = JSON.parse(buf.toString('utf8'));
  198. assert.equal('127.0.0.1:' + httpsPort, data.host);
  199. done();
  200. });
  201. });
  202. req.once('error', done);
  203. });
  204. it('should work over an HTTPS proxy', function (done) {
  205. var gotReq = false;
  206. httpsServer.once('request', function (req, res) {
  207. gotReq = true;
  208. res.end(JSON.stringify(req.headers));
  209. });
  210. function FindProxyForURL(url, host) {
  211. return "HTTPS 127.0.0.1:PORT;"
  212. }
  213. var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', proxyHttpsPort));
  214. var agent = new PacProxyAgent(uri, {
  215. rejectUnauthorized: false
  216. });
  217. var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
  218. opts.agent = agent;
  219. opts.rejectUnauthorized = false;
  220. var req = https.get(opts, function (res) {
  221. toBuffer(res, function (err, buf) {
  222. if (err) return done(err);
  223. var data = JSON.parse(buf.toString('utf8'));
  224. assert.equal('127.0.0.1:' + httpsPort, data.host);
  225. assert(gotReq);
  226. done();
  227. });
  228. });
  229. req.once('error', done);
  230. });
  231. it('should work over a SOCKS proxy', function (done) {
  232. var gotReq = false;
  233. httpsServer.once('request', function (req, res) {
  234. gotReq = true;
  235. res.end(JSON.stringify(req.headers));
  236. });
  237. function FindProxyForURL(url, host) {
  238. return "SOCKS 127.0.0.1:PORT;"
  239. }
  240. var uri = 'data:,' + encodeURIComponent(FindProxyForURL.toString().replace('PORT', socksPort));
  241. var agent = new PacProxyAgent(uri);
  242. var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
  243. opts.agent = agent;
  244. opts.rejectUnauthorized = false;
  245. var req = https.get(opts, function (res) {
  246. toBuffer(res, function (err, buf) {
  247. if (err) return done(err);
  248. var data = JSON.parse(buf.toString('utf8'));
  249. assert.equal('127.0.0.1:' + httpsPort, data.host);
  250. assert(gotReq);
  251. done();
  252. });
  253. });
  254. req.once('error', done);
  255. });
  256. });
  257. });