Skip to content

Commit 24c580f

Browse files
fix: ensure alb response header values are strings (#699)
* fix: stringify response headers as ALB expects response headers to be type string This was identified through using express within AWS Lambda which returns content-length header as a number which causes ALB to throw a 502 bad gateway exception. This change fixes this issue. * fix: stringify response headers as ALB expects response headers to be type string This was identified through using express within AWS Lambda which returns content-length header as a number which causes ALB to throw a 502 bad gateway exception. This change fixes this issue.
1 parent 5944638 commit 24c580f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

__tests__/integration-alb.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const express = require('express')
2+
const serverlessExpress = require('../src/index')
3+
const {
4+
makeEvent,
5+
makeResponse
6+
} = require('../jest-helpers')
7+
8+
describe('alb:express integration tests', () => {
9+
test('reasponse headers are of type string', async () => {
10+
const app = express()
11+
const router = express.Router()
12+
app.use('/', router)
13+
const serverlessExpressInstance = serverlessExpress({ app })
14+
router.get('/foo', (req, res) => {
15+
res.send('123')
16+
})
17+
const event = makeEvent({
18+
eventSourceName: 'alb',
19+
path: '/foo',
20+
httpMethod: 'GET',
21+
headers: {}
22+
})
23+
const response = await serverlessExpressInstance(event)
24+
const expectedResponse = makeResponse({
25+
eventSourceName: 'alb',
26+
body: '123',
27+
headers: {
28+
'content-length': '3',
29+
'content-type': 'text/html; charset=utf-8',
30+
'x-powered-by': 'Express',
31+
etag: 'W/"3-QL0AFWMIX8NRZTKeof9cXsvbvu8"'
32+
},
33+
multiValueHeaders: undefined
34+
})
35+
expect(response).toMatchObject(expectedResponse)
36+
})
37+
})

src/event-sources/aws/alb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const getResponseToAlb = ({
5252
const multiValueHeaders = !event.headers ? getMultiValueHeaders({ headers: responseHeaders }) : undefined
5353
const headers = event.headers
5454
? Object.entries(responseHeaders).reduce((acc, [k, v]) => {
55-
acc[k] = Array.isArray(v) ? v[0] : v
55+
acc[k] = Array.isArray(v) ? String(v[0]) : String(v)
5656
return acc
5757
}, {})
5858
: undefined

0 commit comments

Comments
 (0)