Skip to content

Commit

Permalink
reimplement multi-value query params
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Dec 22, 2018
1 parent 882ab0d commit 1545475
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
5 changes: 4 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ class REQUEST {
this.path = this.app._event.path

// Set the query parameters
this.query = Object.assign({}, this.app._event.queryStringParameters, this.app._event.multiValueQueryStringParameters)
this.query = Object.assign({}, this.app._event.queryStringParameters)

// Set the multi-value query parameters
this.multiValueQuery = Object.assign({}, this.app._event.multiValueQueryStringParameters)

// Set the raw headers
this.rawHeaders = this.app._event.headers || {}
Expand Down
12 changes: 7 additions & 5 deletions test/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ describe('Request Tests:', function() {
expect(body.request.clientCountry).to.equal('US')
expect(body.request.route).to.equal('/test/hello')
expect(body.request.query.qs1).to.equal('foo')
expect(body.request.query.qs2).to.deep.equal(['foo','bar'])
expect(body.request.query.qs3).to.deep.equal(['bat','baz'])
// console.log(body);
expect(body.request.query.qs2).to.equal('bar')
expect(body.request.multiValueQuery.qs2).to.deep.equal(['foo','bar'])
expect(body.request.multiValueQuery.qs3).to.deep.equal(['bat','baz'])

})

it('Missing X-Forwarded-For (sourceIp fallback)', async function() {
Expand All @@ -61,8 +62,9 @@ describe('Request Tests:', function() {
expect(body.request.clientCountry).to.equal('US')
expect(body.request.route).to.equal('/test/hello')
expect(body.request.query.qs1).to.equal('foo')
expect(body.request.query.qs2).to.deep.equal(['foo','bar'])
expect(body.request.query.qs3).to.deep.equal(['bat','baz'])
expect(body.request.query.qs2).to.equal('bar')
expect(body.request.multiValueQuery.qs2).to.deep.equal(['foo','bar'])
expect(body.request.multiValueQuery.qs3).to.deep.equal(['bat','baz'])
// console.log(body);
})
})
Expand Down
18 changes: 9 additions & 9 deletions test/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ api.delete('/test/:test/:test2', function(req,res) {

api.get('/test/:test/query', function(req,res) {
// console.log(req)
res.status(200).json({ method: 'get', status: 'ok', param: req.params.test, query: req.query.test })
res.status(200).json({ method: 'get', status: 'ok', param: req.params.test, query: req.query.test ? req.query.test : req.multiValueQuery.test })
})

api.post('/test/:test/query', function(req,res) {
// console.log(req)
res.status(200).json({ method: 'post', status: 'ok', param: req.params.test, query: req.query.test })
res.status(200).json({ method: 'post', status: 'ok', param: req.params.test, query: req.query.test ? req.query.test : req.multiValueQuery.test })
})

api.put('/test/:test/query', function(req,res) {
// console.log(req)
res.status(200).json({ method: 'put', status: 'ok', param: req.params.test, query: req.query.test })
res.status(200).json({ method: 'put', status: 'ok', param: req.params.test, query: req.query.test ? req.query.test : req.multiValueQuery.test })
})

api.options('/test/:test/query', function(req,res) {
res.status(200).json({ method: 'options', status: 'ok', param: req.params.test, query: req.query.test })
res.status(200).json({ method: 'options', status: 'ok', param: req.params.test, query: req.query.test ? req.query.test : req.multiValueQuery.test })
})

api.get('/test/:test/query/:test2', function(req,res) {
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('Route Tests:', function() {
}) // end it

it('Path with parameter and multiple querystring: /test/123/query/?test=123&test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', queryStringParameters: { test: ['123', '321'] } })
let _event = Object.assign({},event,{ path: '/test/123/query', multiValueQueryStringParameters: { test: ['123', '321'] } })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123","query":["123","321"]}', isBase64Encoded: false })
}) // end it
Expand Down Expand Up @@ -420,7 +420,7 @@ describe('Route Tests:', function() {
}) // end it

it('Path with parameter and multiple querystring: /test/123/query/?test=123&test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'head', queryStringParameters: { test: ['123', '321'] } })
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'head', multiValueQueryStringParameters: { test: ['123', '321'] } })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '', isBase64Encoded: false })
}) // end it
Expand Down Expand Up @@ -494,7 +494,7 @@ describe('Route Tests:', function() {
}) // end it

it('Path with parameter and multiple querystring: /test/123/query/?test=123&test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'post', queryStringParameters: { test: ['123', '321'] } })
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'post', multiValueQueryStringParameters: { test: ['123', '321'] } })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"post","status":"ok","param":"123","query":["123","321"]}', isBase64Encoded: false })
}) // end it
Expand Down Expand Up @@ -609,7 +609,7 @@ describe('Route Tests:', function() {
}) // end it

it('Path with parameter and multiple querystring: /test/123/query/?test=123&test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'put', queryStringParameters: { test: ['123', '321'] } })
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'put', multiValueQueryStringParameters: { test: ['123', '321'] } })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","param":"123","query":["123","321"]}', isBase64Encoded: false })
}) // end it
Expand Down Expand Up @@ -756,7 +756,7 @@ describe('Route Tests:', function() {
}) // end it

it('Path with parameter and multiple querystring: /test/123/query/?test=123&test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'options', queryStringParameters: { test: ['123', '321'] } })
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'options', multiValueQueryStringParameters: { test: ['123', '321'] } })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"options","status":"ok","param":"123","query":["123","321"]}', isBase64Encoded: false })
}) // end it
Expand Down

0 comments on commit 1545475

Please sign in to comment.