-
Notifications
You must be signed in to change notification settings - Fork 8
/
plugin-renders-view.js
72 lines (60 loc) · 1.63 KB
/
plugin-renders-view.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict'
const Test = require('ava')
const Path = require('path')
const Hapi = require('@hapi/hapi')
const Vision = require('@hapi/vision')
const Handlebars = require('handlebars')
async function setupServer (options) {
const server = new Hapi.Server()
await server.register([
{
plugin: Vision
},
{
plugin: require('../lib'),
options: Object.assign({
view: 'rate-limit-exceeded',
max: 1,
duration: 1000,
namespace: `view-limits-${Date.now()}`
}, options)
}])
server.views({
engines: {
html: Handlebars
},
path: Path.resolve(__dirname, 'views')
})
await server.initialize()
return server
}
Test('plugin renders a view', async (t) => {
const server = await setupServer()
server.route({
method: 'GET',
path: '/',
handler: () => {
return 'This is rate limitoooooooor!'
}
})
const request = {
url: '/',
method: 'GET'
}
const response1 = await server.inject(request)
t.is(response1.statusCode, 200)
t.is(response1.headers['x-rate-limit-limit'], 1)
t.is(response1.headers['x-rate-limit-remaining'], 0)
t.not(response1.headers['x-rate-limit-reset'], null)
const response2 = await server.inject(request)
t.is(response2.statusCode, 429)
t.true(response2.payload.includes('You’ve exceeded the rate limit.'))
t.is(response2.headers['x-rate-limit-limit'], 1)
t.is(response2.headers['x-rate-limit-remaining'], 0)
t.not(response2.headers['x-rate-limit-reset'], null)
})
Test('plugin fails for missing view', async (t) => {
await t.throwsAsync(
setupServer({ view: 'not-existing-view' })
)
})