@fastify/reply-from
Version:
forward your HTTP request to another server, for fastify
59 lines (47 loc) • 1.19 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 Readable = require('node:stream').Readable
const instance = Fastify()
instance.register(From)
t.plan(5)
t.teardown(instance.close.bind(instance))
const target = http.createServer((_req, res) => {
t.fail('the target server should never be called')
res.end()
})
instance.post('/', (_request, reply) => {
const body = new Readable({
read: function () {
t.fail('the read function should never be called')
}
})
t.throws(() => {
reply.from(`http://localhost:${target.address().port}`, {
body
})
})
// return a 500
reply.code(500).send({ an: 'error' })
})
t.teardown(target.close.bind(target))
instance.listen({ port: 0 }, (err) => {
t.error(err)
target.listen({ port: 0 }, (err) => {
t.error(err)
get({
url: `http://localhost:${instance.server.address().port}`,
method: 'POST',
json: true,
body: {
hello: 'world'
}
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 500)
})
})
})