-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
300 lines (256 loc) · 7.92 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
eslint
no-multi-spaces: ["error", {exceptions: {"VariableDeclarator": true}}]
padded-blocks: ["error", {"classes": "always"}]
max-len: ["error", 80]
*/
'use strict'
const express = require('express')
const request = require('supertest')
const bodyParser = require('body-parser')
const Joi = require('joi')
const mocha = require('mocha')
const it = mocha.it
const describe = mocha.describe
const before = mocha.before
const validateSchema = require('./')
const app = express()
const someSchema = Joi.object().keys(
{
id: Joi.number().positive().required()
}
)
const customSchema = Joi.string().required()
const headersSchema = Joi.object().keys(
{
host: Joi.string(),
'accept-encoding': Joi.string(),
'user-agent': Joi.string(),
id: Joi.number().positive().required(),
connection: Joi.string().valid('close')
}
)
describe('express midlleware schema validator', () => {
describe('request', () => {
before((done) => {
const router = express.Router()
// query string endpoint
router.get(
'/querystring',
validateSchema({ processHttpCallOnError: true }).query(someSchema),
(req, res) => { res.send('query string') }
)
// params endpoint
router.get(
'/params/:id',
validateSchema({ processHttpCallOnError: true }).params(someSchema),
(req, res) => { res.send('params') }
)
// body endpoint
router.post(
'/body',
validateSchema({ processHttpCallOnError: true }).body(someSchema),
(req, res) => { res.send('body') }
)
// headers endpoint
router.get(
'/headers',
validateSchema({ processHttpCallOnError: true })
.headers(headersSchema),
(req, res) => { res.send('headers') }
)
// custom key validation endpoint
router.get(
'/custom/:foobar?',
(req, res, next) => {
req.foobar = req.params.foobar
next()
},
validateSchema({ processHttpCallOnError: true })
.custom('foobar', customSchema),
(req, res) => { res.send('custom') }
)
// using Joi.validate options
router.get(
'/joi.validate',
validateSchema(
{
validationOptions: { allowUnknown: true },
processHttpCallOnError: true
}
)
.query(someSchema),
(req, res) => { res.send('`joi.validate` options') }
)
// multiple validations
router.put(
'/someresouce/:id',
validateSchema({ processHttpCallOnError: true }).params(someSchema),
validateSchema({ processHttpCallOnError: true })
.body(Joi.object().keys({ name: Joi.string().required() })),
validateSchema(
{
validationOptions: { allowUnknown: true },
processHttpCallOnError: false
}
)
.headers(Joi.object().keys({ hello: Joi.string().required() })),
(req, res) => { res.send('yay!') }
)
// when with an error middleware
router.get(
'/middleware123',
validateSchema().query(someSchema),
(req, res) => { res.send('yay!') }
)
app.use(bodyParser.json())
app.use('/request', router)
app.use((err, req, res, next) => res.send(err.message))
done()
})
it('should throw a 400 when fails on the schema validation',
(done) => {
request(app)
.get('/request/querystring?id=bad')
.expect(
400,
'child "id" fails because ["id" must be a number]',
done
)
}
)
it('should return a 200 when validating a valid param(s)', (done) => {
request(app)
.get('/request/params/123')
.expect(200, 'params', done)
})
it('should return a 200 when validating a valid body', (done) => {
request(app)
.post('/request/body')
.send({ id: 123 })
.expect(200, 'body', done)
})
it('should return a 200 when validating a valid header(s)', (done) => {
request(app)
.get('/request/headers')
.set('id', 123)
.expect(200, 'headers', done)
})
it('should return a 200 when validating a valid query string', (done) => {
request(app)
.get('/request/querystring?id=123')
.expect(200, 'query string', done)
})
it('should return a 200 when validating a valid custom req key', (done) => {
request(app)
.get('/request/custom/foobar')
.expect(200, 'custom', done)
})
it('should return a 400 when fails to validate custom req key', (done) => {
request(app)
.get('/request/custom')
.expect(400, '"value" is required', done)
})
it('should return a 200 when using `joi.validate` options',
(done) => {
request(app)
.get('/request/joi.validate?id=123&hello=world')
.expect(200, '`joi.validate` options', done)
}
)
it('should throw a 400 when fail in one of the validations',
(done) => {
request(app)
.put('/request/someresouce/123')
.set('hello', 'world')
.send({ name: 123 })
.expect(
400,
'child "name" fails because ["name" must be a string]',
done
)
}
)
it('should return a 200 when doing various validations',
(done) => {
request(app)
.put('/request/someresouce/123')
.set('hello', 'world')
.send({ name: 'Joe Doe' })
.expect(200, 'yay!', done)
}
)
it('should return a 400 through a middleware error when setting' +
' `processHttpCallOnError` to false',
(done) => {
request(app)
.get('/request/middleware123')
.expect(400, 'child "id" fails because ["id" is required]', done)
}
)
})
describe('response', () => {
before((done) => {
const router = express.Router()
router.get(
'/good',
validateSchema({ processHttpCallOnError: true })
.response(someSchema),
(req, res) => { res.send({id: 123}) }
)
router.get(
'/processhttpcallimmediate',
validateSchema({ processHttpCallOnError: true })
.response(Joi.object().keys({ value: 'ok' })),
(req, res) => { res.send({id: 123}) }
)
router.get(
'/bad',
validateSchema({ processHttpCallOnError: false })
.response(someSchema),
(req, res) => { res.send({id: 'bad'}) }
)
router.get(
'/502',
validateSchema({ processHttpCallOnError: true })
.response(someSchema),
(req, res) => { res.status(502).send({message: 'alarm!!!'}) }
)
app.use(bodyParser.json())
app.use('/response', router)
app.use((err, req, res, next) => res.send(err.message))
done()
})
it('should follow the server normal behaviour when an ' +
'error happens (server logic) 4.x.x / 5.x.x without json response',
(done) => {
request(app)
.get('/response/dontexists')
.expect(404, done)
}
)
it('should follow the server normal behaviour when an ' +
'error happens (server logic) 4.x.x / 5.x.x with a json response',
(done) => {
request(app)
.get('/response/502')
.expect(502, { message: 'alarm!!!' }, done)
}
)
it('should throw a 500 for an invalid response', (done) => {
request(app)
.get('/response/processhttpcallimmediate')
.expect(500, '"id" is not allowed', done)
})
it('should throw a 500 for an invalid response', (done) => {
request(app)
.get('/response/bad')
.expect(500, 'child "id" fails because ["id" must be a number]', done)
})
it('should return a 200 for a valid response', (done) => {
request(app)
.get('/response/good')
.expect(200, { id: 123 }, done)
})
})
})