strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const fastify = require('..')5 6test('same shape on Request', async (t) => {7 t.plan(1)8 9 const app = fastify()10 11 let request12 13 app.decorateRequest('user')14 15 app.addHook('preHandler', (req, reply, done) => {16 if (request) {17 req.user = 'User'18 }19 done()20 })21 22 app.get('/', (req, reply) => {23 if (request) {24 t.assert.deepStrictEqual(request, req)25 }26 27 request = req28 29 return 'hello world'30 })31 32 await app.inject('/')33 await app.inject('/')34})35 36test('same shape on Request when object', async (t) => {37 t.plan(1)38 39 const app = fastify()40 41 let request42 43 app.decorateRequest('object', null)44 45 app.addHook('preHandler', (req, reply, done) => {46 if (request) {47 req.object = {}48 }49 done()50 })51 52 app.get('/', (req, reply) => {53 if (request) {54 t.assert.deepStrictEqual(request, req)55 }56 57 request = req58 59 return 'hello world'60 })61 62 await app.inject('/')63 await app.inject('/')64})65 66test('same shape on Reply', async (t) => {67 t.plan(1)68 69 const app = fastify()70 71 let _reply72 73 app.decorateReply('user')74 75 app.addHook('preHandler', (req, reply, done) => {76 if (_reply) {77 reply.user = 'User'78 }79 done()80 })81 82 app.get('/', (req, reply) => {83 if (_reply) {84 t.assert.deepStrictEqual(_reply, reply)85 }86 87 _reply = reply88 89 return 'hello world'90 })91 92 await app.inject('/')93 await app.inject('/')94})95 96test('same shape on Reply when object', async (t) => {97 t.plan(1)98 99 const app = fastify()100 101 let _reply102 103 app.decorateReply('object', null)104 105 app.addHook('preHandler', (req, reply, done) => {106 if (_reply) {107 reply.object = {}108 }109 done()110 })111 112 app.get('/', (req, reply) => {113 if (_reply) {114 t.assert.deepStrictEqual(_reply, reply)115 }116 117 _reply = reply118 119 return 'hello world'120 })121 122 await app.inject('/')123 await app.inject('/')124})125 