strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('../fastify')5const fs = require('node:fs')6const { Readable } = require('node:stream')7const { fetch: undiciFetch } = require('undici')8 9test('should response with a ReadableStream', async (t) => {10 t.plan(2)11 12 const fastify = Fastify()13 14 fastify.get('/', function (request, reply) {15 const stream = fs.createReadStream(__filename)16 reply.code(200).send(Readable.toWeb(stream))17 })18 19 const {20 statusCode,21 body22 } = await fastify.inject({ method: 'GET', path: '/' })23 24 const expected = await fs.promises.readFile(__filename)25 26 t.assert.strictEqual(statusCode, 200)27 t.assert.strictEqual(expected.toString(), body.toString())28})29 30test('should response with a Response', async (t) => {31 t.plan(3)32 33 const fastify = Fastify()34 35 fastify.get('/', function (request, reply) {36 const stream = fs.createReadStream(__filename)37 reply.send(new Response(Readable.toWeb(stream), {38 status: 200,39 headers: {40 hello: 'world'41 }42 }))43 })44 45 const {46 statusCode,47 headers,48 body49 } = await fastify.inject({ method: 'GET', path: '/' })50 51 const expected = await fs.promises.readFile(__filename)52 53 t.assert.strictEqual(statusCode, 200)54 t.assert.strictEqual(expected.toString(), body.toString())55 t.assert.strictEqual(headers.hello, 'world')56})57 58test('should response with a Response 204', async (t) => {59 t.plan(3)60 61 const fastify = Fastify()62 63 fastify.get('/', function (request, reply) {64 reply.send(new Response(null, {65 status: 204,66 headers: {67 hello: 'world'68 }69 }))70 })71 72 const {73 statusCode,74 headers,75 body76 } = await fastify.inject({ method: 'GET', path: '/' })77 78 t.assert.strictEqual(statusCode, 204)79 t.assert.strictEqual(body, '')80 t.assert.strictEqual(headers.hello, 'world')81})82 83test('should response with a Response 304', async (t) => {84 t.plan(3)85 86 const fastify = Fastify()87 88 fastify.get('/', function (request, reply) {89 reply.send(new Response(null, {90 status: 304,91 headers: {92 hello: 'world'93 }94 }))95 })96 97 const {98 statusCode,99 headers,100 body101 } = await fastify.inject({ method: 'GET', path: '/' })102 103 t.assert.strictEqual(statusCode, 304)104 t.assert.strictEqual(body, '')105 t.assert.strictEqual(headers.hello, 'world')106})107 108test('should response with a Response without body', async (t) => {109 t.plan(3)110 111 const fastify = Fastify()112 113 fastify.get('/', function (request, reply) {114 reply.send(new Response(null, {115 status: 200,116 headers: {117 hello: 'world'118 }119 }))120 })121 122 const {123 statusCode,124 headers,125 body126 } = await fastify.inject({ method: 'GET', path: '/' })127 128 t.assert.strictEqual(statusCode, 200)129 t.assert.strictEqual(body, '')130 t.assert.strictEqual(headers.hello, 'world')131})132 133test('able to use in onSend hook - ReadableStream', async (t) => {134 t.plan(4)135 136 const fastify = Fastify()137 138 fastify.get('/', function (request, reply) {139 const stream = fs.createReadStream(__filename)140 reply.code(500).send(Readable.toWeb(stream))141 })142 143 fastify.addHook('onSend', (request, reply, payload, done) => {144 t.assert.strictEqual(Object.prototype.toString.call(payload), '[object ReadableStream]')145 done(null, new Response(payload, {146 status: 200,147 headers: {148 hello: 'world'149 }150 }))151 })152 153 const {154 statusCode,155 headers,156 body157 } = await fastify.inject({ method: 'GET', path: '/' })158 159 const expected = await fs.promises.readFile(__filename)160 161 t.assert.strictEqual(statusCode, 200)162 t.assert.strictEqual(expected.toString(), body.toString())163 t.assert.strictEqual(headers.hello, 'world')164})165 166test('able to use in onSend hook - Response', async (t) => {167 t.plan(4)168 169 const fastify = Fastify()170 171 fastify.get('/', function (request, reply) {172 const stream = fs.createReadStream(__filename)173 reply.send(new Response(Readable.toWeb(stream), {174 status: 500,175 headers: {176 hello: 'world'177 }178 }))179 })180 181 fastify.addHook('onSend', (request, reply, payload, done) => {182 t.assert.strictEqual(Object.prototype.toString.call(payload), '[object Response]')183 done(null, new Response(payload.body, {184 status: 200,185 headers: payload.headers186 }))187 })188 189 const {190 statusCode,191 headers,192 body193 } = await fastify.inject({ method: 'GET', path: '/' })194 195 const expected = await fs.promises.readFile(__filename)196 197 t.assert.strictEqual(statusCode, 200)198 t.assert.strictEqual(expected.toString(), body.toString())199 t.assert.strictEqual(headers.hello, 'world')200})201 202test('Error when Response.bodyUsed', async (t) => {203 t.plan(4)204 205 const expected = await fs.promises.readFile(__filename)206 207 const fastify = Fastify()208 209 fastify.get('/', async function (request, reply) {210 const stream = fs.createReadStream(__filename)211 const response = new Response(Readable.toWeb(stream), {212 status: 200,213 headers: {214 hello: 'world'215 }216 })217 const file = await response.text()218 t.assert.strictEqual(expected.toString(), file)219 t.assert.strictEqual(response.bodyUsed, true)220 return reply.send(response)221 })222 223 const response = await fastify.inject({ method: 'GET', path: '/' })224 225 t.assert.strictEqual(response.statusCode, 500)226 const body = response.json()227 t.assert.strictEqual(body.code, 'FST_ERR_REP_RESPONSE_BODY_CONSUMED')228})229 230test('Error when Response.body.locked', async (t) => {231 t.plan(3)232 233 const fastify = Fastify()234 235 fastify.get('/', async function (request, reply) {236 const stream = Readable.toWeb(fs.createReadStream(__filename))237 const response = new Response(stream, {238 status: 200,239 headers: {240 hello: 'world'241 }242 })243 stream.getReader()244 t.assert.strictEqual(stream.locked, true)245 return reply.send(response)246 })247 248 const response = await fastify.inject({ method: 'GET', path: '/' })249 250 t.assert.strictEqual(response.statusCode, 500)251 const body = response.json()252 t.assert.strictEqual(body.code, 'FST_ERR_REP_READABLE_STREAM_LOCKED')253})254 255test('Error when ReadableStream.locked', async (t) => {256 t.plan(3)257 258 const fastify = Fastify()259 260 fastify.get('/', async function (request, reply) {261 const stream = Readable.toWeb(fs.createReadStream(__filename))262 stream.getReader()263 t.assert.strictEqual(stream.locked, true)264 return reply.send(stream)265 })266 267 const response = await fastify.inject({ method: 'GET', path: '/' })268 269 t.assert.strictEqual(response.statusCode, 500)270 const body = response.json()271 t.assert.strictEqual(body.code, 'FST_ERR_REP_READABLE_STREAM_LOCKED')272})273 274test('allow to pipe with fetch', async (t) => {275 t.plan(2)276 const abortController = new AbortController()277 const { signal } = abortController278 279 const fastify = Fastify()280 t.after(() => {281 fastify.close()282 abortController.abort()283 })284 285 fastify.get('/', function (request, reply) {286 return fetch(`${fastify.listeningOrigin}/fetch`, {287 method: 'GET',288 signal289 })290 })291 292 fastify.get('/fetch', function async (request, reply) {293 reply.code(200).send({ ok: true })294 })295 296 await fastify.listen()297 298 const response = await fastify.inject({ method: 'GET', path: '/' })299 300 t.assert.strictEqual(response.statusCode, 200)301 t.assert.deepStrictEqual(response.json(), { ok: true })302})303 304test('allow to pipe with undici.fetch', async (t) => {305 t.plan(2)306 const abortController = new AbortController()307 const { signal } = abortController308 309 const fastify = Fastify()310 t.after(() => {311 fastify.close()312 abortController.abort()313 })314 315 fastify.get('/', function (request, reply) {316 return undiciFetch(`${fastify.listeningOrigin}/fetch`, {317 method: 'GET',318 signal319 })320 })321 322 fastify.get('/fetch', function (request, reply) {323 reply.code(200).send({ ok: true })324 })325 326 await fastify.listen()327 328 const response = await fastify.inject({ method: 'GET', path: '/' })329 330 t.assert.strictEqual(response.statusCode, 200)331 t.assert.deepStrictEqual(response.json(), { ok: true })332})333 