test.js 9.6 KB

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