UNPKG

8.71 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const test = t.test
5const Stream = require('stream')
6const util = require('util')
7const Fastify = require('..')
8const FormData = require('form-data')
9
10test('inject should exist', t => {
11 t.plan(2)
12 const fastify = Fastify()
13 t.ok(fastify.inject)
14 t.is(typeof fastify.inject, 'function')
15})
16
17test('should wait for the ready event', t => {
18 t.plan(4)
19 const fastify = Fastify()
20 const payload = { hello: 'world' }
21
22 fastify.register((instance, opts, next) => {
23 instance.get('/', (req, reply) => {
24 reply.send(payload)
25 })
26
27 setTimeout(next, 500)
28 })
29
30 fastify.inject({
31 method: 'GET',
32 url: '/'
33 }, (err, res) => {
34 t.error(err)
35 t.deepEqual(payload, JSON.parse(res.payload))
36 t.strictEqual(res.statusCode, 200)
37 t.strictEqual(res.headers['content-length'], '17')
38 })
39})
40
41test('inject get request', t => {
42 t.plan(4)
43 const fastify = Fastify()
44 const payload = { hello: 'world' }
45
46 fastify.get('/', (req, reply) => {
47 reply.send(payload)
48 })
49
50 fastify.inject({
51 method: 'GET',
52 url: '/'
53 }, (err, res) => {
54 t.error(err)
55 t.deepEqual(payload, JSON.parse(res.payload))
56 t.strictEqual(res.statusCode, 200)
57 t.strictEqual(res.headers['content-length'], '17')
58 })
59})
60
61test('inject get request - code check', t => {
62 t.plan(4)
63 const fastify = Fastify()
64 const payload = { hello: 'world' }
65
66 fastify.get('/', (req, reply) => {
67 reply.code(201).send(payload)
68 })
69
70 fastify.inject({
71 method: 'GET',
72 url: '/'
73 }, (err, res) => {
74 t.error(err)
75 t.deepEqual(payload, JSON.parse(res.payload))
76 t.strictEqual(res.statusCode, 201)
77 t.strictEqual(res.headers['content-length'], '17')
78 })
79})
80
81test('inject get request - headers check', t => {
82 t.plan(4)
83 const fastify = Fastify()
84
85 fastify.get('/', (req, reply) => {
86 reply.header('content-type', 'text/plain').send('')
87 })
88
89 fastify.inject({
90 method: 'GET',
91 url: '/'
92 }, (err, res) => {
93 t.error(err)
94 t.strictEqual('', res.payload)
95 t.strictEqual(res.headers['content-type'], 'text/plain')
96 t.strictEqual(res.headers['content-length'], '0')
97 })
98})
99
100test('inject get request - querystring', t => {
101 t.plan(4)
102 const fastify = Fastify()
103
104 fastify.get('/', (req, reply) => {
105 reply.send(req.query)
106 })
107
108 fastify.inject({
109 method: 'GET',
110 url: '/?hello=world'
111 }, (err, res) => {
112 t.error(err)
113 t.deepEqual({ hello: 'world' }, JSON.parse(res.payload))
114 t.strictEqual(res.statusCode, 200)
115 t.strictEqual(res.headers['content-length'], '17')
116 })
117})
118
119test('inject get request - params', t => {
120 t.plan(4)
121 const fastify = Fastify()
122
123 fastify.get('/:hello', (req, reply) => {
124 reply.send(req.params)
125 })
126
127 fastify.inject({
128 method: 'GET',
129 url: '/world'
130 }, (err, res) => {
131 t.error(err)
132 t.deepEqual({ hello: 'world' }, JSON.parse(res.payload))
133 t.strictEqual(res.statusCode, 200)
134 t.strictEqual(res.headers['content-length'], '17')
135 })
136})
137
138test('inject get request - wildcard', t => {
139 t.plan(4)
140 const fastify = Fastify()
141
142 fastify.get('/test/*', (req, reply) => {
143 reply.send(req.params)
144 })
145
146 fastify.inject({
147 method: 'GET',
148 url: '/test/wildcard'
149 }, (err, res) => {
150 t.error(err)
151 t.deepEqual({ '*': 'wildcard' }, JSON.parse(res.payload))
152 t.strictEqual(res.statusCode, 200)
153 t.strictEqual(res.headers['content-length'], '16')
154 })
155})
156
157test('inject get request - headers', t => {
158 t.plan(4)
159 const fastify = Fastify()
160
161 fastify.get('/', (req, reply) => {
162 reply.send(req.headers)
163 })
164
165 fastify.inject({
166 method: 'GET',
167 url: '/',
168 headers: { hello: 'world' }
169 }, (err, res) => {
170 t.error(err)
171 t.strictEqual('world', JSON.parse(res.payload).hello)
172 t.strictEqual(res.statusCode, 200)
173 t.strictEqual(res.headers['content-length'], '69')
174 })
175})
176
177test('inject post request', t => {
178 t.plan(4)
179 const fastify = Fastify()
180 const payload = { hello: 'world' }
181
182 fastify.post('/', (req, reply) => {
183 reply.send(req.body)
184 })
185
186 fastify.inject({
187 method: 'POST',
188 url: '/',
189 payload: payload
190 }, (err, res) => {
191 t.error(err)
192 t.deepEqual(payload, JSON.parse(res.payload))
193 t.strictEqual(res.statusCode, 200)
194 t.strictEqual(res.headers['content-length'], '17')
195 })
196})
197
198test('inject post request - send stream', t => {
199 t.plan(4)
200 const fastify = Fastify()
201
202 fastify.post('/', (req, reply) => {
203 reply.send(req.body)
204 })
205
206 fastify.inject({
207 method: 'POST',
208 url: '/',
209 headers: { 'content-type': 'application/json' },
210 payload: getStream()
211 }, (err, res) => {
212 t.error(err)
213 t.deepEqual('{"hello":"world"}', res.payload)
214 t.strictEqual(res.statusCode, 200)
215 t.strictEqual(res.headers['content-length'], '17')
216 })
217})
218
219test('inject get request - reply stream', t => {
220 t.plan(3)
221 const fastify = Fastify()
222
223 fastify.get('/', (req, reply) => {
224 reply.send(getStream())
225 })
226
227 fastify.inject({
228 method: 'GET',
229 url: '/'
230 }, (err, res) => {
231 t.error(err)
232 t.deepEqual('{"hello":"world"}', res.payload)
233 t.strictEqual(res.statusCode, 200)
234 })
235})
236
237test('inject promisify - waiting for ready event', t => {
238 t.plan(1)
239 const fastify = Fastify()
240 const payload = { hello: 'world' }
241
242 fastify.get('/', (req, reply) => {
243 reply.send(payload)
244 })
245
246 const injectParams = {
247 method: 'GET',
248 url: '/'
249 }
250 fastify.inject(injectParams)
251 .then(res => {
252 t.strictEqual(res.statusCode, 200)
253 })
254 .catch(t.fail)
255})
256
257test('inject promisify - after the ready event', t => {
258 t.plan(2)
259 const fastify = Fastify()
260 const payload = { hello: 'world' }
261
262 fastify.get('/', (req, reply) => {
263 reply.send(payload)
264 })
265
266 fastify.ready(err => {
267 t.error(err)
268
269 const injectParams = {
270 method: 'GET',
271 url: '/'
272 }
273 fastify.inject(injectParams)
274 .then(res => {
275 t.strictEqual(res.statusCode, 200)
276 })
277 .catch(t.fail)
278 })
279})
280
281test('inject promisify - when the server is up', t => {
282 t.plan(2)
283 const fastify = Fastify()
284 const payload = { hello: 'world' }
285
286 fastify.get('/', (req, reply) => {
287 reply.send(payload)
288 })
289
290 fastify.ready(err => {
291 t.error(err)
292
293 // setTimeout because the ready event don't set "started" flag
294 // in this iteration of the 'event loop'
295 setTimeout(() => {
296 const injectParams = {
297 method: 'GET',
298 url: '/'
299 }
300 fastify.inject(injectParams)
301 .then(res => {
302 t.strictEqual(res.statusCode, 200)
303 })
304 .catch(t.fail)
305 }, 10)
306 })
307})
308
309test('should reject in error case', t => {
310 t.plan(1)
311 const fastify = Fastify()
312
313 const error = new Error('DOOM!')
314 fastify.register((instance, opts, next) => {
315 setTimeout(next, 500, error)
316 })
317
318 fastify.inject({
319 method: 'GET',
320 url: '/'
321 })
322 .catch(e => {
323 t.strictEqual(e, error)
324 })
325})
326
327test('inject a multipart request using form-body', t => {
328 t.plan(2)
329 const fastify = Fastify()
330
331 fastify.addContentTypeParser('*', function (req, done) {
332 var body = ''
333 req.on('data', d => {
334 body += d
335 })
336 req.on('end', () => {
337 done(null, body)
338 })
339 })
340 fastify.post('/', (req, reply) => {
341 reply.send(req.body)
342 })
343
344 const form = new FormData()
345 form.append('my_field', 'my value')
346
347 fastify.inject({
348 method: 'POST',
349 url: '/',
350 payload: form
351 })
352 .then(response => {
353 t.equal(response.statusCode, 200)
354 t.ok(/Content-Disposition: form-data; name="my_field"/.test(response.payload))
355 })
356})
357
358// https://github.com/hapijs/shot/blob/master/test/index.js#L836
359function getStream () {
360 const Read = function () {
361 Stream.Readable.call(this)
362 }
363 util.inherits(Read, Stream.Readable)
364 const word = '{"hello":"world"}'
365 var i = 0
366
367 Read.prototype._read = function (size) {
368 this.push(word[i] ? word[i++] : null)
369 }
370
371 return new Read()
372}
373
374test('should error the promise if ready errors', t => {
375 t.plan(3)
376 const fastify = Fastify()
377
378 fastify.register((instance, opts) => {
379 return Promise.reject(new Error('kaboom'))
380 }).after(function () {
381 t.pass('after is called')
382 })
383
384 fastify.inject({
385 method: 'GET',
386 url: '/'
387 }).then(() => {
388 t.fail('this should not be called')
389 }).catch(err => {
390 t.ok(err)
391 t.strictequal(err.message, 'kaboom')
392 })
393})
394
395test('should throw error if callback specified and if ready errors', t => {
396 t.plan(2)
397 const fastify = Fastify()
398 const error = new Error('kaboom')
399
400 fastify.register((instance, opts) => {
401 return Promise.reject(error)
402 })
403
404 fastify.inject({
405 method: 'GET',
406 url: '/'
407 }, err => {
408 t.ok(err)
409 t.strictEqual(err, error)
410 })
411})