-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
error-helper.spec.js
152 lines (138 loc) · 4.68 KB
/
error-helper.spec.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict'
const { expect } = require('chai')
const { checkErrorResponse } = require('./error-helper')
const {
NotFound,
InvalidResponse,
Inaccessible,
} = require('../services/errors')
describe('Standard Error Handler', function() {
it('makes inaccessible badge', function() {
const badgeData = { text: [] }
expect(checkErrorResponse(badgeData, 'something other than null', {})).to.be
.true
expect(badgeData.text[1]).to.equal('inaccessible')
expect(badgeData.colorscheme).to.equal('red')
})
it('makes not found badge', function() {
const badgeData = { text: [] }
expect(checkErrorResponse(badgeData, null, { statusCode: 404 })).to.be.true
expect(badgeData.text[1]).to.equal('not found')
expect(badgeData.colorscheme).to.equal('lightgrey')
})
it('makes not found badge with custom error', function() {
const badgeData = { text: [] }
expect(
checkErrorResponse(
badgeData,
null,
{ statusCode: 404 },
{ 404: 'custom message' }
)
).to.be.true
expect(badgeData.text[1]).to.equal('custom message')
expect(badgeData.colorscheme).to.equal('lightgrey')
})
it('makes invalid badge', function() {
const badgeData = { text: [] }
expect(checkErrorResponse(badgeData, null, { statusCode: 500 })).to.be.true
expect(badgeData.text[1]).to.equal('invalid')
expect(badgeData.colorscheme).to.equal('lightgrey')
})
it('return false on 200 status', function() {
expect(checkErrorResponse({ text: [] }, null, { statusCode: 200 })).to.be
.false
})
})
describe('async error handler', function() {
context('when status is 200', function() {
it('passes through the inputs', async function() {
const args = { buffer: 'buffer', res: { statusCode: 200 } }
expect(await checkErrorResponse.asPromise()(args)).to.deep.equal(args)
})
})
context('when status is 404', function() {
const res = { statusCode: 404 }
it('throws NotFound', async function() {
try {
await checkErrorResponse.asPromise()({ res })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(NotFound)
expect(e.message).to.equal('Not Found')
expect(e.prettyMessage).to.equal('not found')
}
})
it('displays the custom not found message', async function() {
const notFoundMessage = 'no goblins found'
try {
await checkErrorResponse.asPromise({ 404: notFoundMessage })({ res })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(NotFound)
expect(e.message).to.equal('Not Found: no goblins found')
expect(e.prettyMessage).to.equal('no goblins found')
}
})
})
context('when status is 4xx', function() {
it('throws InvalidResponse', async function() {
const res = { statusCode: 499 }
try {
await checkErrorResponse.asPromise()({ res })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
expect(e.message).to.equal(
'Invalid Response: Got status code 499 (expected 200)'
)
expect(e.prettyMessage).to.equal('invalid')
}
})
it('displays the custom error message', async function() {
const res = { statusCode: 403 }
try {
await checkErrorResponse.asPromise({
403: 'access denied',
})({ res })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
expect(e.message).to.equal(
'Invalid Response: Got status code 403 (expected 200)'
)
expect(e.prettyMessage).to.equal('access denied')
}
})
})
context('when status is 5xx', function() {
it('throws Inaccessible', async function() {
const res = { statusCode: 503 }
try {
await checkErrorResponse.asPromise()({ res })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Inaccessible)
expect(e.message).to.equal(
'Inaccessible: Got status code 503 (expected 200)'
)
expect(e.prettyMessage).to.equal('inaccessible')
}
})
it('displays the custom error message', async function() {
const res = { statusCode: 500 }
try {
await checkErrorResponse.asPromise({
500: 'server overloaded',
})({ res })
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Inaccessible)
expect(e.message).to.equal(
'Inaccessible: Got status code 500 (expected 200)'
)
expect(e.prettyMessage).to.equal('server overloaded')
}
})
})
})