Commit ab0a8647 authored by Jan Potoms's avatar Jan Potoms Committed by Tim Neutkens

API: Take multiple values into account for the query (#9196)

* Take multiple values into account

* make typescript happy
parent 20978af0
...@@ -119,7 +119,15 @@ export function getQueryParser({ url }: IncomingMessage) { ...@@ -119,7 +119,15 @@ export function getQueryParser({ url }: IncomingMessage) {
const query: { [key: string]: string | string[] } = {} const query: { [key: string]: string | string[] } = {}
for (const [key, value] of params) { for (const [key, value] of params) {
query[key] = value if (query[key]) {
if (Array.isArray(query[key])) {
;(query[key] as string[]).push(value)
} else {
query[key] = [query[key], value]
}
} else {
query[key] = value
}
} }
return query return query
......
export default (req, res) => {
res.status(200).send(req.query)
}
...@@ -180,6 +180,23 @@ function runTests (serverless = false) { ...@@ -180,6 +180,23 @@ function runTests (serverless = false) {
expect(data).toEqual({ message: 'Parsed body' }) expect(data).toEqual({ message: 'Parsed body' })
}) })
it('should return empty query object', async () => {
const data = await fetchViaHTTP(appPort, '/api/query', null, {}).then(
res => res.ok && res.json()
)
expect(data).toEqual({})
})
it('should parse query correctly', async () => {
const data = await fetchViaHTTP(
appPort,
'/api/query?a=1&b=2&a=3',
null,
{}
).then(res => res.ok && res.json())
expect(data).toEqual({ a: ['1', '3'], b: '2' })
})
it('should return empty cookies object', async () => { it('should return empty cookies object', async () => {
const data = await fetchViaHTTP(appPort, '/api/cookies', null, {}).then( const data = await fetchViaHTTP(appPort, '/api/cookies', null, {}).then(
res => res.ok && res.json() res => res.ok && res.json()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment