index.js
7 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Load modules
var Lab = require('lab');
var Boom = require('../lib');
// Declare internals
var internals = {};
// Test shortcuts
var expect = Lab.expect;
var before = Lab.before;
var after = Lab.after;
var describe = Lab.experiment;
var it = Lab.test;
describe('Boom', function () {
it('returns an error with info when constructed using another error', function (done) {
var error = new Error('ka-boom');
error.xyz = 123;
var err = new Boom(error);
expect(err.data.xyz).to.equal(123);
expect(err.message).to.equal('ka-boom');
expect(err.response).to.deep.equal({
code: 500,
payload: {
code: 500,
error: 'Internal Server Error',
message: 'ka-boom'
},
headers: {}
});
done();
});
describe('#isBoom', function () {
it('returns true for Boom object', function (done) {
expect(Boom.badRequest().isBoom).to.equal(true);
done();
});
it('returns false for Error object', function (done) {
expect(new Error().isBoom).to.not.exist;
done();
});
});
describe('#badRequest', function () {
it('returns a 400 error code', function (done) {
expect(Boom.badRequest().response.code).to.equal(400);
done();
});
it('sets the message with the passed in message', function (done) {
expect(Boom.badRequest('my message').message).to.equal('my message');
done();
});
});
describe('#unauthorized', function () {
it('returns a 401 error code', function (done) {
var err = Boom.unauthorized();
expect(err.response.code).to.equal(401);
expect(err.response.headers).to.deep.equal({});
done();
});
it('sets the message with the passed in message', function (done) {
expect(Boom.unauthorized('my message').message).to.equal('my message');
done();
});
it('returns a WWW-Authenticate header when passed a scheme', function (done) {
var err = Boom.unauthorized('boom', 'Test');
expect(err.response.code).to.equal(401);
expect(err.response.headers['WWW-Authenticate']).to.equal('Test error="boom"');
done();
});
it('returns a WWW-Authenticate header when passed a scheme and attributes', function (done) {
var err = Boom.unauthorized('boom', 'Test', { a: 1, b: 'something', c: null, d: 0 });
expect(err.response.code).to.equal(401);
expect(err.response.headers['WWW-Authenticate']).to.equal('Test a="1", b="something", c="", d="0", error="boom"');
done();
});
it('sets the isMissing flag when error message is empty', function (done) {
var err = Boom.unauthorized('', 'Basic');
expect(err.isMissing).to.equal(true);
done();
});
it('does not set the isMissing flag when error message is not empty', function (done) {
var err = Boom.unauthorized('message', 'Basic');
expect(err.isMissing).to.equal(undefined);
done();
});
it('sets a WWW-Authenticate when passed as an array', function (done) {
var err = Boom.unauthorized('message', ['Basic', 'Example e="1"', 'Another x="3", y="4"']);
expect(err.response.headers['WWW-Authenticate']).to.equal('Basic, Example e="1", Another x="3", y="4"');
done();
});
});
describe('#clientTimeout', function () {
it('returns a 408 error code', function (done) {
expect(Boom.clientTimeout().response.code).to.equal(408);
done();
});
it('sets the message with the passed in message', function (done) {
expect(Boom.clientTimeout('my message').message).to.equal('my message');
done();
});
});
describe('#serverTimeout', function () {
it('returns a 503 error code', function (done) {
expect(Boom.serverTimeout().response.code).to.equal(503);
done();
});
it('sets the message with the passed in message', function (done) {
expect(Boom.serverTimeout('my message').message).to.equal('my message');
done();
});
});
describe('#forbidden', function () {
it('returns a 403 error code', function (done) {
expect(Boom.forbidden().response.code).to.equal(403);
done();
});
it('sets the message with the passed in message', function (done) {
expect(Boom.forbidden('my message').message).to.equal('my message');
done();
});
});
describe('#notFound', function () {
it('returns a 404 error code', function (done) {
expect(Boom.notFound().response.code).to.equal(404);
done();
});
it('sets the message with the passed in message', function (done) {
expect(Boom.notFound('my message').message).to.equal('my message');
done();
});
});
describe('#internal', function () {
it('returns a 500 error code', function (done) {
expect(Boom.internal().response.code).to.equal(500);
done();
});
it('sets the message with the passed in message', function (done) {
var err = Boom.internal('my message');
expect(err.message).to.equal('my message');
expect(err.response.payload.message).to.equal('An internal server error occurred');
done();
});
it('passes data on the callback if its passed in', function (done) {
expect(Boom.internal('my message', { my: 'data' }).data.my).to.equal('data');
done();
});
it('uses passed in stack if its available', function (done) {
var error = new Error();
error.stack = 'my stack line\nmy second stack line';
expect(Boom.internal('my message', error).trace[0]).to.equal('my stack line');
done();
});
});
describe('#passThrough', function () {
it('returns a pass-through error', function (done) {
var err = Boom.passThrough(499, { a: 1 }, 'application/text', { 'X-Test': 'Boom' });
expect(err.response.code).to.equal(499);
expect(err.message).to.equal('Pass-through');
expect(err.response).to.deep.equal({
code: 499,
payload: { a: 1 },
headers: { 'X-Test': 'Boom' },
type: 'application/text'
});
done();
});
});
describe('#reformat', function () {
it('encodes any HTML markup in the response payload', function (done) {
var boom = new Boom(new Error('<script>alert(1)</script>'));
expect(boom.response.payload.message).to.not.contain('<script>');
done();
});
});
});