@fastify/reply-from
Version:
forward your HTTP request to another server, for fastify
60 lines (50 loc) • 1.59 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, {
contentTypesToEncode: ['application/x-www-form-urlencoded']
})
instance.register(require('@fastify/formbody'))
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'], 'application/x-www-form-urlencoded')
let data = ''
req.setEncoding('utf8')
req.on('data', (d) => {
data += d
})
req.on('end', () => {
const str = data.toString()
t.same(JSON.parse(data), { some: 'info', another: 'detail' })
res.statusCode = 200
res.setHeader('content-type', 'application/x-www-form-urlencoded')
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': 'application/x-www-form-urlencoded' },
body: 'some=info&another=detail'
}, (err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'application/x-www-form-urlencoded')
t.same(JSON.parse(data), { some: 'info', another: 'detail' })
})
})
})