Skip to content

Commit 3fce7ef

Browse files
committed
fix query fallback and tests
1 parent e572f6c commit 3fce7ef

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

lib/request.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ class REQUEST {
7070
// Set the query parameters (backfill for ALB)
7171
this.query = Object.assign({}, this.app._event.queryStringParameters,
7272
'queryStringParameters' in this.app._event ? {} // do nothing
73-
: Object.assign({},Object.keys(this.app._event.multiValueQueryStringParameters)
73+
: Object.keys(Object.assign({},this.app._event.multiValueQueryStringParameters))
7474
.reduce((qs,key) => Object.assign(qs, // get the last value of the array
7575
{ [key]: this.app._event.multiValueQueryStringParameters[key].slice(-1)[0] }
76-
), {}))
76+
), {})
7777
)
7878

7979
// Set the multi-value query parameters (simulate if no mult-value support)
@@ -84,6 +84,8 @@ class REQUEST {
8484

8585
// Set the raw headers
8686
this.rawHeaders = this.app._event.headers || {}
87+
// this.rawHeaders = this._multiValueSupport ? this.app._event.multiValueHeaders
88+
// : this.app._event.headers
8789

8890
// Set the headers to lowercase
8991
this.headers = Object.keys(this.rawHeaders).reduce((acc,header) =>

test/routes.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,21 @@ api.delete('/test/:test/:test2', function(req,res) {
107107

108108
api.get('/test/:test/query', function(req,res) {
109109
// console.log(req)
110-
res.status(200).json({ method: 'get', status: 'ok', param: req.params.test, query: req.query.test ? req.query.test : req.multiValueQuery.test })
110+
res.status(200).json({ method: 'get', status: 'ok', param: req.params.test, query: req.query, multiValueQuery: req.multiValueQuery })
111111
})
112112

113113
api.post('/test/:test/query', function(req,res) {
114114
// console.log(req)
115-
res.status(200).json({ method: 'post', status: 'ok', param: req.params.test, query: req.query.test ? req.query.test : req.multiValueQuery.test })
115+
res.status(200).json({ method: 'post', status: 'ok', param: req.params.test, query: req.query, multiValueQuery: req.multiValueQuery })
116116
})
117117

118118
api.put('/test/:test/query', function(req,res) {
119119
// console.log(req)
120-
res.status(200).json({ method: 'put', status: 'ok', param: req.params.test, query: req.query.test ? req.query.test : req.multiValueQuery.test })
120+
res.status(200).json({ method: 'put', status: 'ok', param: req.params.test, query: req.query, multiValueQuery: req.multiValueQuery })
121121
})
122122

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

127127
api.get('/test/:test/query/:test2', function(req,res) {
@@ -312,13 +312,13 @@ describe('Route Tests:', function() {
312312
it('Path with parameter and querystring: /test/123/query/?test=321', async function() {
313313
let _event = Object.assign({},event,{ path: '/test/123/query', queryStringParameters: { test: '321' } })
314314
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
315-
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123","query":"321"}', isBase64Encoded: false })
315+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123","query":{"test":"321"},"multiValueQuery":{"test":["321"]}}', isBase64Encoded: false })
316316
}) // end it
317317

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

324324
it('Path with multiple parameters and querystring: /test/123/query/456/?test=321', async function() {
@@ -331,13 +331,13 @@ describe('Route Tests:', function() {
331331
it('Event path + querystring w/ trailing slash (this shouldn\'t happen with API Gateway)', async function() {
332332
let _event = Object.assign({},event,{ path: '/test/123/query/?test=321', queryStringParameters: { test: '321' } })
333333
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
334-
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123","query":"321"}', isBase64Encoded: false })
334+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123","query":{"test":"321"},"multiValueQuery":{"test":["321"]}}', isBase64Encoded: false })
335335
}) // end it
336336

337337
it('Event path + querystring w/o trailing slash (this shouldn\'t happen with API Gateway)', async function() {
338338
let _event = Object.assign({},event,{ path: '/test/123/query?test=321', queryStringParameters: { test: '321' } })
339339
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
340-
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123","query":"321"}', isBase64Encoded: false })
340+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123","query":{"test":"321"},"multiValueQuery":{"test":["321"]}}', isBase64Encoded: false })
341341
}) // end it
342342

343343

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

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

502502
it('Path with multiple parameters and querystring: /test/123/query/456/?test=321', async function() {
@@ -605,13 +605,13 @@ describe('Route Tests:', function() {
605605
it('Path with parameter and querystring: /test/123/query/?test=321', async function() {
606606
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'put', queryStringParameters: { test: '321' } })
607607
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
608-
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","param":"123","query":"321"}', isBase64Encoded: false })
608+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","param":"123","query":{"test":"321"},"multiValueQuery":{"test":["321"]}}', isBase64Encoded: false })
609609
}) // end it
610610

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

617617
it('Path with multiple parameters and querystring: /test/123/query/456/?test=321', async function() {
@@ -752,13 +752,13 @@ describe('Route Tests:', function() {
752752
it('Path with parameter and querystring: /test/123/query/?test=321', async function() {
753753
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'options', queryStringParameters: { test: '321' } })
754754
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
755-
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"options","status":"ok","param":"123","query":"321"}', isBase64Encoded: false })
755+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"options","status":"ok","param":"123","query":{"test":"321"},"multiValueQuery":{"test":["321"]}}', isBase64Encoded: false })
756756
}) // end it
757757

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

764764
it('Path with multiple parameters and querystring: /test/123/query/456/?test=321', async function() {

0 commit comments

Comments
 (0)