-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
73 lines (55 loc) · 1.74 KB
/
test.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
'use strict'
const { test } = require('tap')
const Fastify = require('fastify')
const Boom = require('@hapi/boom')
const boomPlugin = require('.')
test('set the default error', (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(boomPlugin)
fastify.get('/', (request, reply) => {
reply.code(401).send(new Error('invalid password'))
})
fastify.inject({ method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res.statusCode, 401)
t.equal(res.statusMessage, 'Unauthorized')
t.include(JSON.parse(res.payload), { message: 'invalid password' })
fastify.close()
})
})
test('set the boom error without plugin', (t) => {
t.plan(5)
const fastify = Fastify()
fastify.get('/', (request, reply) => {
reply.send(Boom.unauthorized('invalid password', 'sample'))
})
fastify.inject({ method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.doesNotHave(res.headers, {
'www-authenticate': 'sample error="invalid password"'
})
t.equal(res.statusCode, 500)
t.equal(res.statusMessage, 'Internal Server Error')
t.include(JSON.parse(res.payload), { message: 'invalid password' })
fastify.close()
})
})
test('set the boom error', (t) => {
t.plan(5)
const fastify = Fastify()
fastify.register(boomPlugin)
fastify.get('/boom', (request, reply) => {
reply.send(Boom.unauthorized('invalid password', 'sample'))
})
fastify.inject({ method: 'GET', url: '/boom' }, (err, res) => {
t.error(err)
t.include(res.headers, {
'www-authenticate': 'sample error="invalid password"'
})
t.equal(res.statusCode, 401)
t.equal(res.statusMessage, 'Unauthorized')
t.include(JSON.parse(res.payload), { message: 'invalid password' })
fastify.close()
})
})