utils.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Load modules
  2. var Code = require('code');
  3. var Hawk = require('../lib');
  4. var Lab = require('lab');
  5. var Package = require('../package.json');
  6. // Declare internals
  7. var internals = {};
  8. // Test shortcuts
  9. var lab = exports.lab = Lab.script();
  10. var describe = lab.experiment;
  11. var it = lab.test;
  12. var expect = Code.expect;
  13. describe('Utils', function () {
  14. describe('parseHost()', function () {
  15. it('returns port 80 for non tls node request', function (done) {
  16. var req = {
  17. method: 'POST',
  18. url: '/resource/4?filter=a',
  19. headers: {
  20. host: 'example.com',
  21. 'content-type': 'text/plain;x=y'
  22. }
  23. };
  24. expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(80);
  25. done();
  26. });
  27. it('returns port 443 for non tls node request', function (done) {
  28. var req = {
  29. method: 'POST',
  30. url: '/resource/4?filter=a',
  31. headers: {
  32. host: 'example.com',
  33. 'content-type': 'text/plain;x=y'
  34. },
  35. connection: {
  36. encrypted: true
  37. }
  38. };
  39. expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(443);
  40. done();
  41. });
  42. it('returns port 443 for non tls node request (IPv6)', function (done) {
  43. var req = {
  44. method: 'POST',
  45. url: '/resource/4?filter=a',
  46. headers: {
  47. host: '[123:123:123]',
  48. 'content-type': 'text/plain;x=y'
  49. },
  50. connection: {
  51. encrypted: true
  52. }
  53. };
  54. expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(443);
  55. done();
  56. });
  57. it('parses IPv6 headers', function (done) {
  58. var req = {
  59. method: 'POST',
  60. url: '/resource/4?filter=a',
  61. headers: {
  62. host: '[123:123:123]:8000',
  63. 'content-type': 'text/plain;x=y'
  64. },
  65. connection: {
  66. encrypted: true
  67. }
  68. };
  69. var host = Hawk.utils.parseHost(req, 'Host');
  70. expect(host.port).to.equal('8000');
  71. expect(host.name).to.equal('[123:123:123]');
  72. done();
  73. });
  74. it('errors on header too long', function (done) {
  75. var long = '';
  76. for (var i = 0; i < 5000; ++i) {
  77. long += 'x';
  78. }
  79. expect(Hawk.utils.parseHost({ headers: { host: long } })).to.be.null();
  80. done();
  81. });
  82. });
  83. describe('parseAuthorizationHeader()', function () {
  84. it('errors on header too long', function (done) {
  85. var long = 'Scheme a="';
  86. for (var i = 0; i < 5000; ++i) {
  87. long += 'x';
  88. }
  89. long += '"';
  90. var err = Hawk.utils.parseAuthorizationHeader(long, ['a']);
  91. expect(err).to.be.instanceof(Error);
  92. expect(err.message).to.equal('Header length too long');
  93. done();
  94. });
  95. });
  96. describe('version()', function () {
  97. it('returns the correct package version number', function (done) {
  98. expect(Hawk.utils.version()).to.equal(Package.version);
  99. done();
  100. });
  101. });
  102. describe('unauthorized()', function () {
  103. it('returns a hawk 401', function (done) {
  104. expect(Hawk.utils.unauthorized('kaboom').output.headers['WWW-Authenticate']).to.equal('Hawk error="kaboom"');
  105. done();
  106. });
  107. it('supports attributes', function (done) {
  108. expect(Hawk.utils.unauthorized('kaboom', { a: 'b' }).output.headers['WWW-Authenticate']).to.equal('Hawk a="b", error="kaboom"');
  109. done();
  110. });
  111. });
  112. });