basic_auth_spec.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. var helpers = require('./helpers'),
  2. should = require('should'),
  3. needle = require('./../'),
  4. server;
  5. var port = 7707;
  6. describe('Basic Auth', function() {
  7. before(function(done) {
  8. server = helpers.server({ port: port }, done);
  9. })
  10. after(function(done) {
  11. server.close(done);
  12. })
  13. ///////////////// helpers
  14. var get_auth = function(header) {
  15. var token = header.split(/\s+/).pop();
  16. return token && Buffer.from(token, 'base64').toString().split(':');
  17. }
  18. describe('when neither username or password are passed', function() {
  19. it('doesnt send any Authorization headers', function(done) {
  20. needle.get('localhost:' + port, { parse: true }, function(err, resp) {
  21. var sent_headers = resp.body.headers;
  22. Object.keys(sent_headers).should.not.containEql('authorization');
  23. done();
  24. })
  25. })
  26. })
  27. describe('when username is an empty string, and password is a valid string', function() {
  28. var opts = { username: '', password: 'foobar', parse: true };
  29. it('doesnt send any Authorization headers', function(done) {
  30. needle.get('localhost:' + port, { parse: true }, function(err, resp) {
  31. var sent_headers = resp.body.headers;
  32. Object.keys(sent_headers).should.not.containEql('authorization');
  33. done();
  34. })
  35. })
  36. });
  37. describe('when username is a valid string, but no username is passed', function() {
  38. var opts = { username: 'foobar', parse: true };
  39. it('sends Authorization header', function(done) {
  40. needle.get('localhost:' + port, opts, function(err, resp) {
  41. var sent_headers = resp.body.headers;
  42. Object.keys(sent_headers).should.containEql('authorization');
  43. done();
  44. })
  45. })
  46. it('Basic Auth only includes username, without colon', function(done) {
  47. needle.get('localhost:' + port, opts, function(err, resp) {
  48. var sent_headers = resp.body.headers;
  49. var auth = get_auth(sent_headers['authorization']);
  50. auth[0].should.equal('foobar');
  51. auth.should.have.lengthOf(1);
  52. done();
  53. })
  54. })
  55. })
  56. describe('when username is a valid string, and password is null', function() {
  57. var opts = { username: 'foobar', password: null, parse: true };
  58. it('sends Authorization header', function(done) {
  59. needle.get('localhost:' + port, opts, function(err, resp) {
  60. var sent_headers = resp.body.headers;
  61. Object.keys(sent_headers).should.containEql('authorization');
  62. done();
  63. })
  64. })
  65. it('Basic Auth only includes both username and password', function(done) {
  66. needle.get('localhost:' + port, opts, function(err, resp) {
  67. var sent_headers = resp.body.headers;
  68. var auth = get_auth(sent_headers['authorization']);
  69. auth[0].should.equal('foobar');
  70. auth[1].should.equal('');
  71. done();
  72. })
  73. })
  74. })
  75. describe('when username is a valid string, and password is an empty string', function() {
  76. var opts = { username: 'foobar', password: '', parse: true };
  77. it('sends Authorization header', function(done) {
  78. needle.get('localhost:' + port, opts, function(err, resp) {
  79. var sent_headers = resp.body.headers;
  80. Object.keys(sent_headers).should.containEql('authorization');
  81. done();
  82. })
  83. })
  84. it('Basic Auth only includes both username and password', function(done) {
  85. needle.get('localhost:' + port, opts, function(err, resp) {
  86. var sent_headers = resp.body.headers;
  87. var auth = get_auth(sent_headers['authorization']);
  88. auth[0].should.equal('foobar');
  89. auth[1].should.equal('');
  90. auth.should.have.lengthOf(2);
  91. done();
  92. })
  93. })
  94. })
  95. describe('when username AND password are non empty strings', function() {
  96. var opts = { username: 'foobar', password: 'jakub', parse: true };
  97. it('sends Authorization header', function(done) {
  98. needle.get('localhost:' + port, opts, function(err, resp) {
  99. var sent_headers = resp.body.headers;
  100. Object.keys(sent_headers).should.containEql('authorization');
  101. done();
  102. })
  103. })
  104. it('Basic Auth only includes both user and password', function(done) {
  105. needle.get('localhost:' + port, opts, function(err, resp) {
  106. var sent_headers = resp.body.headers;
  107. var auth = get_auth(sent_headers['authorization']);
  108. auth[0].should.equal('foobar');
  109. auth[1].should.equal('jakub');
  110. auth.should.have.lengthOf(2);
  111. done();
  112. })
  113. })
  114. })
  115. describe('URL with @ but not username/pass', function() {
  116. it('doesnt send Authorization header', function(done) {
  117. var url = 'localhost:' + port + '/abc/@def/xyz.zip';
  118. needle.get(url, {}, function(err, resp) {
  119. var sent_headers = resp.body.headers;
  120. Object.keys(sent_headers).should.not.containEql('authorization');
  121. done();
  122. })
  123. })
  124. it('sends user:pass headers if passed via options', function(done) {
  125. var url = 'localhost:' + port + '/abc/@def/xyz.zip';
  126. needle.get(url, { username: 'foo' }, function(err, resp) {
  127. var sent_headers = resp.body.headers;
  128. Object.keys(sent_headers).should.containEql('authorization');
  129. sent_headers['authorization'].should.eql('Basic Zm9v')
  130. done();
  131. })
  132. })
  133. })
  134. describe('when username/password are included in URL', function() {
  135. var opts = { parse: true };
  136. it('sends Authorization header', function(done) {
  137. needle.get('foobar:jakub@localhost:' + port, opts, function(err, resp) {
  138. var sent_headers = resp.body.headers;
  139. Object.keys(sent_headers).should.containEql('authorization');
  140. done();
  141. })
  142. })
  143. it('Basic Auth only includes both user and password', function(done) {
  144. needle.get('foobar:jakub@localhost:' + port, opts, function(err, resp) {
  145. var sent_headers = resp.body.headers;
  146. var auth = get_auth(sent_headers['authorization']);
  147. auth[0].should.equal('foobar');
  148. auth[1].should.equal('jakub');
  149. auth.should.have.lengthOf(2);
  150. done();
  151. })
  152. })
  153. })
  154. })