Skip to content

Commit

Permalink
Merge pull request #16 from jeremydaly/v0.3.1
Browse files Browse the repository at this point in the history
v0.3.1
  • Loading branch information
jeremydaly authored Mar 23, 2018
2 parents 5e23d38 + d4e663e commit 514d73a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ Lambda Proxy Integration is an option in API Gateway that allows the details of

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

## Install

```
npm i lambda-api --save
```

## Configuration

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.
Expand Down Expand Up @@ -161,7 +167,8 @@ The `REQUEST` object contains a parsed and normalized request from API Gateway.
- `method`: The HTTP method of the request
- `path`: The path passed in by the request
- `query`: Querystring parameters parsed into an object
- `headers`: An object containing the request headers
- `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))
- `rawHeaders`: An object containing the original request headers (property case preserved)
- `body`: The body of the request.
- If the `Content-Type` header is `application/json`, it will attempt to parse the request using `JSON.parse()`
- If the `Content-Type` header is `application/x-www-form-urlencoded`, it will attempt to parse a URL encoded string using `querystring`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-api",
"version": "0.3.0",
"version": "0.3.1",
"description": "Lightweight web framework for your serverless applications",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 7 additions & 4 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ class REQUEST {
this.query = app._event.queryStringParameters ? app._event.queryStringParameters : {}

// Set the headers
this.headers = app._event.headers
this.rawHeaders = app._event.headers

this.headers = Object.keys(this.rawHeaders).reduce((acc,header) =>
Object.assign(acc,{[header.toLowerCase()]:this.rawHeaders[header]}), {})

// Set and parse cookies
this.cookies = app._event.headers.Cookie ?
app._event.headers.Cookie.split(';')
this.cookies = this.headers.cookie ?
this.headers.cookie.split(';')
.reduce(
(acc,cookie) => {
cookie = cookie.trim().split('=')
Expand All @@ -53,7 +56,7 @@ class REQUEST {
this.requestContext = app._event.requestContext

// Set the body
if (this.headers['Content-Type'] && this.headers['Content-Type'].includes("application/x-www-form-urlencoded")) {
if (this.headers['content-type'] && this.headers['content-type'].includes("application/x-www-form-urlencoded")) {
this.body = QS.parse(app._event.body)
} else if (typeof app._event.body === 'object') {
this.body = app._event.body
Expand Down
65 changes: 65 additions & 0 deletions test/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,38 @@ describe('Route Tests:', function() {
})
}) // end it

it('With "x-www-form-urlencoded; charset=UTF-8" body: /test/form', function() {
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' } })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
// console.log(result);
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"post","status":"ok","body":{"test":"123","test2":"456"}}' })
})
}) // end it

it('With x-www-form-urlencoded body and lowercase "Content-Type" header: /test/form', function() {
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' } })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
// console.log(result);
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"post","status":"ok","body":{"test":"123","test2":"456"}}' })
})
}) // end it

it('With x-www-form-urlencoded body and mixed case "Content-Type" header: /test/form', function() {
let _event = Object.assign({},event,{ path: '/test/form', httpMethod: 'post', body: 'test=123&test2=456', headers: { 'CoNtEnt-TYPe': 'application/x-www-form-urlencoded' } })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
// console.log(result);
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"post","status":"ok","body":{"test":"123","test2":"456"}}' })
})
}) // end it

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

it('With "x-www-form-urlencoded; charset=UTF-8" body: /test/form', function() {
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' } })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
// console.log(result);
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","body":{"test":"123","test2":"456"}}' })
})
}) // end it

it('With x-www-form-urlencoded body and lowercase "Content-Type" header: /test/form', function() {
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' } })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
// console.log(result);
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","body":{"test":"123","test2":"456"}}' })
})
}) // end it

it('With x-www-form-urlencoded body and mixed case "Content-Type" header: /test/form', function() {
let _event = Object.assign({},event,{ path: '/test/form', httpMethod: 'put', body: 'test=123&test2=456', headers: { 'CoNtEnt-TYPe': 'application/x-www-form-urlencoded' } })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
// console.log(result);
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"put","status":"ok","body":{"test":"123","test2":"456"}}' })
})
}) // end it


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

0 comments on commit 514d73a

Please sign in to comment.