Skip to content

Commit

Permalink
Correctly append combined search parameters to paths
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Dec 15, 2023
1 parent 4064bca commit 3c94f2f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 17 deletions.
42 changes: 35 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,45 @@ const getFork = (forks, req) => {
}

/**
* Get original query string
* @param {object} req - Express request
* @returns {string} Original query string
* Get path with combined search params
* @param {string} originalUrl - Original URL path
* @param {string} path - Path to add params to
* @returns {string} Path with combined search params
*/
const getPathWithSearchParams = (originalUrl, path) => {
if (!path) {
return ''
}

// Add any path params to params on original URL
const originalParams = getSearchParams(originalUrl)
const pathParams = getSearchParams(path)
for (let [key, value] of originalParams.entries()) {
pathParams.append(key, value);
}

// Remove query string from path
path = path.split('?')[0]

return pathParams.size
? `${path}?${pathParams.toString()}`
: path
}

/**
* Get search parameters
* @param {string} originalUrl - Original URL path
* @returns {URLSearchParams} Search parameters
*/
const getOriginalQuery = (req) => {
const originalQueryString = req.originalUrl.split('?')[1]
const getSearchParams = (originalUrl) => {
const queryString = originalUrl.split('?')[1]
const searchParams = new URLSearchParams(queryString);

return originalQueryString ? `?${originalQueryString}` : ''
return searchParams
}

module.exports = {
getFork,
getOriginalQuery
getPathWithSearchParams,
getSearchParams
}
35 changes: 30 additions & 5 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,36 @@ describe('Utility functions', () => {
})
})

it('Gets original query string', () => {
const req1 = { originalUrl: '/admin/new?sort=desc' }
const req2 = { originalUrl: '/admin/new?sort=desc&page=1' }
it('Gets path with combined search params', async (t) => {
await t.test('when path has search params', () => {
const originalUrl = '/filter/location'
const path = '/list?success=1'
const result = utils.getPathWithSearchParams(originalUrl, path)

assert.equal(utils.getOriginalQuery(req1), '?sort=desc')
assert.equal(utils.getOriginalQuery(req2), '?sort=desc&page=1')
assert.equal(result, '/list?success=1')
})

await t.test('when original url has search params', () => {
const originalUrl = '/filter/location?page=2&sort=asc'
const path = '/list'
const result = utils.getPathWithSearchParams(originalUrl, path)

assert.equal(result, '/list?page=2&sort=asc')
})

await t.test('when original url and path both have search params', () => {
const originalUrl = '/filter/location?page=2&sort=asc'
const path = '/list?success=1'
const result = utils.getPathWithSearchParams(originalUrl, path)

assert.equal(result, '/list?success=1&page=2&sort=asc')
})
})

it('Gets search parameters', () => {
const result = utils.getSearchParams('/admin/new?sort=desc&page=1')

assert.equal(result.get('sort'), 'desc')
assert.equal(result.get('page'), '1')
})
})
24 changes: 24 additions & 0 deletions test/wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ describe('GOV.UK Prototype Wizard', () => {
})
})

it('Returns next path with modified query string', () => {
const journey = {
'/filter/location': {},
'/list?success=1': {}
}

const req = {
method: 'POST',
originalUrl: '/filter/location?page=2&sort=asc',
path: '/filter/location',
query: {
page: '2',
sort: 'asc'
},
session: { data: {} }
}

assert.deepEqual(wizard(journey, req), {
back: '',
current: '/filter/location?page=2&sort=asc',
next: '/list?success=1&page=2&sort=asc'
})
})

it('Adds fork data pointing back to where user forked from', () => {
const req = {
method: 'GET',
Expand Down
9 changes: 4 additions & 5 deletions wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ const utils = require('./lib/utils.js')
* @returns {Object} Next and back paths
*/
const wizard = (journey, req) => {
const { method, path, session } = req
const { method, originalUrl, path, session } = req
const { data } = session
const paths = Object.keys(journey)
const query = utils.getOriginalQuery(req)
const index = paths.indexOf(path)
let fork
let next
Expand Down Expand Up @@ -41,9 +40,9 @@ const wizard = (journey, req) => {
}

return {
next: next && next + query,
back: back && back + query,
current: path + query
next: utils.getPathWithSearchParams(originalUrl, next),
back: utils.getPathWithSearchParams(originalUrl, back),
current: originalUrl
}
}

Expand Down

0 comments on commit 3c94f2f

Please sign in to comment.