utils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Load modules
  2. var Lab = require('lab');
  3. var Hawk = require('../lib');
  4. var Package = require('../package.json');
  5. // Declare internals
  6. var internals = {};
  7. // Test shortcuts
  8. var expect = Lab.expect;
  9. var before = Lab.before;
  10. var after = Lab.after;
  11. var describe = Lab.experiment;
  12. var it = Lab.test;
  13. describe('Hawk', function () {
  14. describe('Utils', function () {
  15. describe('#parseHost', function () {
  16. it('returns port 80 for non tls node request', function (done) {
  17. var req = {
  18. method: 'POST',
  19. url: '/resource/4?filter=a',
  20. headers: {
  21. host: 'example.com',
  22. 'content-type': 'text/plain;x=y'
  23. }
  24. };
  25. expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(80);
  26. done();
  27. });
  28. it('returns port 443 for non tls node request', function (done) {
  29. var req = {
  30. method: 'POST',
  31. url: '/resource/4?filter=a',
  32. headers: {
  33. host: 'example.com',
  34. 'content-type': 'text/plain;x=y'
  35. },
  36. connection: {
  37. encrypted: true
  38. }
  39. };
  40. expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(443);
  41. done();
  42. });
  43. it('returns port 443 for non tls node request (IPv6)', function (done) {
  44. var req = {
  45. method: 'POST',
  46. url: '/resource/4?filter=a',
  47. headers: {
  48. host: '[123:123:123]',
  49. 'content-type': 'text/plain;x=y'
  50. },
  51. connection: {
  52. encrypted: true
  53. }
  54. };
  55. expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(443);
  56. done();
  57. });
  58. it('parses IPv6 headers', function (done) {
  59. var req = {
  60. method: 'POST',
  61. url: '/resource/4?filter=a',
  62. headers: {
  63. host: '[123:123:123]:8000',
  64. 'content-type': 'text/plain;x=y'
  65. },
  66. connection: {
  67. encrypted: true
  68. }
  69. };
  70. var host = Hawk.utils.parseHost(req, 'Host');
  71. expect(host.port).to.equal('8000');
  72. expect(host.name).to.equal('[123:123:123]');
  73. done();
  74. });
  75. });
  76. describe('#version', function () {
  77. it('returns the correct package version number', function (done) {
  78. expect(Hawk.utils.version()).to.equal(Package.version);
  79. done();
  80. });
  81. });
  82. describe('#unauthorized', function () {
  83. it('returns a hawk 401', function (done) {
  84. expect(Hawk.utils.unauthorized('kaboom').response.headers['WWW-Authenticate']).to.equal('Hawk error="kaboom"');
  85. done();
  86. });
  87. });
  88. });
  89. });