@fastify/reply-from
Version:
forward your HTTP request to another server, for fastify
57 lines (47 loc) • 1.33 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()
instance.register(From)
t.plan(9)
t.teardown(instance.close.bind(instance))
const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'POST')
t.equal(req.headers['content-type'], 'text/plain')
let data = ''
req.setEncoding('utf8')
req.on('data', (d) => {
data += d
})
req.on('end', () => {
const str = data.toString()
t.same(str, 'this is plain text')
res.statusCode = 200
res.setHeader('content-type', 'text/plain')
res.end(str)
})
})
instance.post('/', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}`)
})
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',
headers: { 'content-type': 'text/plain' },
body: 'this is plain text'
}, (err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'text/plain')
t.same(data.toString(), 'this is plain text')
})
})
})