forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhydro.js
More file actions
70 lines (62 loc) · 1.94 KB
/
hydro.js
File metadata and controls
70 lines (62 loc) · 1.94 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
const nock = require('nock')
const Hydro = require('../../lib/hydro')
describe('hydro', () => {
let hydro, params
beforeEach(() => {
hydro = new Hydro({ secret: '123', endpoint: 'https://real-hydro.com' })
nock(hydro.endpoint, {
reqheaders: {
Authorization: /^Hydro [\d\w]{64}$/,
'Content-Type': 'application/json',
'X-Hydro-App': 'docs-production'
}
})
// Respond with a 200 and store the body we sent
.post('/').reply(200, (_, body) => { params = body })
})
describe('#publish', () => {
it('publishes a single event to Hydro', async () => {
await hydro.publish('event-name', { pizza: true })
expect(params).toEqual({
events: [{
schema: 'event-name',
value: JSON.stringify({ pizza: true }),
cluster: 'potomac'
}]
})
})
})
describe('#publishMany', () => {
it('publishes multiple events to Hydro', async () => {
await hydro.publishMany([
{ schema: 'event-name', value: { pizza: true } },
{ schema: 'other-name', value: { salad: false } }
])
expect(params).toEqual({
events: [{
schema: 'event-name',
value: JSON.stringify({ pizza: true }),
cluster: 'potomac'
}, {
schema: 'other-name',
value: JSON.stringify({ salad: false }),
cluster: 'potomac'
}]
})
})
})
describe('#generatePayloadHmac', () => {
it('returns a SHA256 HMAC string', () => {
const body = JSON.stringify({ pizza: true })
const hash = hydro.generatePayloadHmac(body)
expect(hash).toEqual(expect.any(String))
expect(hash).toHaveLength(64)
})
it('generates the same string for the same payload', () => {
const body = JSON.stringify({ pizza: true })
const one = hydro.generatePayloadHmac(body)
const two = hydro.generatePayloadHmac(body)
expect(one).toBe(two)
})
})
})