readme.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Load modules
  2. var Code = require('code');
  3. var Hawk = require('../lib');
  4. var Hoek = require('hoek');
  5. var Lab = require('lab');
  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('README', function () {
  14. describe('core', function () {
  15. var credentials = {
  16. id: 'dh37fgj492je',
  17. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  18. algorithm: 'sha256'
  19. };
  20. var options = {
  21. credentials: credentials,
  22. timestamp: 1353832234,
  23. nonce: 'j4h3g2',
  24. ext: 'some-app-ext-data'
  25. };
  26. it('should generate a header protocol example', function (done) {
  27. var header = Hawk.client.header('http://example.com:8000/resource/1?b=1&a=2', 'GET', options).field;
  28. expect(header).to.equal('Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", ext="some-app-ext-data", mac="6R4rV5iE+NPoym+WwjeHzjAGXUtLNIxmo1vpMofpLAE="');
  29. done();
  30. });
  31. it('should generate a normalized string protocol example', function (done) {
  32. var normalized = Hawk.crypto.generateNormalizedString('header', {
  33. credentials: credentials,
  34. ts: options.timestamp,
  35. nonce: options.nonce,
  36. method: 'GET',
  37. resource: '/resource?a=1&b=2',
  38. host: 'example.com',
  39. port: 8000,
  40. ext: options.ext
  41. });
  42. expect(normalized).to.equal('hawk.1.header\n1353832234\nj4h3g2\nGET\n/resource?a=1&b=2\nexample.com\n8000\n\nsome-app-ext-data\n');
  43. done();
  44. });
  45. var payloadOptions = Hoek.clone(options);
  46. payloadOptions.payload = 'Thank you for flying Hawk';
  47. payloadOptions.contentType = 'text/plain';
  48. it('should generate a header protocol example (with payload)', function (done) {
  49. var header = Hawk.client.header('http://example.com:8000/resource/1?b=1&a=2', 'POST', payloadOptions).field;
  50. expect(header).to.equal('Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", hash="Yi9LfIIFRtBEPt74PVmbTF/xVAwPn7ub15ePICfgnuY=", ext="some-app-ext-data", mac="aSe1DERmZuRl3pI36/9BdZmnErTw3sNzOOAUlfeKjVw="');
  51. done();
  52. });
  53. it('should generate a normalized string protocol example (with payload)', function (done) {
  54. var normalized = Hawk.crypto.generateNormalizedString('header', {
  55. credentials: credentials,
  56. ts: options.timestamp,
  57. nonce: options.nonce,
  58. method: 'POST',
  59. resource: '/resource?a=1&b=2',
  60. host: 'example.com',
  61. port: 8000,
  62. hash: Hawk.crypto.calculatePayloadHash(payloadOptions.payload, credentials.algorithm, payloadOptions.contentType),
  63. ext: options.ext
  64. });
  65. expect(normalized).to.equal('hawk.1.header\n1353832234\nj4h3g2\nPOST\n/resource?a=1&b=2\nexample.com\n8000\nYi9LfIIFRtBEPt74PVmbTF/xVAwPn7ub15ePICfgnuY=\nsome-app-ext-data\n');
  66. done();
  67. });
  68. });
  69. });