Skip to content

Commit 514d73a

Browse files
authored
Merge pull request #16 from jeremydaly/v0.3.1
v0.3.1
2 parents 5e23d38 + d4e663e commit 514d73a

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ Lambda Proxy Integration is an option in API Gateway that allows the details of
117117

118118
The API automatically parses this information to create a normalized `REQUEST` object. The request can then be routed using the APIs methods.
119119

120+
## Install
121+
122+
```
123+
npm i lambda-api --save
124+
```
125+
120126
## Configuration
121127

122128
Require the `lambda-api` module into your Lambda handler script and instantiate it. You can initialize the API with an optional `version` which can be accessed via the `REQUEST` object and a `base` path.
@@ -161,7 +167,8 @@ The `REQUEST` object contains a parsed and normalized request from API Gateway.
161167
- `method`: The HTTP method of the request
162168
- `path`: The path passed in by the request
163169
- `query`: Querystring parameters parsed into an object
164-
- `headers`: An object containing the request headers
170+
- `headers`: An object containing the request headers (properties converted to lowercase for HTTP/2, see [rfc7540 8.1.2. HTTP Header Fields](https://tools.ietf.org/html/rfc7540))
171+
- `rawHeaders`: An object containing the original request headers (property case preserved)
165172
- `body`: The body of the request.
166173
- If the `Content-Type` header is `application/json`, it will attempt to parse the request using `JSON.parse()`
167174
- If the `Content-Type` header is `application/x-www-form-urlencoded`, it will attempt to parse a URL encoded string using `querystring`

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambda-api",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Lightweight web framework for your serverless applications",
55
"main": "index.js",
66
"scripts": {

request.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ class REQUEST {
3636
this.query = app._event.queryStringParameters ? app._event.queryStringParameters : {}
3737

3838
// Set the headers
39-
this.headers = app._event.headers
39+
this.rawHeaders = app._event.headers
40+
41+
this.headers = Object.keys(this.rawHeaders).reduce((acc,header) =>
42+
Object.assign(acc,{[header.toLowerCase()]:this.rawHeaders[header]}), {})
4043

4144
// Set and parse cookies
42-
this.cookies = app._event.headers.Cookie ?
43-
app._event.headers.Cookie.split(';')
45+
this.cookies = this.headers.cookie ?
46+
this.headers.cookie.split(';')
4447
.reduce(
4548
(acc,cookie) => {
4649
cookie = cookie.trim().split('=')
@@ -53,7 +56,7 @@ class REQUEST {
5356
this.requestContext = app._event.requestContext
5457

5558
// Set the body
56-
if (this.headers['Content-Type'] && this.headers['Content-Type'].includes("application/x-www-form-urlencoded")) {
59+
if (this.headers['content-type'] && this.headers['content-type'].includes("application/x-www-form-urlencoded")) {
5760
this.body = QS.parse(app._event.body)
5861
} else if (typeof app._event.body === 'object') {
5962
this.body = app._event.body

test/routes.js

+65
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,38 @@ describe('Route Tests:', function() {
327327
})
328328
}) // end it
329329

330+
it('With "x-www-form-urlencoded; charset=UTF-8" body: /test/form', function() {
331+
let _event = Object.assign({},event,{ path: '/test/form', httpMethod: 'post', body: 'test=123&test2=456', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } })
332+
333+
return new Promise((resolve,reject) => {
334+
api.run(_event,{},function(err,res) { resolve(res) })
335+
}).then((result) => {
336+
// console.log(result);
337+
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"post","status":"ok","body":{"test":"123","test2":"456"}}' })
338+
})
339+
}) // end it
340+
341+
it('With x-www-form-urlencoded body and lowercase "Content-Type" header: /test/form', function() {
342+
let _event = Object.assign({},event,{ path: '/test/form', httpMethod: 'post', body: 'test=123&test2=456', headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' } })
343+
344+
return new Promise((resolve,reject) => {
345+
api.run(_event,{},function(err,res) { resolve(res) })
346+
}).then((result) => {
347+
// console.log(result);
348+
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"post","status":"ok","body":{"test":"123","test2":"456"}}' })
349+
})
350+
}) // end it
351+
352+
it('With x-www-form-urlencoded body and mixed case "Content-Type" header: /test/form', function() {
353+
let _event = Object.assign({},event,{ path: '/test/form', httpMethod: 'post', body: 'test=123&test2=456', headers: { 'CoNtEnt-TYPe': 'application/x-www-form-urlencoded' } })
354+
355+
return new Promise((resolve,reject) => {
356+
api.run(_event,{},function(err,res) { resolve(res) })
357+
}).then((result) => {
358+
// console.log(result);
359+
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"post","status":"ok","body":{"test":"123","test2":"456"}}' })
360+
})
361+
}) // end it
330362

331363
it('Missing path: /not_found', function() {
332364
let _event = Object.assign({},event,{ path: '/not_found', httpMethod: 'post' })
@@ -434,6 +466,39 @@ describe('Route Tests:', function() {
434466
})
435467
}) // end it
436468

469+
it('With "x-www-form-urlencoded; charset=UTF-8" body: /test/form', function() {
470+
let _event = Object.assign({},event,{ path: '/test/form', httpMethod: 'put', body: 'test=123&test2=456', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } })
471+
472+
return new Promise((resolve,reject) => {
473+
api.run(_event,{},function(err,res) { resolve(res) })
474+
}).then((result) => {
475+
// console.log(result);
476+
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","body":{"test":"123","test2":"456"}}' })
477+
})
478+
}) // end it
479+
480+
it('With x-www-form-urlencoded body and lowercase "Content-Type" header: /test/form', function() {
481+
let _event = Object.assign({},event,{ path: '/test/form', httpMethod: 'put', body: 'test=123&test2=456', headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' } })
482+
483+
return new Promise((resolve,reject) => {
484+
api.run(_event,{},function(err,res) { resolve(res) })
485+
}).then((result) => {
486+
// console.log(result);
487+
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","body":{"test":"123","test2":"456"}}' })
488+
})
489+
}) // end it
490+
491+
it('With x-www-form-urlencoded body and mixed case "Content-Type" header: /test/form', function() {
492+
let _event = Object.assign({},event,{ path: '/test/form', httpMethod: 'put', body: 'test=123&test2=456', headers: { 'CoNtEnt-TYPe': 'application/x-www-form-urlencoded' } })
493+
494+
return new Promise((resolve,reject) => {
495+
api.run(_event,{},function(err,res) { resolve(res) })
496+
}).then((result) => {
497+
// console.log(result);
498+
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","body":{"test":"123","test2":"456"}}' })
499+
})
500+
}) // end it
501+
437502

438503
it('Missing path: /not_found', function() {
439504
let _event = Object.assign({},event,{ path: '/not_found', httpMethod: 'put' })

0 commit comments

Comments
 (0)