MCPcopy
hub / github.com/fastify/fastify / setup

Function setup

test/custom-http-server.test.js:9–154  ·  test/custom-http-server.test.js::setup
()

Source from the content-addressed store, hash-verified

7const { FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE } = require(class="st">'../lib/errors')
8
9async function setup () {
10 const localAddresses = await dns.lookup(class="st">'localhost', { all: true })
11
12 test(class="st">'Should support a custom http server', { skip: localAddresses.length < 1 }, async t => {
13 t.plan(5)
14
15 const fastify = Fastify({
16 serverFactory: (handler, opts) => {
17 t.assert.ok(opts.serverFactory, class="st">'it is called once for localhost')
18
19 const server = http.createServer((req, res) => {
20 req.custom = true
21 handler(req, res)
22 })
23
24 return server
25 }
26 })
27
28 t.after(() => fastify.close())
29 fastify.get(class="st">'/', (req, reply) => {
30 t.assert.ok(req.raw.custom)
31 reply.send({ hello: class="st">'world' })
32 })
33
34 await fastify.listen({ port: 0 })
35
36 const response = await fetch(class="st">'http:class="cm">//localhost:' + fastify.server.address().port, {
37 method: class="st">'GET'
38 })
39 t.assert.ok(response.ok)
40 t.assert.strictEqual(response.status, 200)
41 const body = await response.text()
42 t.assert.deepStrictEqual(JSON.parse(body), { hello: class="st">'world' })
43 })
44
45 test(class="st">'Should not allow forceCloseConnection=idle if the server does not support closeIdleConnections', t => {
46 t.plan(1)
47
48 t.assert.throws(
49 () => {
50 Fastify({
51 forceCloseConnections: class="st">'idle',
52 serverFactory (handler, opts) {
53 return {
54 on () {
55
56 }
57 }
58 }
59 })
60 },
61 FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE,
62 class="st">"Cannot set forceCloseConnections to 'idle' as your HTTP server does not support closeIdleConnections method"
63 )
64 })
65
66 test(class="st">'Should not make an extra closeIdleConnections call for native servers', async t => {

Callers 1

Calls 7

afterMethod · 0.80
closeMethod · 0.80
sendMethod · 0.80
listenMethod · 0.80
readyMethod · 0.80
addressesMethod · 0.80
handlerFunction · 0.70

Tested by

no test coverage detected