/
test-hawk.js
57 lines (52 loc) 路 1.3 KB
/
test-hawk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict'
var http = require('http')
var request = require('../index')
var hawk = require('hawk')
var tape = require('tape')
var assert = require('assert')
var server = http.createServer(function (req, res) {
var getCred = function (id) {
assert.equal(id, 'dh37fgj492je')
var credentials = {
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256',
user: 'Steve'
}
return credentials
}
hawk.server.authenticate(req, getCred)
.then((credentials, artifacts) => {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Hello ' + credentials.credentials.user)
})
.catch(() => {
res.writeHead(401, {'Content-Type': 'text/plain'})
res.end('Shoosh!')
})
})
tape('setup', function (t) {
server.listen(0, function () {
server.url = 'http://localhost:' + this.address().port
t.end()
})
})
tape('hawk', function (t) {
var creds = {
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256',
id: 'dh37fgj492je'
}
request(server.url, {
hawk: { credentials: creds }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(body, 'Hello Steve')
t.end()
})
})
tape('cleanup', function (t) {
server.close(function () {
t.end()
})
})