UNPKG

@fastify/reply-from

Version:

forward your HTTP request to another server, for fastify

51 lines (40 loc) 1.44 kB
'use strict' const t = require('node:test') const Fastify = require('fastify') const { request, Agent } = require('undici') const From = require('..') const http = require('node:http') const instance = Fastify() t.test('core with path in base', async (t) => { t.plan(8) t.after(() => instance.close()) const target = http.createServer((req, res) => { t.assert.ok('request proxied') t.assert.strictEqual(req.method, 'GET') t.assert.strictEqual(req.url, '/hello') t.assert.strictEqual(req.headers.connection, 'close') res.statusCode = 205 res.setHeader('Content-Type', 'text/plain') res.setHeader('x-my-header', 'hello!') res.end('hello world') }) instance.get('/', (_request, reply) => { reply.from('/hello') }) t.after(() => target.close()) await new Promise(resolve => target.listen({ port: 0 }, resolve)) instance.register(From, { base: `http://localhost:${target.address().port}/hello`, http: true }) await new Promise(resolve => instance.listen({ port: 0 }, resolve)) const result = await request(`http://localhost:${instance.server.address().port}`, { dispatcher: new Agent({ pipelining: 0 }) }) t.assert.strictEqual(result.headers['content-type'], 'text/plain') t.assert.strictEqual(result.headers['x-my-header'], 'hello!') t.assert.strictEqual(result.statusCode, 205) t.assert.strictEqual(await result.body.text(), 'hello world') })