@fastify/reply-from
Version:
forward your HTTP request to another server, for fastify
53 lines (40 loc) • 1.15 kB
JavaScript
const { test } = require('tap')
const Fastify = require('fastify')
const From = require('..')
const got = require('got')
test('http -> http2', async (t) => {
const instance = Fastify()
t.teardown(instance.close.bind(instance))
const target = Fastify({
http2: true
})
target.get('/', (_request, reply) => {
t.pass('request proxied')
reply.code(404).header('x-my-header', 'hello!').send({
hello: 'world'
})
})
instance.get('/', (_request, reply) => {
reply.from()
})
t.teardown(target.close.bind(target))
await target.listen({ port: 0 })
instance.register(From, {
base: `http://localhost:${target.server.address().port}`,
http2: true
})
await instance.listen({ port: 0 })
try {
await got(`http://localhost:${instance.server.address().port}`)
} catch (err) {
t.equal(err.response.statusCode, 404)
t.equal(err.response.headers['x-my-header'], 'hello!')
t.match(err.response.headers['content-type'], /application\/json/)
t.same(JSON.parse(err.response.body), { hello: 'world' })
instance.close()
target.close()
return
}
t.fail()
})