forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps.test.js
More file actions
135 lines (116 loc) · 3.53 KB
/
Copy pathhttps.test.js
File metadata and controls
135 lines (116 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict'
const { test } = require('node:test')
const { request, Agent, fetch } = require('undici')
const Fastify = require('../..')
const { buildCertificate } = require('../build-certificate')
test.before(buildCertificate)
test('https', async (t) => {
t.plan(3)
let fastify
try {
fastify = Fastify({
https: {
key: global.context.key,
cert: global.context.cert
}
})
t.assert.ok('Key/cert successfully loaded')
} catch (e) {
t.assert.fail('Key/cert loading failed')
}
fastify.get('/', function (req, reply) {
reply.code(200).send({ hello: 'world' })
})
fastify.get('/proto', function (req, reply) {
reply.code(200).send({ proto: req.protocol })
})
await fastify.listen({ port: 0 })
t.after(() => { fastify.close() })
await t.test('https get request', async t => {
t.plan(4)
const result = await fetch('https://localhost:' + fastify.server.address().port, {
dispatcher: new Agent({
connect: {
rejectUnauthorized: false
}
})
})
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
})
await t.test('https get request without trust proxy - protocol', async t => {
t.plan(3)
const result1 = await fetch(`${'https://localhost:' + fastify.server.address().port}/proto`, {
dispatcher: new Agent({
connect: {
rejectUnauthorized: false
}
})
})
t.assert.ok(result1.ok)
t.assert.deepStrictEqual(await result1.json(), { proto: 'https' })
const result2 = await fetch(`${'https://localhost:' + fastify.server.address().port}/proto`, {
dispatcher: new Agent({
connect: {
rejectUnauthorized: false
}
}),
headers: {
'x-forwarded-proto': 'lorem'
}
})
t.assert.deepStrictEqual(await result2.json(), { proto: 'https' })
})
})
test('https - headers', async (t) => {
t.plan(3)
let fastify
try {
fastify = Fastify({
https: {
key: global.context.key,
cert: global.context.cert
}
})
t.assert.ok('Key/cert successfully loaded')
} catch (e) {
t.assert.fail('Key/cert loading failed')
}
fastify.get('/', function (req, reply) {
reply.code(200).send({ hello: 'world', hostname: req.hostname, port: req.port })
})
t.after(async () => { await fastify.close() })
await fastify.listen({ port: 0 })
await t.test('https get request', async t => {
t.plan(3)
const result = await fetch('https://localhost:' + fastify.server.address().port, {
dispatcher: new Agent({
connect: {
rejectUnauthorized: false
}
})
})
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
t.assert.deepStrictEqual(await result.json(), { hostname: 'localhost', port: fastify.server.address().port, hello: 'world' })
})
await t.test('https get request - test port fall back', async t => {
t.plan(2)
const result = await request('https://localhost:' + fastify.server.address().port, {
method: 'GET',
headers: {
host: 'fastify.test'
},
dispatcher: new Agent({
connect: {
rejectUnauthorized: false
}
})
})
t.assert.strictEqual(result.statusCode, 200)
t.assert.deepStrictEqual(await result.body.json(), { hello: 'world', hostname: 'fastify.test', port: null })
})
})