Skip to content

Commit

Permalink
fix query fallback and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Dec 23, 2018
1 parent e572f6c commit 3fce7ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
6 changes: 4 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ class REQUEST {
// Set the query parameters (backfill for ALB)
this.query = Object.assign({}, this.app._event.queryStringParameters,
'queryStringParameters' in this.app._event ? {} // do nothing
: Object.assign({},Object.keys(this.app._event.multiValueQueryStringParameters)
: Object.keys(Object.assign({},this.app._event.multiValueQueryStringParameters))
.reduce((qs,key) => Object.assign(qs, // get the last value of the array
{ [key]: this.app._event.multiValueQueryStringParameters[key].slice(-1)[0] }
), {}))
), {})
)

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

// Set the raw headers
this.rawHeaders = this.app._event.headers || {}
// this.rawHeaders = this._multiValueSupport ? this.app._event.multiValueHeaders
// : this.app._event.headers

// Set the headers to lowercase
this.headers = Object.keys(this.rawHeaders).reduce((acc,header) =>
Expand Down
28 changes: 14 additions & 14 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 ? req.query.test : req.multiValueQuery.test })
res.status(200).json({ method: 'get', status: 'ok', param: req.params.test, query: req.query, multiValueQuery: req.multiValueQuery })
})

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 ? req.query.test : req.multiValueQuery.test })
res.status(200).json({ method: 'post', status: 'ok', param: req.params.test, query: req.query, multiValueQuery: req.multiValueQuery })
})

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 ? req.query.test : req.multiValueQuery.test })
res.status(200).json({ method: 'put', status: 'ok', param: req.params.test, query: req.query, multiValueQuery: req.multiValueQuery })
})

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

api.get('/test/:test/query/:test2', function(req,res) {
Expand Down Expand Up @@ -312,13 +312,13 @@ describe('Route Tests:', function() {
it('Path with parameter and querystring: /test/123/query/?test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', queryStringParameters: { test: '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":"321"}', isBase64Encoded: false })
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 })
}) // 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', 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 })
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 })
}) // end it

it('Path with multiple parameters and querystring: /test/123/query/456/?test=321', async function() {
Expand All @@ -331,13 +331,13 @@ describe('Route Tests:', function() {
it('Event path + querystring w/ trailing slash (this shouldn\'t happen with API Gateway)', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query/?test=321', queryStringParameters: { test: '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":"321"}', isBase64Encoded: false })
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 })
}) // end it

it('Event path + querystring w/o trailing slash (this shouldn\'t happen with API Gateway)', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query?test=321', queryStringParameters: { test: '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":"321"}', isBase64Encoded: false })
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 })
}) // end it


Expand Down Expand Up @@ -490,13 +490,13 @@ describe('Route Tests:', function() {
it('Path with parameter and querystring: /test/123/query/?test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'post', queryStringParameters: { test: '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":"321"}', isBase64Encoded: false })
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 })
}) // 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', 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 })
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 })
}) // end it

it('Path with multiple parameters and querystring: /test/123/query/456/?test=321', async function() {
Expand Down Expand Up @@ -605,13 +605,13 @@ describe('Route Tests:', function() {
it('Path with parameter and querystring: /test/123/query/?test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'put', queryStringParameters: { test: '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":"321"}', isBase64Encoded: false })
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 })
}) // 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', 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 })
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 })
}) // end it

it('Path with multiple parameters and querystring: /test/123/query/456/?test=321', async function() {
Expand Down Expand Up @@ -752,13 +752,13 @@ describe('Route Tests:', function() {
it('Path with parameter and querystring: /test/123/query/?test=321', async function() {
let _event = Object.assign({},event,{ path: '/test/123/query', httpMethod: 'options', queryStringParameters: { test: '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":"321"}', isBase64Encoded: false })
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 })
}) // 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', 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 })
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 })
}) // end it

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

0 comments on commit 3fce7ef

Please sign in to comment.