CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
stream.3.test.js193 linesDownload Raw Back to test
1'use strict'2 3const t = require('tap')4const test = t.test5const split = require('split2')6const Fastify = require('..')7 8test('Destroying streams prematurely', t => {9  t.plan(6)10 11  let fastify = null12  const logStream = split(JSON.parse)13  try {14    fastify = Fastify({15      logger: {16        stream: logStream,17        level: 'info'18      }19    })20  } catch (e) {21    t.fail()22  }23  const stream = require('node:stream')24  const http = require('node:http')25 26  // Test that "premature close" errors are logged with level warn27  logStream.on('data', line => {28    if (line.res) {29      t.equal(line.msg, 'stream closed prematurely')30      t.equal(line.level, 30)31    }32  })33 34  fastify.get('/', function (request, reply) {35    t.pass('Received request')36 37    let sent = false38    const reallyLongStream = new stream.Readable({39      read: function () {40        if (!sent) {41          this.push(Buffer.from('hello\n'))42        }43        sent = true44      }45    })46 47    reply.send(reallyLongStream)48  })49 50  fastify.listen({ port: 0 }, err => {51    t.error(err)52    t.teardown(() => { fastify.close() })53 54    const port = fastify.server.address().port55 56    http.get(`http://localhost:${port}`, function (response) {57      t.equal(response.statusCode, 200)58      response.on('readable', function () {59        response.destroy()60      })61 62      // Node bug? Node never emits 'close' here.63      response.on('aborted', function () {64        t.pass('Response closed')65      })66    })67  })68})69 70test('Destroying streams prematurely should call close method', t => {71  t.plan(7)72 73  let fastify = null74  const logStream = split(JSON.parse)75  try {76    fastify = Fastify({77      logger: {78        stream: logStream,79        level: 'info'80      }81    })82  } catch (e) {83    t.fail()84  }85  const stream = require('node:stream')86  const http = require('node:http')87 88  // Test that "premature close" errors are logged with level warn89  logStream.on('data', line => {90    if (line.res) {91      t.equal(line.msg, 'stream closed prematurely')92      t.equal(line.level, 30)93    }94  })95 96  fastify.get('/', function (request, reply) {97    t.pass('Received request')98 99    let sent = false100    const reallyLongStream = new stream.Readable({101      read: function () {102        if (!sent) {103          this.push(Buffer.from('hello\n'))104        }105        sent = true106      }107    })108    reallyLongStream.destroy = undefined109    reallyLongStream.close = () => t.ok('called')110    reply.send(reallyLongStream)111  })112 113  fastify.listen({ port: 0 }, err => {114    t.error(err)115    t.teardown(() => { fastify.close() })116 117    const port = fastify.server.address().port118 119    http.get(`http://localhost:${port}`, function (response) {120      t.equal(response.statusCode, 200)121      response.on('readable', function () {122        response.destroy()123      })124      // Node bug? Node never emits 'close' here.125      response.on('aborted', function () {126        t.pass('Response closed')127      })128    })129  })130})131 132test('Destroying streams prematurely should call close method when destroy is not a function', t => {133  t.plan(7)134 135  let fastify = null136  const logStream = split(JSON.parse)137  try {138    fastify = Fastify({139      logger: {140        stream: logStream,141        level: 'info'142      }143    })144  } catch (e) {145    t.fail()146  }147  const stream = require('node:stream')148  const http = require('node:http')149 150  // Test that "premature close" errors are logged with level warn151  logStream.on('data', line => {152    if (line.res) {153      t.equal(line.msg, 'stream closed prematurely')154      t.equal(line.level, 30)155    }156  })157 158  fastify.get('/', function (request, reply) {159    t.pass('Received request')160 161    let sent = false162    const reallyLongStream = new stream.Readable({163      read: function () {164        if (!sent) {165          this.push(Buffer.from('hello\n'))166        }167        sent = true168      }169    })170    reallyLongStream.destroy = true171    reallyLongStream.close = () => t.ok('called')172    reply.send(reallyLongStream)173  })174 175  fastify.listen({ port: 0 }, err => {176    t.error(err)177    t.teardown(() => { fastify.close() })178 179    const port = fastify.server.address().port180 181    http.get(`http://localhost:${port}`, function (response) {182      t.equal(response.statusCode, 200)183      response.on('readable', function () {184        response.destroy()185      })186      // Node bug? Node never emits 'close' here.187      response.on('aborted', function () {188        t.pass('Response closed')189      })190    })191  })192})193