You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 9.6KB

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