Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
- fix failing test cases
- add comment about motivation for change in index.js
  • Loading branch information
HotelCalifornia committed Jun 21, 2018
1 parent 1ef34b8 commit 9c8541e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions __tests__/index.js
Expand Up @@ -51,6 +51,8 @@ function mapApiGatewayEventToHttpRequest(headers) {
headers
}
const eventClone = JSON.parse(JSON.stringify(event))
// NOTE: mapApiGatewayEventToHttpRequest will specify Content-Length if not specified
eventClone.headers = Object.assign(eventClone.headers, {'Content-Length': Buffer.byteLength(eventClone.body)})
delete eventClone.body
const context = {
'foo': 'bar'
Expand All @@ -68,6 +70,7 @@ test('mapApiGatewayEventToHttpRequest: with headers', () => {
method: 'GET',
path: '/foo',
headers: {
'Content-Length': Buffer.byteLength('Hello serverless!'),
'x-foo': 'foo',
'x-apigateway-event': encodeURIComponent(JSON.stringify(r.eventClone)),
'x-apigateway-context': encodeURIComponent(JSON.stringify(r.context))
Expand All @@ -83,6 +86,7 @@ test('mapApiGatewayEventToHttpRequest: without headers', () => {
method: 'GET',
path: '/foo',
headers: {
'Content-Length': Buffer.byteLength('Hello serverless!'),
'x-apigateway-event': encodeURIComponent(JSON.stringify(r.eventClone)),
'x-apigateway-context': encodeURIComponent(JSON.stringify(r.context))
},
Expand Down
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -35,7 +35,8 @@ function mapApiGatewayEventToHttpRequest(event, context, socketPath) {
const headers = event.headers || {} // NOTE: Mutating event.headers; prefer deep clone of event.headers
const eventWithoutBody = Object.assign({}, event)
delete eventWithoutBody.body


// NOTE: API Gateway is not setting Content-Length header on any requests even when they have a body
if (event.body && !headers['Content-Length']) {
headers['Content-Length'] = Buffer.byteLength(event.body)
}
Expand Down

0 comments on commit 9c8541e

Please sign in to comment.