Skip to content

Commit a3b3cfb

Browse files
authored
fix: prevent internal error when qs is sent (#228)
* fix: prevent internal error when qs is sent * fix: lint issue
1 parent eafbc01 commit a3b3cfb

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ async function httpProxy (fastify, opts) {
184184
})
185185

186186
function handler (request, reply) {
187-
let dest = request.raw.url
187+
const queryParamIndex = request.raw.url.indexOf('?')
188+
let dest = request.raw.url.slice(0, queryParamIndex !== -1 ? queryParamIndex : undefined)
188189
dest = dest.replace(this.prefix, rewritePrefix)
189190
reply.from(dest || '/', replyOpts)
190191
}

test/test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,38 @@ async function run () {
654654
})
655655
t.equal(resultUnproxied.body, 'this is unproxied a')
656656
})
657+
658+
test('prefixed proxy with query search', async t => {
659+
const appServer = Fastify()
660+
661+
appServer.get('/second-service', async (request, reply) => {
662+
return `Hello World - lang = ${request.query.lang}`
663+
})
664+
appServer.get('/second-service/foo', async (request, reply) => {
665+
return `Hello World (foo) - lang = ${request.query.lang}`
666+
})
667+
const address = await appServer.listen(0)
668+
669+
const proxyServer = Fastify()
670+
proxyServer.register(proxy, {
671+
upstream: `${address}/second-service`,
672+
prefix: '/second-service'
673+
})
674+
const proxyAddress = await proxyServer.listen(0)
675+
676+
t.teardown(appServer.close.bind(appServer))
677+
t.teardown(proxyServer.close.bind(proxyServer))
678+
679+
const resultRoot = await got(
680+
`${proxyAddress}/second-service?lang=en`
681+
)
682+
t.equal(resultRoot.body, 'Hello World - lang = en')
683+
684+
const resultFooRoute = await got(
685+
`${proxyAddress}/second-service/foo?lang=en`
686+
)
687+
t.equal(resultFooRoute.body, 'Hello World (foo) - lang = en')
688+
})
657689
}
658690

659691
run()

0 commit comments

Comments
 (0)