@fastify/reply-from
Version:
forward your HTTP request to another server, for fastify
52 lines (41 loc) • 1.17 kB
JavaScript
const t = require('tap')
const Fastify = require('fastify')
const From = require('..')
const http = require('node:http')
const get = require('simple-get').concat
const instance = Fastify()
t.plan(7)
t.teardown(instance.close.bind(instance))
const target = http.createServer((_req, res) => {
t.fail('this should never get called')
res.end('hello world')
})
instance.head('/', (_request, reply) => {
try {
reply.from(null, { body: 'this is the new body' })
} catch (e) {
t.equal(e.message, 'Rewriting the body when doing a HEAD is not allowed')
reply.header('x-http-error', '1')
reply.send('hello world')
}
})
t.teardown(target.close.bind(target))
target.listen({ port: 0 }, (err) => {
t.error(err)
instance.register(From, {
base: `http://localhost:${target.address().port}`
})
instance.listen({ port: 0 }, (err) => {
t.error(err)
get({
url: `http://localhost:${instance.server.address().port}`,
method: 'HEAD'
}, (err, res, data) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['x-http-error'], '1')
t.equal(data.toString(), '')
})
})
})