1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const fs = require('fs')
|
6 | const path = require('path')
|
7 | const Fastify = require('../..')
|
8 | const h2url = require('h2url')
|
9 | const sget = require('simple-get').concat
|
10 | const msg = { hello: 'world' }
|
11 |
|
12 | var fastify
|
13 | try {
|
14 | fastify = Fastify({
|
15 | http2: true,
|
16 | https: {
|
17 | allowHTTP1: true,
|
18 | key: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.key')),
|
19 | cert: fs.readFileSync(path.join(__dirname, '..', 'https', 'fastify.cert'))
|
20 | }
|
21 | })
|
22 | t.pass('Key/cert successfully loaded')
|
23 | } catch (e) {
|
24 | t.fail('Key/cert loading failed', e)
|
25 | }
|
26 |
|
27 | fastify.get('/', function (req, reply) {
|
28 | reply.code(200).send(msg)
|
29 | })
|
30 |
|
31 | fastify.post('/', function (req, reply) {
|
32 | reply.code(200).send(req.body)
|
33 | })
|
34 |
|
35 | fastify.get('/error', async function (req, reply) {
|
36 | throw new Error('kaboom')
|
37 | })
|
38 |
|
39 | fastify.listen(0, err => {
|
40 | t.error(err)
|
41 | fastify.server.unref()
|
42 |
|
43 | test('https get error', async (t) => {
|
44 | t.plan(1)
|
45 |
|
46 | const url = `https://localhost:${fastify.server.address().port}/error`
|
47 | const res = await h2url.concat({ url })
|
48 |
|
49 | t.strictEqual(res.headers[':status'], 500)
|
50 | })
|
51 |
|
52 | test('https post', async (t) => {
|
53 | t.plan(2)
|
54 |
|
55 | const url = `https://localhost:${fastify.server.address().port}`
|
56 | const res = await h2url.concat({
|
57 | url,
|
58 | method: 'POST',
|
59 | body: JSON.stringify({ hello: 'http2' }),
|
60 | headers: {
|
61 | 'content-type': 'application/json'
|
62 | }
|
63 | })
|
64 |
|
65 | t.strictEqual(res.headers[':status'], 200)
|
66 | t.deepEqual(JSON.parse(res.body), { hello: 'http2' })
|
67 | })
|
68 |
|
69 | test('https get request', async (t) => {
|
70 | t.plan(3)
|
71 |
|
72 | const url = `https://localhost:${fastify.server.address().port}`
|
73 | const res = await h2url.concat({ url })
|
74 |
|
75 | t.strictEqual(res.headers[':status'], 200)
|
76 | t.strictEqual(res.headers['content-length'], '' + JSON.stringify(msg).length)
|
77 | t.deepEqual(JSON.parse(res.body), msg)
|
78 | })
|
79 |
|
80 | test('http1 get request', t => {
|
81 | t.plan(4)
|
82 | sget({
|
83 | method: 'GET',
|
84 | url: 'https://localhost:' + fastify.server.address().port,
|
85 | rejectUnauthorized: false
|
86 | }, (err, response, body) => {
|
87 | t.error(err)
|
88 | t.strictEqual(response.statusCode, 200)
|
89 | t.strictEqual(response.headers['content-length'], '' + body.length)
|
90 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
91 | })
|
92 | })
|
93 |
|
94 | test('http1 get error', (t) => {
|
95 | t.plan(2)
|
96 | sget({
|
97 | method: 'GET',
|
98 | url: 'https://localhost:' + fastify.server.address().port + '/error',
|
99 | rejectUnauthorized: false
|
100 | }, (err, response, body) => {
|
101 | t.error(err)
|
102 | t.strictEqual(response.statusCode, 500)
|
103 | })
|
104 | })
|
105 | })
|