-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
handleRequest.test.js
281 lines (261 loc) · 6.76 KB
/
handleRequest.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
'use strict'
const { test } = require('tap')
const semver = require('semver')
const handleRequest = require('../../lib/handleRequest')
const internals = require('../../lib/handleRequest')[Symbol.for('internals')]
const Request = require('../../lib/request')
const Reply = require('../../lib/reply')
const buildSchema = require('../../lib/validation').compileSchemasForValidation
const sget = require('simple-get').concat
const Ajv = require('ajv')
const ajv = new Ajv({ coerceTypes: true })
function schemaValidator ({ schema, method, url, httpPart }) {
const validateFuncion = ajv.compile(schema)
const fn = function (body) {
const isOk = validateFuncion(body)
if (isOk) return
return false
}
fn.errors = []
return fn
}
test('handleRequest function - sent reply', t => {
t.plan(1)
const request = {}
const reply = { sent: true }
const res = handleRequest(null, request, reply)
t.equal(res, undefined)
})
test('handleRequest function - invoke with error', t => {
t.plan(1)
const request = {}
const reply = {}
reply.send = (err) => t.equal(err.message, 'Kaboom')
handleRequest(new Error('Kaboom'), request, reply)
})
test('handler function - invalid schema', t => {
t.plan(2)
const res = {}
res.end = () => {
t.equal(res.statusCode, 400)
t.pass()
}
res.writeHead = () => {}
res.log = { error: () => {}, info: () => {} }
const context = {
config: {
method: 'GET',
url: '/an-url'
},
schema: {
body: {
type: 'object',
properties: {
hello: { type: 'number' }
}
}
},
handler: () => {},
Reply,
Request,
preValidation: [],
preHandler: [],
onSend: [],
onError: [],
attachValidation: false,
schemaErrorFormatter: () => new Error()
}
buildSchema(context, schemaValidator)
const request = {
body: { hello: 'world' },
context
}
internals.handler(request, new Reply(res, request))
})
test('handler function - reply', t => {
t.plan(3)
const res = {}
res.end = () => {
t.equal(res.statusCode, 204)
t.pass()
}
res.writeHead = () => {}
const context = {
handler: (req, reply) => {
t.equal(typeof reply, 'object')
reply.code(204)
reply.send(undefined)
},
Reply,
Request,
preValidation: [],
preHandler: [],
onSend: [],
onError: []
}
buildSchema(context, schemaValidator)
internals.handler({}, new Reply(res, { context }))
})
test('handler function - preValidationCallback with finished response', t => {
t.plan(0)
const res = {}
// Be sure to check only `writableEnded` where is available
if (semver.gte(process.versions.node, '12.9.0')) {
res.writableEnded = true
} else {
res.writable = false
res.finished = true
}
res.end = () => {
t.fail()
}
res.writeHead = () => {}
const context = {
handler: (req, reply) => {
t.fail()
reply.send(undefined)
},
Reply,
Request,
preValidation: null,
preHandler: [],
onSend: [],
onError: []
}
buildSchema(context, schemaValidator)
internals.handler({}, new Reply(res, { context }))
})
test('handler function - preValidationCallback with finished response (< v12.9.0)', t => {
t.plan(0)
const res = {}
// Be sure to check only `writableEnded` where is available
res.writable = false
res.finished = true
res.end = () => {
t.fail()
}
res.writeHead = () => {}
const context = {
handler: (req, reply) => {
t.fail()
reply.send(undefined)
},
Reply,
Request,
preValidation: null,
preHandler: [],
onSend: [],
onError: []
}
buildSchema(context, schemaValidator)
internals.handler({}, new Reply(res, { context }))
})
test('request should be defined in onSend Hook on post request with content type application/json', t => {
t.plan(8)
const fastify = require('../..')()
fastify.addHook('onSend', (request, reply, payload, done) => {
t.ok(request)
t.ok(request.raw)
t.ok(request.id)
t.ok(request.params)
t.ok(request.query)
done()
})
fastify.post('/', (request, reply) => {
reply.send(200)
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port,
headers: {
'content-type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
// a 400 error is expected because of no body
t.equal(response.statusCode, 400)
})
})
})
test('request should be defined in onSend Hook on post request with content type application/x-www-form-urlencoded', t => {
t.plan(7)
const fastify = require('../..')()
fastify.addHook('onSend', (request, reply, payload, done) => {
t.ok(request)
t.ok(request.raw)
t.ok(request.params)
t.ok(request.query)
done()
})
fastify.post('/', (request, reply) => {
reply.send(200)
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port,
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}, (err, response, body) => {
t.error(err)
// a 415 error is expected because of missing content type parser
t.equal(response.statusCode, 415)
})
})
})
test('request should be defined in onSend Hook on options request with content type application/x-www-form-urlencoded', t => {
t.plan(7)
const fastify = require('../..')()
fastify.addHook('onSend', (request, reply, payload, done) => {
t.ok(request)
t.ok(request.raw)
t.ok(request.params)
t.ok(request.query)
done()
})
fastify.options('/', (request, reply) => {
reply.send(200)
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
sget({
method: 'OPTIONS',
url: 'http://localhost:' + fastify.server.address().port,
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}, (err, response, body) => {
t.error(err)
// Body parsing skipped, so no body sent
t.equal(response.statusCode, 200)
})
})
})
test('request should respond with an error if an unserialized payload is sent inside an async handler', t => {
t.plan(3)
const fastify = require('../..')()
fastify.get('/', (request, reply) => {
reply.type('text/html')
return Promise.resolve(request.headers)
})
fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 500)
t.strictSame(JSON.parse(res.payload), {
error: 'Internal Server Error',
code: 'FST_ERR_REP_INVALID_PAYLOAD_TYPE',
message: 'Attempted to send payload of invalid type \'object\'. Expected a string or Buffer.',
statusCode: 500
})
})
})