CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
max-requests-per-socket.test.js114 linesDownload Raw Back to test
1'use strict'2 3const net = require('node:net')4const { test } = require('node:test')5const Fastify = require('..')6 7test('maxRequestsPerSocket', (t, done) => {8  t.plan(8)9 10  const fastify = Fastify({ maxRequestsPerSocket: 2 })11  fastify.get('/', (req, reply) => {12    reply.send({ hello: 'world' })13  })14 15  fastify.listen({ port: 0 }, function (err) {16    t.assert.ifError(err)17 18    const port = fastify.server.address().port19    const client = net.createConnection({ port }, () => {20      client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')21 22      client.once('data', data => {23        t.assert.match(data.toString(), /Connection:\s*keep-alive/i)24        t.assert.match(data.toString(), /Keep-Alive:\s*timeout=\d+/i)25        t.assert.match(data.toString(), /200 OK/i)26 27        client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')28 29        client.once('data', data => {30          t.assert.match(data.toString(), /Connection:\s*close/i)31          t.assert.match(data.toString(), /200 OK/i)32 33          client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')34 35          client.once('data', data => {36            t.assert.match(data.toString(), /Connection:\s*close/i)37            t.assert.match(data.toString(), /503 Service Unavailable/i)38            client.end()39            fastify.close()40            done()41          })42        })43      })44    })45  })46})47 48test('maxRequestsPerSocket zero should behave same as null', (t, done) => {49  t.plan(10)50 51  const fastify = Fastify({ maxRequestsPerSocket: 0 })52  fastify.get('/', (req, reply) => {53    reply.send({ hello: 'world' })54  })55 56  fastify.listen({ port: 0 }, function (err) {57    t.assert.ifError(err)58 59    const port = fastify.server.address().port60    const client = net.createConnection({ port }, () => {61      client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')62 63      client.once('data', data => {64        t.assert.match(data.toString(), /Connection:\s*keep-alive/i)65        t.assert.match(data.toString(), /Keep-Alive:\s*timeout=\d+/i)66        t.assert.match(data.toString(), /200 OK/i)67 68        client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')69 70        client.once('data', data => {71          t.assert.match(data.toString(), /Connection:\s*keep-alive/i)72          t.assert.match(data.toString(), /Keep-Alive:\s*timeout=\d+/i)73          t.assert.match(data.toString(), /200 OK/i)74 75          client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')76 77          client.once('data', data => {78            t.assert.match(data.toString(), /Connection:\s*keep-alive/i)79            t.assert.match(data.toString(), /Keep-Alive:\s*timeout=\d+/i)80            t.assert.match(data.toString(), /200 OK/i)81            client.end()82            fastify.close()83            done()84          })85        })86      })87    })88  })89})90 91test('maxRequestsPerSocket should be set', async (t) => {92  t.plan(1)93 94  const initialConfig = Fastify({ maxRequestsPerSocket: 5 }).initialConfig95  t.assert.deepStrictEqual(initialConfig.maxRequestsPerSocket, 5)96})97 98test('maxRequestsPerSocket should 0', async (t) => {99  t.plan(1)100 101  const initialConfig = Fastify().initialConfig102  t.assert.deepStrictEqual(initialConfig.maxRequestsPerSocket, 0)103})104 105test('requestTimeout passed to server', t => {106  t.plan(2)107 108  const httpServer = Fastify({ maxRequestsPerSocket: 5 }).server109  t.assert.strictEqual(httpServer.maxRequestsPerSocket, 5)110 111  const httpsServer = Fastify({ maxRequestsPerSocket: 5, https: true }).server112  t.assert.strictEqual(httpsServer.maxRequestsPerSocket, 5)113})114