forked from joaquimserafim/express-validate-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
248 lines (209 loc) · 6.18 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
/*
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 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) => {
let router = express.Router()
// query string endpoint
router.get(
'/querystring',
validateSchema().query(someSchema),
(req, res) => { res.send('query string') }
)
// params endpoint
router.get(
'/params/:id',
validateSchema().params(someSchema),
(req, res) => { res.send('params') }
)
// body endpoint
router.post(
'/body',
validateSchema().body(someSchema),
(req, res) => { res.send('body') }
)
// headers endpoint
router.get(
'/headers',
validateSchema().headers(headersSchema),
(req, res) => { res.send('headers') }
)
// using Joi.validate options
router.get(
'/joi.validate',
validateSchema({allowUnknown: true}).query(someSchema),
(req, res) => { res.send('`joi.validate` options') }
)
// multiple validations
router.put(
'/someresouce/:id',
validateSchema().params(someSchema),
validateSchema()
.body(Joi.object().keys({name: Joi.string().required()})),
validateSchema({allowUnknown: true})
.headers(Joi.object().keys({hello: Joi.string().required()})),
(req, res) => { res.send('yay!') }
)
app.use(bodyParser.json())
app.use('/request', router)
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 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 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)
}
)
})
describe('response', () => {
before((done) => {
let router = express.Router()
router.get(
'/good',
validateSchema().response(someSchema),
(req, res) => { res.send({id: 123}) }
)
router.get(
'/bad',
validateSchema().response(someSchema),
(req, res) => { res.send({id: 'bad'}) }
)
router.get(
'/502',
validateSchema().response(someSchema),
(req, res) => { res.status(502).send({message: 'alarm!!!'}) }
)
app.use(bodyParser.json())
app.use('/response', router)
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/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)
})
})
})