Skip to content

Commit fa13aa8

Browse files
astralhpiEomm
authored andcommitted
fix: add request argument to rewriteHeaders (#270)
1 parent f276f53 commit fa13aa8

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,13 @@ async function httpProxy (fastify, opts) {
200200
fastify.addContentTypeParser('*', bodyParser)
201201
}
202202

203-
function rewriteHeaders (headers) {
203+
function rewriteHeaders (headers, req) {
204204
const location = headers.location
205205
if (location && !isExternalUrl(location)) {
206206
headers.location = location.replace(rewritePrefix, fastify.prefix)
207207
}
208208
if (oldRewriteHeaders) {
209-
headers = oldRewriteHeaders(headers)
209+
headers = oldRewriteHeaders(headers, req)
210210
}
211211
return headers
212212
}

test/test.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,10 @@ async function run () {
387387
upstream: `http://localhost:${origin.server.address().port}`,
388388
prefix: '/api',
389389
replyOptions: {
390-
rewriteHeaders: headers => Object.assign({ 'x-test': 'test' }, headers)
390+
rewriteHeaders: (headers, req) => Object.assign({
391+
'x-test': 'test',
392+
'x-req': req.headers['x-req']
393+
}, headers)
391394
}
392395
})
393396

@@ -397,10 +400,13 @@ async function run () {
397400
proxyServer.close()
398401
})
399402

400-
const { headers } = await got(
401-
`http://localhost:${proxyServer.server.address().port}/api`
402-
)
403-
t.match(headers, { 'x-test': 'test' })
403+
const { headers } = await got({
404+
url: `http://localhost:${proxyServer.server.address().port}/api`,
405+
headers: {
406+
'x-req': 'from-header'
407+
}
408+
})
409+
t.match(headers, { 'x-test': 'test', 'x-req': 'from-header' })
404410
})
405411

406412
test('rewritePrefix', async t => {

0 commit comments

Comments
 (0)