Spaces:
Runtime error
Runtime error
File size: 2,856 Bytes
23ac194 | 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 | 'use strict'
const { test } = require('node:test')
const Fastify = require('../..')
const h2url = require('h2url')
const alpha = { res: 'alpha' }
const beta = { res: 'beta' }
const { buildCertificate } = require('../build-certificate')
test.before(buildCertificate)
test('A route supports host constraints under http2 protocol and secure connection', async (t) => {
t.plan(5)
let fastify
try {
fastify = Fastify({
http2: true,
https: {
key: global.context.key,
cert: global.context.cert
}
})
t.assert.ok(true, 'Key/cert successfully loaded')
} catch (e) {
t.assert.fail('Key/cert loading failed')
}
const constrain = 'fastify.dev'
fastify.route({
method: 'GET',
url: '/',
handler: function (_, reply) {
reply.code(200).send(alpha)
}
})
fastify.route({
method: 'GET',
url: '/beta',
constraints: { host: constrain },
handler: function (_, reply) {
reply.code(200).send(beta)
}
})
fastify.route({
method: 'GET',
url: '/hostname_port',
constraints: { host: constrain },
handler: function (req, reply) {
reply.code(200).send({ ...beta, hostname: req.hostname })
}
})
t.after(() => { fastify.close() })
await fastify.listen({ port: 0 })
await t.test('https get request - no constrain', async (t) => {
t.plan(3)
const url = `https://localhost:${fastify.server.address().port}`
const res = await h2url.concat({ url })
t.assert.strictEqual(res.headers[':status'], 200)
t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(alpha).length)
t.assert.deepStrictEqual(JSON.parse(res.body), alpha)
})
await t.test('https get request - constrain', async (t) => {
t.plan(3)
const url = `https://localhost:${fastify.server.address().port}/beta`
const res = await h2url.concat({
url,
headers: {
':authority': constrain
}
})
t.assert.strictEqual(res.headers[':status'], 200)
t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(beta).length)
t.assert.deepStrictEqual(JSON.parse(res.body), beta)
})
await t.test('https get request - constrain - not found', async (t) => {
t.plan(1)
const url = `https://localhost:${fastify.server.address().port}/beta`
const res = await h2url.concat({
url
})
t.assert.strictEqual(res.headers[':status'], 404)
})
await t.test('https get request - constrain - verify hostname and port from request', async (t) => {
t.plan(1)
const url = `https://localhost:${fastify.server.address().port}/hostname_port`
const res = await h2url.concat({
url,
headers: {
':authority': constrain
}
})
const body = JSON.parse(res.body)
t.assert.strictEqual(body.hostname, constrain)
})
})
|