Skip to content

Commit ebbe103

Browse files
lynxtaamcollina
authored andcommitted
feat: support reply options (#21)
* feat: support reply options * added unit test * refactor test
1 parent c2a9973 commit ebbe103

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ The prefix to mount this plugin on. All the requests to the current server start
8383

8484
A `beforeHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
8585

86+
### replyOptions
87+
88+
Object with [reply options](https://github.com/fastify/fastify-reply-from#replyfromsource-opts) for `fastify-reply-from`.
89+
8690
## Benchmarks
8791

8892
The following benchmarks where generated on a Macbook 2018 with i5 and

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ module.exports = async function (fastify, opts) {
2727

2828
function reply (request, reply) {
2929
var dest = request.req.url.replace(this.basePath, '')
30-
reply.from(dest || '/')
30+
reply.from(dest || '/', opts.replyOptions)
3131
}
3232
}

test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ async function run () {
158158
const secondProxyPrefix = await got(`http://localhost:${proxyServer.server.address().port}/api2`)
159159
t.equal(secondProxyPrefix.body, 'this is root for origin2')
160160
})
161+
162+
test('passes replyOptions object to reply.from() calls', async (t) => {
163+
const proxyServer = Fastify()
164+
165+
proxyServer.register(proxy, {
166+
upstream: `http://localhost:${origin.server.address().port}`,
167+
prefix: '/api',
168+
replyOptions: {
169+
rewriteHeaders: headers => Object.assign({ 'x-test': 'test' }, headers)
170+
}
171+
})
172+
173+
await proxyServer.listen(0)
174+
175+
t.tearDown(() => {
176+
proxyServer.close()
177+
})
178+
179+
const { headers } = await got(`http://localhost:${proxyServer.server.address().port}/api`)
180+
t.match(headers, { 'x-test': 'test' })
181+
})
161182
}
162183

163184
run()

0 commit comments

Comments
 (0)