strong-tie/inbound-calls
0
1'use strict'2 3const t = require('tap')4const test = t.test5const Stream = require('node:stream')6const util = require('node:util')7const Fastify = require('..')8const { Readable } = require('node:stream')9 10test('inject should exist', t => {11 t.plan(2)12 const fastify = Fastify()13 t.ok(fastify.inject)14 t.equal(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, done) => {23 instance.get('/', (req, reply) => {24 reply.send(payload)25 })26 27 setTimeout(done, 500)28 })29 30 fastify.inject({31 method: 'GET',32 url: '/'33 }, (err, res) => {34 t.error(err)35 t.same(payload, JSON.parse(res.payload))36 t.equal(res.statusCode, 200)37 t.equal(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.same(payload, JSON.parse(res.payload))56 t.equal(res.statusCode, 200)57 t.equal(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.same(payload, JSON.parse(res.payload))76 t.equal(res.statusCode, 201)77 t.equal(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.equal('', res.payload)95 t.equal(res.headers['content-type'], 'text/plain')96 t.equal(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.same({ hello: 'world' }, JSON.parse(res.payload))114 t.equal(res.statusCode, 200)115 t.equal(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.same({ hello: 'world' }, JSON.parse(res.payload))133 t.equal(res.statusCode, 200)134 t.equal(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.same({ '*': 'wildcard' }, JSON.parse(res.payload))152 t.equal(res.statusCode, 200)153 t.equal(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.equal('world', JSON.parse(res.payload).hello)172 t.equal(res.statusCode, 200)173 t.equal(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 payload190 }, (err, res) => {191 t.error(err)192 t.same(payload, JSON.parse(res.payload))193 t.equal(res.statusCode, 200)194 t.equal(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.same('{"hello":"world"}', res.payload)214 t.equal(res.statusCode, 200)215 t.equal(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.same('{"hello":"world"}', res.payload)233 t.equal(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.equal(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.equal(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" flag294 // 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.equal(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, done) => {315 setTimeout(done, 500, error)316 })317 318 fastify.inject({319 method: 'GET',320 url: '/'321 })322 .catch(e => {323 t.equal(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, payload, done) {332 let body = ''333 payload.on('data', d => {334 body += d335 })336 payload.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.set('my_field', 'my value')346 347 fastify.inject({348 method: 'POST',349 url: '/',350 payload: form351 })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#L836359function getStream () {360 const Read = function () {361 Stream.Readable.call(this)362 }363 util.inherits(Read, Stream.Readable)364 const word = '{"hello":"world"}'365 let i = 0366 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.equal(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.equal(err, error)410 })411})412 413test('should support builder-style injection with ready app', async (t) => {414 t.plan(3)415 const fastify = Fastify()416 const payload = { hello: 'world' }417 418 fastify.get('/', (req, reply) => {419 reply.send(payload)420 })421 422 await fastify.ready()423 const res = await fastify.inject().get('/').end()424 t.same(payload, JSON.parse(res.payload))425 t.equal(res.statusCode, 200)426 t.equal(res.headers['content-length'], '17')427})428 429test('should support builder-style injection with non-ready app', async (t) => {430 t.plan(3)431 const fastify = Fastify()432 const payload = { hello: 'world' }433 434 fastify.get('/', (req, reply) => {435 reply.send(payload)436 })437 438 const res = await fastify.inject().get('/').end()439 t.same(payload, JSON.parse(res.payload))440 t.equal(res.statusCode, 200)441 t.equal(res.headers['content-length'], '17')442})443 444test('should handle errors in builder-style injection correctly', async (t) => {445 t.plan(2)446 const fastify = Fastify()447 fastify.register((instance, opts, done) => {448 done(new Error('Kaboom'))449 })450 451 try {452 await fastify.inject().get('/')453 } catch (err) {454 t.ok(err)455 t.equal(err.message, 'Kaboom')456 }457})458 459test('Should not throw on access to routeConfig frameworkErrors handler - FST_ERR_BAD_URL', t => {460 t.plan(5)461 462 const fastify = Fastify({463 frameworkErrors: function (err, req, res) {464 t.ok(typeof req.id === 'string')465 t.ok(req.raw instanceof Readable)466 t.same(req.routerPath, undefined)467 res.send(`${err.message} - ${err.code}`)468 }469 })470 471 fastify.get('/test/:id', (req, res) => {472 res.send('{ hello: \'world\' }')473 })474 475 fastify.inject(476 {477 method: 'GET',478 url: '/test/%world'479 },480 (err, res) => {481 t.error(err)482 t.equal(res.body, '\'/test/%world\' is not a valid url component - FST_ERR_BAD_URL')483 }484 )485})486 