PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/FpgaClient/node_modules/http-proxy-agent/test/test.js

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