Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,13 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {

function getQueryString (search, reqUrl, opts, request) {
if (typeof opts.queryString === 'function') {
return '?' + opts.queryString(search, reqUrl, request)
const qs = opts.queryString(search, reqUrl, request)
return qs ? '?' + qs : ''
}

if (opts.queryString) {
return '?' + querystring.stringify(opts.queryString)
const qs = querystring.stringify(opts.queryString)
return qs ? '?' + qs : ''
}

if (search.length > 0) {
Expand Down
41 changes: 41 additions & 0 deletions test/full-querystring-rewrite-option-empty-object.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const t = require('node:test')
const Fastify = require('fastify')
const { request } = require('undici')
const From = require('..')
const http = require('node:http')

const instance = Fastify()

t.test('queryString empty object should not append trailing ?', async (t) => {
t.plan(4)
t.after(() => instance.close())

const target = http.createServer((req, res) => {
t.assert.ok('request proxied')
t.assert.strictEqual(req.url, '/world')
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('hello world')
})

instance.get('/hello', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}/world`, {
queryString: {}
})
})

t.after(() => target.close())

await new Promise(resolve => target.listen({ port: 0 }, resolve))

instance.register(From)

await new Promise(resolve => instance.listen({ port: 0 }, resolve))

const result = await request(`http://localhost:${instance.server.address().port}/hello`)

t.assert.strictEqual(result.statusCode, 200)
t.assert.strictEqual(await result.body.text(), 'hello world')
})
43 changes: 43 additions & 0 deletions test/full-querystring-rewrite-option-function-empty.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const t = require('node:test')
const Fastify = require('fastify')
const { request } = require('undici')
const From = require('..')
const http = require('node:http')

const instance = Fastify()

t.test('queryString function returning empty string should not append trailing ?', async (t) => {
t.plan(4)
t.after(() => instance.close())

const target = http.createServer((req, res) => {
t.assert.ok('request proxied')
t.assert.strictEqual(req.url, '/world')
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('hello world')
})

instance.get('/hello', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}/world`, {
queryString () {
return ''
}
})
})

t.after(() => target.close())

await new Promise(resolve => target.listen({ port: 0 }, resolve))

instance.register(From)

await new Promise(resolve => instance.listen({ port: 0 }, resolve))

const result = await request(`http://localhost:${instance.server.address().port}/hello`)

t.assert.strictEqual(result.statusCode, 200)
t.assert.strictEqual(await result.body.text(), 'hello world')
})