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 Lab = require('lab');
6
var Package = require('../package.json');
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('Utils', function () {
25
26
describe('#parseHost', function () {
27
28
it('returns port 80 for non tls node request', function (done) {
29
30
var req = {
31
method: 'POST',
32
url: '/resource/4?filter=a',
33
headers: {
34
host: 'example.com',
35
'content-type': 'text/plain;x=y'
36
}
37
};
38
39
expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(80);
40
done();
41
});
42
43
it('returns port 443 for non tls node request', function (done) {
44
45
var req = {
46
method: 'POST',
47
url: '/resource/4?filter=a',
48
headers: {
49
host: 'example.com',
50
'content-type': 'text/plain;x=y'
51
},
52
connection: {
53
encrypted: true
54
}
55
};
56
57
expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(443);
58
done();
59
});
60
61
it('returns port 443 for non tls node request (IPv6)', function (done) {
62
63
var req = {
64
method: 'POST',
65
url: '/resource/4?filter=a',
66
headers: {
67
host: '[123:123:123]',
68
'content-type': 'text/plain;x=y'
69
},
70
connection: {
71
encrypted: true
72
}
73
};
74
75
expect(Hawk.utils.parseHost(req, 'Host').port).to.equal(443);
76
done();
77
});
78
79
it('parses IPv6 headers', function (done) {
80
81
var req = {
82
method: 'POST',
83
url: '/resource/4?filter=a',
84
headers: {
85
host: '[123:123:123]:8000',
86
'content-type': 'text/plain;x=y'
87
},
88
connection: {
89
encrypted: true
90
}
91
};
92
93
var host = Hawk.utils.parseHost(req, 'Host');
94
expect(host.port).to.equal('8000');
95
expect(host.name).to.equal('[123:123:123]');
96
done();
97
});
98
});
99
100
describe('#version', function () {
101
102
it('returns the correct package version number', function (done) {
103
104
expect(Hawk.utils.version()).to.equal(Package.version);
105
done();
106
});
107
});
108
109
describe('#unauthorized', function () {
110
111
it('returns a hawk 401', function (done) {
112
113
expect(Hawk.utils.unauthorized('kaboom').output.headers['WWW-Authenticate']).to.equal('Hawk error="kaboom"');
114
done();
115
});
116
});
117
});
118
});
119
120
121
122