strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const { Readable } = require('node:stream')5const Fastify = require('..')6 7test('code should handle null/undefined/float', (t, done) => {8 t.plan(8)9 10 const fastify = Fastify()11 12 fastify.get('/null', function (request, reply) {13 reply.status(null).send()14 })15 16 fastify.get('/undefined', function (request, reply) {17 reply.status(undefined).send()18 })19 20 fastify.get('/404.5', function (request, reply) {21 reply.status(404.5).send()22 })23 24 fastify.inject({25 method: 'GET',26 url: '/null'27 }, (error, res) => {28 t.assert.ifError(error)29 t.assert.strictEqual(res.statusCode, 500)30 t.assert.deepStrictEqual(res.json(), {31 statusCode: 500,32 code: 'FST_ERR_BAD_STATUS_CODE',33 error: 'Internal Server Error',34 message: 'Called reply with an invalid status code: null'35 })36 })37 38 fastify.inject({39 method: 'GET',40 url: '/undefined'41 }, (error, res) => {42 t.assert.ifError(error)43 t.assert.strictEqual(res.statusCode, 500)44 t.assert.deepStrictEqual(res.json(), {45 statusCode: 500,46 code: 'FST_ERR_BAD_STATUS_CODE',47 error: 'Internal Server Error',48 message: 'Called reply with an invalid status code: undefined'49 })50 })51 52 fastify.inject({53 method: 'GET',54 url: '/404.5'55 }, (error, res) => {56 t.assert.ifError(error)57 t.assert.strictEqual(res.statusCode, 404)58 done()59 })60})61 62test('code should handle 204', (t, done) => {63 t.plan(13)64 65 const fastify = Fastify()66 67 fastify.get('/204', function (request, reply) {68 reply.status(204)69 return null70 })71 72 fastify.get('/undefined/204', function (request, reply) {73 reply.status(204).send({ message: 'hello' })74 })75 76 fastify.get('/stream/204', function (request, reply) {77 const stream = new Readable({78 read () {79 this.push(null)80 }81 })82 stream.on('end', () => {83 t.assert.ok('stream ended')84 })85 reply.status(204).send(stream)86 })87 88 fastify.inject({89 method: 'GET',90 url: '/204'91 }, (error, res) => {92 t.assert.ifError(error)93 t.assert.strictEqual(res.statusCode, 204)94 t.assert.strictEqual(res.payload, '')95 t.assert.strictEqual(res.headers['content-length'], undefined)96 })97 98 fastify.inject({99 method: 'GET',100 url: '/undefined/204'101 }, (error, res) => {102 t.assert.ifError(error)103 t.assert.strictEqual(res.statusCode, 204)104 t.assert.strictEqual(res.payload, '')105 t.assert.strictEqual(res.headers['content-length'], undefined)106 })107 108 fastify.inject({109 method: 'GET',110 url: '/stream/204'111 }, (error, res) => {112 t.assert.ifError(error)113 t.assert.strictEqual(res.statusCode, 204)114 t.assert.strictEqual(res.payload, '')115 t.assert.strictEqual(res.headers['content-length'], undefined)116 done()117 })118})119 120test('code should handle onSend hook on 204', (t, done) => {121 t.plan(5)122 123 const fastify = Fastify()124 fastify.addHook('onSend', async function (request, reply, payload) {125 return {126 ...payload,127 world: 'hello'128 }129 })130 131 fastify.get('/204', function (request, reply) {132 reply.status(204).send({133 hello: 'world'134 })135 })136 137 fastify.inject({138 method: 'GET',139 url: '/204'140 }, (error, res) => {141 t.assert.ifError(error)142 t.assert.strictEqual(res.statusCode, 204)143 t.assert.strictEqual(res.payload, '')144 t.assert.strictEqual(res.headers['content-length'], undefined)145 t.assert.strictEqual(res.headers['content-type'], undefined)146 done()147 })148})149 