Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

set Content-Length header in mapApiGatewayEventToHttpRequest #147

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
7 changes: 7 additions & 0 deletions index.js
Expand Up @@ -33,6 +33,13 @@ function isContentTypeBinaryMimeType(params) {

function mapApiGatewayEventToHttpRequest(event, context, socketPath) {
const headers = event.headers || {} // NOTE: Mutating event.headers; prefer deep clone of event.headers
// NOTE: API Gateway is not setting Content-Length header on any requests even when they have a body
if (event.body && !headers['Content-Length']) {
// set header on event directly so x-apigateway-event is set correctly later
if (!event.headers) event.headers = {}
event.headers['Content-Length'] = Buffer.byteLength(event.body)
headers['Content-Length'] = Buffer.byteLength(event.body)
}
const eventWithoutBody = Object.assign({}, event)
delete eventWithoutBody.body

Expand Down