Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81146 views
1
// Load modules
2
3
var Code = require('code');
4
var Hawk = require('../lib');
5
var Hoek = require('hoek');
6
var Lab = require('lab');
7
8
9
// Declare internals
10
11
var internals = {};
12
13
14
// Test shortcuts
15
16
var lab = exports.lab = Lab.script();
17
var describe = lab.experiment;
18
var it = lab.test;
19
var expect = Code.expect;
20
21
22
describe('Hawk', function () {
23
24
describe('README', function () {
25
26
describe('core', function () {
27
28
var credentials = {
29
id: 'dh37fgj492je',
30
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
31
algorithm: 'sha256'
32
};
33
34
var options = {
35
credentials: credentials,
36
timestamp: 1353832234,
37
nonce: 'j4h3g2',
38
ext: 'some-app-ext-data'
39
};
40
41
it('should generate a header protocol example', function (done) {
42
43
var header = Hawk.client.header('http://example.com:8000/resource/1?b=1&a=2', 'GET', options).field;
44
45
expect(header).to.equal('Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", ext="some-app-ext-data", mac="6R4rV5iE+NPoym+WwjeHzjAGXUtLNIxmo1vpMofpLAE="');
46
done();
47
});
48
49
it('should generate a normalized string protocol example', function (done) {
50
51
var normalized = Hawk.crypto.generateNormalizedString('header', {
52
credentials: credentials,
53
ts: options.timestamp,
54
nonce: options.nonce,
55
method: 'GET',
56
resource: '/resource?a=1&b=2',
57
host: 'example.com',
58
port: 8000,
59
ext: options.ext
60
});
61
62
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');
63
done();
64
});
65
66
var payloadOptions = Hoek.clone(options);
67
payloadOptions.payload = 'Thank you for flying Hawk';
68
payloadOptions.contentType = 'text/plain';
69
70
it('should generate a header protocol example (with payload)', function (done) {
71
72
var header = Hawk.client.header('http://example.com:8000/resource/1?b=1&a=2', 'POST', payloadOptions).field;
73
74
expect(header).to.equal('Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", hash="Yi9LfIIFRtBEPt74PVmbTF/xVAwPn7ub15ePICfgnuY=", ext="some-app-ext-data", mac="aSe1DERmZuRl3pI36/9BdZmnErTw3sNzOOAUlfeKjVw="');
75
done();
76
});
77
78
it('should generate a normalized string protocol example (with payload)', function (done) {
79
80
var normalized = Hawk.crypto.generateNormalizedString('header', {
81
credentials: credentials,
82
ts: options.timestamp,
83
nonce: options.nonce,
84
method: 'POST',
85
resource: '/resource?a=1&b=2',
86
host: 'example.com',
87
port: 8000,
88
hash: Hawk.crypto.calculatePayloadHash(payloadOptions.payload, credentials.algorithm, payloadOptions.contentType),
89
ext: options.ext
90
});
91
92
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');
93
done();
94
});
95
});
96
});
97
});
98
99
100