Skip to content
Open
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
18 changes: 16 additions & 2 deletions src/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,23 @@ export class Redirect extends Macroable {
* @param query - Query string parameters to append
*/
#sendResponse(url: string, query: Record<string, any>) {
const stringified = this.#qs.stringify(query)
if (this.#qs.stringify(query)) {
/**
* Keep the fragment at the end, and merge the new query string
* with the one the url may already have. The new values replace
* the same keys of the url, as in "back()".
*/
const hashIndex = url.indexOf('#')
const hash = hashIndex === -1 ? '' : url.slice(hashIndex)
const base = hashIndex === -1 ? url : url.slice(0, hashIndex)

const queryIndex = base.indexOf('?')
const pathname = queryIndex === -1 ? base : base.slice(0, queryIndex)
const existingQuery = queryIndex === -1 ? {} : this.#qs.parse(base.slice(queryIndex + 1))

url = `${pathname}?${this.#qs.stringify(Object.assign(existingQuery, query))}${hash}`
}

url = stringified ? `${url}?${stringified}` : url
debug('redirecting to url "%s"', url)

this.#response.location(encodeUrl(url))
Expand Down
55 changes: 55 additions & 0 deletions tests/redirect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,61 @@ test.group('Redirect', () => {
assert.equal(header.location, '/posts/1')
})

test('append query string to a url that already has one', ({ assert }) => {
const response = new HttpResponseFactory().merge({ encryption, router }).create()
response.redirect().withQs({ success: 1 }).toPath('/users?page=2')

assert.equal(response.getHeader('location'), '/users?page=2&success=1')
})

test('add query string before the url fragment', ({ assert }) => {
const response = new HttpResponseFactory().merge({ encryption, router }).create()
response.redirect().withQs({ success: 1 }).toPath('/users?page=2#list')

assert.equal(response.getHeader('location'), '/users?page=2&success=1#list')
})

test('add query string before the fragment of a url without one', ({ assert }) => {
const response = new HttpResponseFactory().merge({ encryption, router }).create()
response.redirect().withQs({ success: 1 }).toPath('/users#list')

assert.equal(response.getHeader('location'), '/users?success=1#list')
})

test('replace the keys of the url query string with the custom query string', ({ assert }) => {
const response = new HttpResponseFactory().merge({ encryption, router }).create()
response.redirect().withQs({ page: 3 }).toPath('/users?page=2')

assert.equal(response.getHeader('location'), '/users?page=3')
})

test('replace the keys of the url query string with the forwarded query string', async ({
assert,
}) => {
const { url } = await httpServer.create((req, res) => {
const response = new HttpResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect('/users?page=2&sort=name', true)
response.finish()
})

const { header } = await supertest(url).get('/?page=3').redirects(1)
assert.equal(header.location, '/users?page=3&sort=name')
})

test('keep the host of an absolute url when merging the query string', ({ assert }) => {
const response = new HttpResponseFactory().merge({ encryption, router }).create()
response.redirect().withQs({ success: 1 }).toPath('https://example.com/users?page=2')

assert.equal(response.getHeader('location'), 'https://example.com/users?page=2&success=1')
})

test('keep the url as it is when there is no query string to add', ({ assert }) => {
const response = new HttpResponseFactory().merge({ encryption, router }).create()
response.redirect().toPath('/users?page=2&page=3#list')

assert.equal(response.getHeader('location'), '/users?page=2&page=3#list')
})

test('encode route params before redirecting', ({ assert }) => {
const route = new RouterFactory().merge({ app, encryption }).create()
route.get('/:page', 'PagesController.show').as('pages.show')
Expand Down
Loading