UNPKG

1.2 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const test = t.test
5const Fastify = require('../..')
6const https = require('https')
7const fs = require('fs')
8const path = require('path')
9const sget = require('simple-get').concat
10
11test('Should support a custom https server', t => {
12 t.plan(6)
13
14 const serverFactory = (handler, opts) => {
15 t.ok(opts.serverFactory)
16
17 const options = {
18 key: fs.readFileSync(path.join(__dirname, 'fastify.key')),
19 cert: fs.readFileSync(path.join(__dirname, 'fastify.cert'))
20 }
21
22 const server = https.createServer(options, (req, res) => {
23 req.custom = true
24 handler(req, res)
25 })
26
27 return server
28 }
29
30 const fastify = Fastify({ serverFactory })
31
32 t.teardown(fastify.close.bind(fastify))
33
34 fastify.get('/', (req, reply) => {
35 t.ok(req.raw.custom)
36 reply.send({ hello: 'world' })
37 })
38
39 fastify.listen(0, err => {
40 t.error(err)
41
42 sget({
43 method: 'GET',
44 url: 'https://localhost:' + fastify.server.address().port,
45 rejectUnauthorized: false
46 }, (err, response, body) => {
47 t.error(err)
48 t.strictEqual(response.statusCode, 200)
49 t.deepEqual(JSON.parse(body), { hello: 'world' })
50 })
51 })
52})