CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
stream.2.test.js120 linesDownload Raw Back to test
1'use strict'2 3const t = require('tap')4const test = t.test5const proxyquire = require('proxyquire')6const fs = require('node:fs')7const resolve = require('node:path').resolve8const zlib = require('node:zlib')9const pipeline = require('node:stream').pipeline10const Fastify = require('..')11 12test('onSend hook stream', t => {13  t.plan(4)14  const fastify = Fastify()15 16  fastify.get('/', function (req, reply) {17    reply.send({ hello: 'world' })18  })19 20  fastify.addHook('onSend', (req, reply, payload, done) => {21    const gzStream = zlib.createGzip()22 23    reply.header('Content-Encoding', 'gzip')24    pipeline(25      fs.createReadStream(resolve(__filename), 'utf8'),26      gzStream,27      t.error28    )29    done(null, gzStream)30  })31 32  fastify.inject({33    url: '/',34    method: 'GET'35  }, (err, res) => {36    t.error(err)37    t.equal(res.headers['content-encoding'], 'gzip')38    const file = fs.readFileSync(resolve(__filename), 'utf8')39    const payload = zlib.gunzipSync(res.rawPayload)40    t.equal(payload.toString('utf-8'), file)41    fastify.close()42  })43})44 45test('onSend hook stream should work even if payload is not a proper stream', t => {46  t.plan(1)47 48  const reply = proxyquire('../lib/reply', {49    'node:stream': {50      finished: (...args) => {51        if (args.length === 2) { args[1](new Error('test-error')) }52      }53    }54  })55  const Fastify = proxyquire('..', {56    './lib/reply.js': reply57  })58  const spyLogger = {59    fatal: () => { },60    error: () => { },61    warn: (message) => {62      t.equal(message, 'stream payload does not end properly')63      fastify.close()64    },65    info: () => { },66    debug: () => { },67    trace: () => { },68    child: () => { return spyLogger }69  }70 71  const fastify = Fastify({ loggerInstance: spyLogger })72  fastify.get('/', function (req, reply) {73    reply.send({ hello: 'world' })74  })75  fastify.addHook('onSend', (req, reply, payload, done) => {76    const fakeStream = { pipe: () => { } }77    done(null, fakeStream)78  })79 80  fastify.inject({81    url: '/',82    method: 'GET'83  })84})85 86test('onSend hook stream should work on payload with "close" ending function', t => {87  t.plan(1)88 89  const reply = proxyquire('../lib/reply', {90    'node:stream': {91      finished: (...args) => {92        if (args.length === 2) { args[1](new Error('test-error')) }93      }94    }95  })96  const Fastify = proxyquire('..', {97    './lib/reply.js': reply98  })99 100  const fastify = Fastify({ logger: false })101  fastify.get('/', function (req, reply) {102    reply.send({ hello: 'world' })103  })104  fastify.addHook('onSend', (req, reply, payload, done) => {105    const fakeStream = {106      pipe: () => { },107      close: (cb) => {108        cb()109        t.pass()110      }111    }112    done(null, fakeStream)113  })114 115  fastify.inject({116    url: '/',117    method: 'GET'118  })119})120