test-basic-auth.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. var assert = require('assert')
  2. , http = require('http')
  3. , request = require('../index')
  4. ;
  5. var numBasicRequests = 0;
  6. var basicServer = http.createServer(function (req, res) {
  7. console.error('Basic auth server: ', req.method, req.url);
  8. numBasicRequests++;
  9. var ok;
  10. if (req.headers.authorization) {
  11. if (req.headers.authorization == 'Basic ' + new Buffer('test:testing2').toString('base64')) {
  12. ok = true;
  13. } else if ( req.headers.authorization == 'Basic ' + new Buffer('test:').toString('base64')) {
  14. ok = true;
  15. } else if ( req.headers.authorization == 'Basic ' + new Buffer(':apassword').toString('base64')) {
  16. ok = true;
  17. } else if ( req.headers.authorization == 'Basic ' + new Buffer('justauser').toString('base64')) {
  18. ok = true;
  19. } else {
  20. // Bad auth header, don't send back WWW-Authenticate header
  21. ok = false;
  22. }
  23. } else {
  24. // No auth header, send back WWW-Authenticate header
  25. ok = false;
  26. res.setHeader('www-authenticate', 'Basic realm="Private"');
  27. }
  28. if (req.url == '/post/') {
  29. var expectedContent = 'data_key=data_value';
  30. req.on('data', function(data) {
  31. assert.equal(data, expectedContent);
  32. console.log('received request data: ' + data);
  33. });
  34. assert.equal(req.method, 'POST');
  35. assert.equal(req.headers['content-length'], '' + expectedContent.length);
  36. assert.equal(req.headers['content-type'], 'application/x-www-form-urlencoded; charset=utf-8');
  37. }
  38. if (ok) {
  39. console.log('request ok');
  40. res.end('ok');
  41. } else {
  42. console.log('status=401');
  43. res.statusCode = 401;
  44. res.end('401');
  45. }
  46. });
  47. basicServer.listen(6767);
  48. var tests = [
  49. function(next) {
  50. request({
  51. 'method': 'GET',
  52. 'uri': 'http://localhost:6767/test/',
  53. 'auth': {
  54. 'user': 'test',
  55. 'pass': 'testing2',
  56. 'sendImmediately': false
  57. }
  58. }, function(error, res, body) {
  59. assert.equal(res.statusCode, 200);
  60. assert.equal(numBasicRequests, 2);
  61. next();
  62. });
  63. },
  64. function(next) {
  65. // If we don't set sendImmediately = false, request will send basic auth
  66. request({
  67. 'method': 'GET',
  68. 'uri': 'http://localhost:6767/test2/',
  69. 'auth': {
  70. 'user': 'test',
  71. 'pass': 'testing2'
  72. }
  73. }, function(error, res, body) {
  74. assert.equal(res.statusCode, 200);
  75. assert.equal(numBasicRequests, 3);
  76. next();
  77. });
  78. },
  79. function(next) {
  80. request({
  81. 'method': 'GET',
  82. 'uri': 'http://test:testing2@localhost:6767/test2/'
  83. }, function(error, res, body) {
  84. assert.equal(res.statusCode, 200);
  85. assert.equal(numBasicRequests, 4);
  86. next();
  87. });
  88. },
  89. function(next) {
  90. request({
  91. 'method': 'POST',
  92. 'form': { 'data_key': 'data_value' },
  93. 'uri': 'http://localhost:6767/post/',
  94. 'auth': {
  95. 'user': 'test',
  96. 'pass': 'testing2',
  97. 'sendImmediately': false
  98. }
  99. }, function(error, res, body) {
  100. assert.equal(res.statusCode, 200);
  101. assert.equal(numBasicRequests, 6);
  102. next();
  103. });
  104. },
  105. function(next) {
  106. assert.doesNotThrow( function() {
  107. request({
  108. 'method': 'GET',
  109. 'uri': 'http://localhost:6767/allow_empty_user/',
  110. 'auth': {
  111. 'user': '',
  112. 'pass': 'apassword',
  113. 'sendImmediately': false
  114. }
  115. }, function(error, res, body ) {
  116. assert.equal(res.statusCode, 200);
  117. assert.equal(numBasicRequests, 8);
  118. next();
  119. });
  120. })
  121. },
  122. function(next) {
  123. assert.doesNotThrow( function() {
  124. request({
  125. 'method': 'GET',
  126. 'uri': 'http://localhost:6767/allow_undefined_password/',
  127. 'auth': {
  128. 'user': 'justauser',
  129. 'pass': undefined,
  130. 'sendImmediately': false
  131. }
  132. }, function(error, res, body ) {
  133. assert.equal(res.statusCode, 200);
  134. assert.equal(numBasicRequests, 10);
  135. next();
  136. });
  137. })
  138. },
  139. function (next) {
  140. request
  141. .get('http://localhost:6767/test/')
  142. .auth("test","",false)
  143. .on('response', function (res) {
  144. assert.equal(res.statusCode, 200);
  145. assert.equal(numBasicRequests, 12);
  146. next();
  147. })
  148. },
  149. function (next) {
  150. request.get('http://localhost:6767/test/',
  151. {
  152. auth: {
  153. user: "test",
  154. pass: "",
  155. sendImmediately: false
  156. }
  157. }, function (err, res) {
  158. assert.equal(res.statusCode, 200);
  159. assert.equal(numBasicRequests, 14);
  160. next();
  161. })
  162. }
  163. ];
  164. function runTest(i) {
  165. if (i < tests.length) {
  166. tests[i](function() {
  167. runTest(i + 1);
  168. });
  169. } else {
  170. console.log('All tests passed');
  171. basicServer.close();
  172. }
  173. }
  174. runTest(0);