proxy_spec.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. var helpers = require('./helpers'),
  2. should = require('should'),
  3. sinon = require('sinon'),
  4. http = require('http'),
  5. needle = require('./../');
  6. var port = 7707;
  7. var url = 'localhost:' + port;
  8. var nonexisting_host = 'awepfokawepofawe.com';
  9. describe('proxy option', function() {
  10. var spy, opts;
  11. function send_request(opts, done) {
  12. if (spy) spy.restore();
  13. spy = sinon.spy(http, 'request');
  14. needle.get(url, opts, done);
  15. }
  16. //////////////////////
  17. // proxy opts helpers
  18. function not_proxied(done) {
  19. return function(err, resp) {
  20. var path = spy.args[0][0].path;
  21. path.should.eql('/'); // not the full original URI
  22. spy.restore();
  23. done();
  24. }
  25. }
  26. function proxied(host, port, done) {
  27. return function(err, resp) {
  28. var path = spy.args[0][0].path;
  29. path.should.eql('http://' + url); // the full original URI
  30. var http_host = spy.args[0][0].host;
  31. if (http_host) http_host.should.eql(host);
  32. var http_port = spy.args[0][0].port;
  33. if (http_port) http_port.should.eql(port);
  34. spy.restore();
  35. done();
  36. }
  37. }
  38. //////////////////////
  39. // auth helpers
  40. function get_auth(header) {
  41. var token = header.split(/\s+/).pop();
  42. return token && Buffer.from(token, 'base64').toString().split(':');
  43. }
  44. function no_proxy_auth(done) {
  45. return function(err, resp) {
  46. var headers = spy.args[0][0].headers;
  47. Object.keys(headers).should.not.containEql('proxy-authorization');
  48. done();
  49. }
  50. }
  51. function header_set(name, user, pass, done) {
  52. return function(err, resp) {
  53. var headers = spy.args[0][0].headers;
  54. var auth = get_auth(headers[name]);
  55. auth[0].should.eql(user);
  56. auth[1].should.eql(pass);
  57. done();
  58. }
  59. }
  60. function proxy_auth_set(user, pass, done) {
  61. return header_set('proxy-authorization', user, pass, done);
  62. }
  63. function basic_auth_set(user, pass, done) {
  64. return header_set('authorization', user, pass, done);
  65. }
  66. after(function() {
  67. spy.restore();
  68. })
  69. describe('when null proxy is passed', function() {
  70. it('does not proxy', function(done) {
  71. send_request({ proxy: null }, not_proxied(done))
  72. })
  73. describe('but defaults has been set', function() {
  74. before(function() {
  75. needle.defaults({ proxy: 'foobar' });
  76. })
  77. after(function() {
  78. needle.defaults({ proxy: null });
  79. })
  80. it('tries to proxy anyway', function(done) {
  81. send_request({}, proxied('foobar', 80, done))
  82. })
  83. })
  84. })
  85. describe('when weird string is passed', function() {
  86. it('tries to proxy anyway', function(done) {
  87. send_request({ proxy: 'alfalfa' }, proxied('alfalfa', 80, done))
  88. })
  89. })
  90. describe('when valid url is passed', function() {
  91. it('proxies request', function(done) {
  92. send_request({ proxy: nonexisting_host + ':123/done' }, proxied(nonexisting_host, '123', done))
  93. })
  94. it('does not set a Proxy-Authorization header', function(done) {
  95. send_request({ proxy: nonexisting_host + ':123/done' }, no_proxy_auth(done));
  96. })
  97. describe('and proxy url contains user:pass', function() {
  98. before(function() {
  99. opts = {
  100. proxy: 'http://mj:x@' + nonexisting_host + ':123/done'
  101. }
  102. })
  103. it('proxies request', function(done) {
  104. send_request(opts, proxied(nonexisting_host, '123', done))
  105. })
  106. it('sets Proxy-Authorization header', function(done) {
  107. send_request(opts, proxy_auth_set('mj', 'x', done));
  108. })
  109. })
  110. describe('and a proxy_user is passed', function() {
  111. before(function() {
  112. opts = {
  113. proxy: nonexisting_host + ':123',
  114. proxy_user: 'someone',
  115. proxy_pass: 'else'
  116. }
  117. })
  118. it('proxies request', function(done) {
  119. send_request(opts, proxied(nonexisting_host, '123', done))
  120. })
  121. it('sets Proxy-Authorization header', function(done) {
  122. send_request(opts, proxy_auth_set('someone', 'else', done));
  123. })
  124. describe('and url also contains user:pass', function() {
  125. it('url user:pass wins', function(done) {
  126. var opts = {
  127. proxy: 'http://xxx:yyy@' + nonexisting_host + ':123',
  128. proxy_user: 'someone',
  129. proxy_pass: 'else'
  130. }
  131. send_request(opts, proxy_auth_set('xxx', 'yyy', done));
  132. })
  133. })
  134. describe('and options.username is also present', function() {
  135. before(function() {
  136. opts = { proxy_user: 'foobar', username: 'someone' };
  137. })
  138. it('a separate Authorization header is set', function(done) {
  139. var opts = {
  140. proxy: nonexisting_host + ':123',
  141. proxy_user: 'someone',
  142. proxy_pass: 'else',
  143. username: 'test',
  144. password: 'X'
  145. }
  146. send_request(opts, basic_auth_set('test', 'X', done));
  147. })
  148. })
  149. })
  150. })
  151. })