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

Function setup

test/https/custom-https-server.test.js:10–56  ·  test/https/custom-https-server.test.js::setup
()

Source from the content-addressed store, hash-verified

8const { Agent } = require(class="st">'undici')
9
10async function setup () {
11 await buildCertificate()
12
13 const localAddresses = await dns.lookup(class="st">'localhost', { all: true })
14
15 test(class="st">'Should support a custom https server', { skip: localAddresses.length < 1 }, async t => {
16 t.plan(5)
17
18 const fastify = Fastify({
19 serverFactory: (handler, opts) => {
20 t.assert.ok(opts.serverFactory, class="st">'it is called once for localhost')
21
22 const options = {
23 key: global.context.key,
24 cert: global.context.cert
25 }
26
27 const server = https.createServer(options, (req, res) => {
28 req.custom = true
29 handler(req, res)
30 })
31
32 return server
33 }
34 })
35
36 t.after(() => { fastify.close() })
37
38 fastify.get(class="st">'/', (req, reply) => {
39 t.assert.ok(req.raw.custom)
40 reply.send({ hello: class="st">'world' })
41 })
42
43 await fastify.listen({ port: 0 })
44
45 const result = await fetch(class="st">'https:class="cm">//localhost:' + fastify.server.address().port, {
46 dispatcher: new Agent({
47 connect: {
48 rejectUnauthorized: false
49 }
50 })
51 })
52 t.assert.ok(result.ok)
53 t.assert.strictEqual(result.status, 200)
54 t.assert.deepStrictEqual(await result.json(), { hello: class="st">'world' })
55 })
56}
57
58setup()

Callers 1

Calls 6

buildCertificateFunction · 0.85
afterMethod · 0.80
closeMethod · 0.80
sendMethod · 0.80
listenMethod · 0.80
handlerFunction · 0.50

Tested by

no test coverage detected