-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplugin-uses-custom-extension-point.js
89 lines (73 loc) · 1.9 KB
/
plugin-uses-custom-extension-point.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict'
const Test = require('ava')
const Hapi = require('@hapi/hapi')
Test.beforeEach('Render view when rate limit is exceeded,', async ({ context }) => {
const server = new Hapi.Server()
await server.register(require('@hapi/basic'))
await server.auth.strategy('basic', 'basic', {
validate: () => {
return { isValid: false }
}
})
context.server = server
})
Test('plugin rate limits request onPreAuth', async (t) => {
const server = t.context.server
await server.register([
{
plugin: require('../lib'),
options: {
max: 20,
extensionPoint: 'onPreAuth',
namespace: `view-limits-${Date.now()}`
}
}
])
server.route({
method: 'GET',
path: '/',
options: {
auth: 'basic',
handler: () => 'This is rate limitoooooooor!'
}
})
const request = {
url: '/',
method: 'GET'
}
const response = await server.inject(request)
t.is(response.statusCode, 401)
t.is(response.headers['x-rate-limit-limit'], 20)
t.is(response.headers['x-rate-limit-remaining'], 19)
t.not(response.headers['x-rate-limit-reset'], undefined)
})
Test('plugin does not rate limit at onPostAuth because auth fails', async (t) => {
const server = t.context.server
await server.register([
{
plugin: require('../lib'),
options: {
max: 20,
extensionPoint: 'onPostAuth',
namespace: `view-limits-${Date.now()}`
}
}
])
server.route({
method: 'GET',
path: '/',
options: {
auth: 'basic',
handler: () => 'This is rate limitoooooooor!'
}
})
const request = {
url: '/',
method: 'GET'
}
const response = await server.inject(request)
t.is(response.statusCode, 401)
t.is(response.headers['x-rate-limit-limit'], undefined)
t.is(response.headers['x-rate-limit-remaining'], undefined)
t.is(response.headers['x-rate-limit-reset'], undefined)
})