CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
server.test.js189 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('..')5const sget = require('simple-get').concat6const undici = require('undici')7 8test('listen should accept null port', async t => {9  const fastify = Fastify()10  t.after(() => fastify.close())11 12  await t.assert.doesNotReject(13    fastify.listen({ port: null })14  )15})16 17test('listen should accept undefined port', async t => {18  const fastify = Fastify()19  t.after(() => fastify.close())20 21  await t.assert.doesNotReject(22    fastify.listen({ port: undefined })23  )24})25 26test('listen should accept stringified number port', async t => {27  const fastify = Fastify()28  t.after(() => fastify.close())29 30  await t.assert.doesNotReject(31    fastify.listen({ port: '1234' })32  )33})34 35test('listen should accept log text resolution function', async t => {36  const fastify = Fastify()37  t.after(() => fastify.close())38 39  await t.assert.doesNotReject(40    fastify.listen({41      host: '127.0.0.1',42      port: '1234',43      listenTextResolver: (address) => {44        t.assert.strictEqual(address, 'http://127.0.0.1:1234')45        return 'hardcoded text'46      }47    })48  )49})50 51test('listen should reject string port', async (t) => {52  const fastify = Fastify()53  t.after(() => fastify.close())54 55  try {56    await fastify.listen({ port: 'hello-world' })57  } catch (error) {58    t.assert.strictEqual(error.code, 'ERR_SOCKET_BAD_PORT')59  }60 61  try {62    await fastify.listen({ port: '1234hello' })63  } catch (error) {64    t.assert.strictEqual(error.code, 'ERR_SOCKET_BAD_PORT')65  }66})67 68test('Test for hostname and port', (t, end) => {69  const app = Fastify()70  t.after(() => app.close())71  app.get('/host', (req, res) => {72    const host = 'localhost:8000'73    t.assert.strictEqual(req.host, host)74    t.assert.strictEqual(req.hostname, req.host.split(':')[0])75    t.assert.strictEqual(req.port, Number(req.host.split(':')[1]))76    res.send('ok')77  })78 79  app.listen({ port: 8000 }, () => {80    sget('http://localhost:8000/host', () => { end() })81  })82})83 84test('abort signal', async t => {85  await t.test('listen should not start server', (t, end) => {86    t.plan(2)87    function onClose (instance, done) {88      t.assert.strictEqual(instance, fastify)89      done()90      end()91    }92    const controller = new AbortController()93 94    const fastify = Fastify()95    fastify.addHook('onClose', onClose)96    fastify.listen({ port: 1234, signal: controller.signal }, (err) => {97      t.assert.ifError(err)98    })99    controller.abort()100    t.assert.strictEqual(fastify.server.listening, false)101  })102 103  await t.test('listen should not start server if already aborted', (t, end) => {104    t.plan(2)105    function onClose (instance, done) {106      t.assert.strictEqual(instance, fastify)107      done()108      end()109    }110 111    const controller = new AbortController()112    controller.abort()113    const fastify = Fastify()114    fastify.addHook('onClose', onClose)115    fastify.listen({ port: 1234, signal: controller.signal }, (err) => {116      t.assert.ifError(err)117    })118    t.assert.strictEqual(fastify.server.listening, false)119  })120 121  await t.test('listen should throw if received invalid signal', t => {122    t.plan(2)123    const fastify = Fastify()124 125    try {126      fastify.listen({ port: 1234, signal: {} }, (err) => {127        t.assert.ifError(err)128      })129      t.assert.fail('should throw')130    } catch (e) {131      t.assert.strictEqual(e.code, 'FST_ERR_LISTEN_OPTIONS_INVALID')132      t.assert.strictEqual(e.message, 'Invalid listen options: \'Invalid options.signal\'')133    }134  })135})136 137test('#5180 - preClose should be called before closing secondary server', async (t) => {138  t.plan(2)139  const fastify = Fastify({ forceCloseConnections: true })140  let flag = false141  t.after(() => fastify.close())142 143  fastify.addHook('preClose', () => {144    flag = true145  })146 147  fastify.get('/', async (req, reply) => {148    // request will be pending for 1 second to simulate a slow request149    await new Promise((resolve) => { setTimeout(resolve, 1000) })150    return { hello: 'world' }151  })152 153  fastify.listen({ port: 0 }, (err) => {154    t.assert.ifError(err)155    const addresses = fastify.addresses()156    const mainServerAddress = fastify.server.address()157    let secondaryAddress158    for (const addr of addresses) {159      if (addr.family !== mainServerAddress.family) {160        secondaryAddress = addr161        secondaryAddress.address = secondaryAddress.family === 'IPv6'162          ? `[${secondaryAddress.address}]`163          : secondaryAddress.address164        break165      }166    }167 168    if (!secondaryAddress) {169      t.assert.ok(true, 'Secondary address not found')170      return171    }172 173    undici.request(`http://${secondaryAddress.address}:${secondaryAddress.port}/`)174      .then(175        () => { t.assert.fail('Request should not succeed') },176        () => {177          t.assert.ok(flag)178        }179      )180 181    // Close the server while the slow request is pending182    setTimeout(fastify.close, 250)183  })184 185  // Wait 1000ms to ensure that the test is finished and async operations are186  // completed187  await new Promise((resolve) => { setTimeout(resolve, 1000) })188})189