UNPKG

1.15 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const test = t.test
5const Fastify = require('../..')
6const h2url = require('h2url')
7const msg = { hello: 'world' }
8
9var fastify
10try {
11 fastify = Fastify({
12 http2: true
13 })
14 t.pass('http2 successfully loaded')
15} catch (e) {
16 t.fail('http2 loading failed', e)
17}
18
19fastify.get('/', function (req, reply) {
20 reply.code(200).send(msg)
21})
22
23fastify.get('/hostname', function (req, reply) {
24 reply.code(200).send(req.hostname)
25})
26
27fastify.listen(0, err => {
28 t.error(err)
29 fastify.server.unref()
30
31 test('http get request', async (t) => {
32 t.plan(3)
33
34 const url = `http://localhost:${fastify.server.address().port}`
35 const res = await h2url.concat({ url })
36
37 t.strictEqual(res.headers[':status'], 200)
38 t.strictEqual(res.headers['content-length'], '' + JSON.stringify(msg).length)
39
40 t.deepEqual(JSON.parse(res.body), msg)
41 })
42
43 test('http hostname', async (t) => {
44 t.plan(1)
45
46 const hostname = `localhost:${fastify.server.address().port}`
47
48 const url = `http://${hostname}/hostname`
49 const res = await h2url.concat({ url })
50
51 t.strictEqual(res.body, hostname)
52 })
53})