Skip to content

Commit 59ddc52

Browse files
rozzillagurgunday
andauthored
fix(qs): preserve fromParameters query params (#451)
* fix(qs): preserve fromParameters query params Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com> * Update index.js Co-authored-by: Gürgün Dayıoğlu <hey@gurgun.day> Signed-off-by: Roberto Bianchi <rbianchidev@gmail.com> --------- Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com> Signed-off-by: Roberto Bianchi <rbianchidev@gmail.com> Co-authored-by: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent ffcf5bb commit 59ddc52

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -605,29 +605,31 @@ async function fastifyHttpProxy (fastify, opts) {
605605
}
606606

607607
dest = dest.replace(prefixPathWithVariables, rewritePrefixWithVariables)
608-
if (queryParams) {
609-
dest += `?${qs.stringify(queryParams)}`
610-
}
611608
} else {
612609
dest = dest.replace(prefix, rewritePrefix)
613610
}
614611

612+
if (queryParams) {
613+
dest += `?${qs.stringify(queryParams)}`
614+
}
615+
615616
return { url: dest || '/', options: replyOpts }
616617
}
617618

618619
function handler (request, reply) {
619620
const { url, options } = fromParameters(request.url, request.params, this.prefix)
621+
const dest = url.split('?', 1)[0]
620622

621623
if (request.raw[kWs]) {
622624
reply.hijack()
623625
try {
624-
wsProxy.handleUpgrade(request, url, noop)
626+
wsProxy.handleUpgrade(request, dest, noop)
625627
} /* c8 ignore start */ catch (err) {
626628
request.log.warn({ err }, 'websocket proxy error')
627629
} /* c8 ignore stop */
628630
return
629631
}
630-
reply.from(url, options)
632+
reply.from(dest, options)
631633
}
632634

633635
fastify.decorateReply('fromParameters', fromParameters)

test/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ async function run () {
3838
return 'this is /api2/a'
3939
})
4040

41+
origin.get('/api2/echo-query', async (request) => {
42+
return `query: ${JSON.stringify(request.query)}`
43+
})
44+
4145
origin.get('/variable-api/:id/endpoint', async (request) => {
4246
return `this is "variable-api" endpoint with id ${request.params.id}`
4347
})
@@ -900,6 +904,27 @@ async function run () {
900904
}
901905
})
902906

907+
test('fromParameters should preserve query params with non-parameterized prefix', async t => {
908+
let fromParametersUrl
909+
const server = Fastify()
910+
server.register(proxy, {
911+
upstream: `http://localhost:${origin.server.address().port}`,
912+
prefix: '/api',
913+
rewritePrefix: '/api2',
914+
preHandler (request, reply, done) {
915+
const { url } = reply.fromParameters(request.url, request.params, '/api')
916+
fromParametersUrl = url
917+
done()
918+
}
919+
})
920+
921+
await server.listen({ port: 0 })
922+
t.after(() => server.close())
923+
924+
await fetch(`http://localhost:${server.server.address().port}/api/echo-query?foo=bar&baz=qux`)
925+
t.assert.strictEqual(fromParametersUrl, '/api2/echo-query?foo=bar&baz=qux')
926+
})
927+
903928
test('preRewrite handler', async t => {
904929
const proxyServer = Fastify()
905930

0 commit comments

Comments
 (0)