CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
stream.1.test.js109 linesDownload Raw Back to test
1'use strict'2 3const t = require('tap')4const test = t.test5const sget = require('simple-get').concat6const fs = require('node:fs')7const Fastify = require('../fastify')8 9test('should respond with a stream', t => {10  t.plan(6)11  const fastify = Fastify()12 13  fastify.get('/', function (req, reply) {14    const stream = fs.createReadStream(__filename, 'utf8')15    reply.code(200).send(stream)16  })17 18  fastify.listen({ port: 0 }, err => {19    t.error(err)20    t.teardown(() => { fastify.close() })21 22    sget(`http://localhost:${fastify.server.address().port}`, function (err, response, data) {23      t.error(err)24      t.equal(response.headers['content-type'], undefined)25      t.equal(response.statusCode, 200)26 27      fs.readFile(__filename, (err, expected) => {28        t.error(err)29        t.equal(expected.toString(), data.toString())30      })31    })32  })33})34 35test('should respond with a stream (error)', t => {36  t.plan(3)37  const fastify = Fastify()38 39  fastify.get('/error', function (req, reply) {40    const stream = fs.createReadStream('not-existing-file', 'utf8')41    reply.code(200).send(stream)42  })43 44  fastify.listen({ port: 0 }, err => {45    t.error(err)46    t.teardown(() => { fastify.close() })47 48    sget(`http://localhost:${fastify.server.address().port}/error`, function (err, response) {49      t.error(err)50      t.equal(response.statusCode, 500)51    })52  })53})54 55test('should trigger the onSend hook', t => {56  t.plan(4)57  const fastify = Fastify()58 59  fastify.get('/', (req, reply) => {60    reply.send(fs.createReadStream(__filename, 'utf8'))61  })62 63  fastify.addHook('onSend', (req, reply, payload, done) => {64    t.ok(payload._readableState)65    reply.header('Content-Type', 'application/javascript')66    done()67  })68 69  fastify.inject({70    url: '/'71  }, (err, res) => {72    t.error(err)73    t.equal(res.headers['content-type'], 'application/javascript')74    t.equal(res.payload, fs.readFileSync(__filename, 'utf8'))75    fastify.close()76  })77})78 79test('should trigger the onSend hook only twice if pumping the stream fails, first with the stream, second with the serialized error', t => {80  t.plan(5)81  const fastify = Fastify()82 83  fastify.get('/', (req, reply) => {84    reply.send(fs.createReadStream('not-existing-file', 'utf8'))85  })86 87  let counter = 088  fastify.addHook('onSend', (req, reply, payload, done) => {89    if (counter === 0) {90      t.ok(payload._readableState)91    } else if (counter === 1) {92      const error = JSON.parse(payload)93      t.equal(error.statusCode, 500)94    }95    counter++96    done()97  })98 99  fastify.listen({ port: 0 }, err => {100    t.error(err)101    t.teardown(() => { fastify.close() })102 103    sget(`http://localhost:${fastify.server.address().port}`, function (err, response) {104      t.error(err)105      t.equal(response.statusCode, 500)106    })107  })108})109