Skip to content

Commit 07f20fb

Browse files
authored
fix: leverage native IncomingMessage.headers method (#684)
closes #680
1 parent c3c1b9a commit 07f20fb

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

__tests__/integration.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,40 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
103103
expect(response).toEqual(expectedResponse)
104104
})
105105

106+
test('headers get lowercased', async () => {
107+
app = express()
108+
router = express.Router()
109+
app.use('/', router)
110+
serverlessExpressInstance = serverlessExpress({ app })
111+
router.get('/foo', (req, res) => {
112+
const xHeaders = Object.fromEntries(
113+
Object.entries(req.headers).filter(([name]) => name.startsWith('x-header-'))
114+
)
115+
res.json({ xHeaders })
116+
})
117+
const event = makeEvent({
118+
eventSourceName: 'apiGatewayV1',
119+
path: '/foo',
120+
httpMethod: 'GET',
121+
multiValueHeaders: undefined,
122+
headers: {
123+
'X-Header-One': 'Value1',
124+
'x-header-two': 'Value2'
125+
}
126+
})
127+
const response = await serverlessExpressInstance(event)
128+
const expectedResponse = makeResponse({
129+
eventSourceName: 'apiGatewayV1',
130+
body: JSON.stringify({
131+
xHeaders: {
132+
'x-header-one': 'Value1',
133+
'x-header-two': 'Value2'
134+
}
135+
})
136+
})
137+
expect(response).toMatchObject(expectedResponse)
138+
})
139+
106140
test('resolutionMode = CALLBACK', (done) => {
107141
const jsonResponse = { data: { name: 'Brett' } }
108142
router.get('/users', (req, res) => {
@@ -287,7 +321,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
287321
const response = await serverlessExpressInstance(event)
288322
const expectedResponse = makeResponse({
289323
eventSourceName,
290-
body: JSON.stringify({ data: { name: name } }),
324+
body: JSON.stringify({ data: { name } }),
291325
multiValueHeaders: {
292326
'content-length': ['29'],
293327
etag: ['W/"1d-9ERga12t1e/5eBdg3k9zfIvAfWo"']

src/request.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@ module.exports = class ServerlessRequest extends http.IncomingMessage {
1515
destroy: Function.prototype
1616
})
1717

18+
// IncomingMessage has a lot of logic for when to lowercase or alias well-known header names,
19+
// so we delegate to that logic here
20+
const headerEntries = Object.entries(headers)
21+
const rawHeaders = new Array(headerEntries.length * 2)
22+
for (let i = 0; i < headerEntries.length; i++) {
23+
rawHeaders[i * 2] = headerEntries[i][0]
24+
rawHeaders[i * 2 + 1] = headerEntries[i][1]
25+
}
26+
this._addHeaderLines(rawHeaders, rawHeaders.length)
27+
1828
Object.assign(this, {
1929
ip: remoteAddress,
2030
complete: true,
2131
httpVersion: '1.1',
2232
httpVersionMajor: '1',
2333
httpVersionMinor: '1',
2434
method,
25-
headers,
2635
body,
2736
url
2837
})

0 commit comments

Comments
 (0)