Skip to content

Commit

Permalink
fix: leverage native IncomingMessage.headers method (#684)
Browse files Browse the repository at this point in the history
closes #680
  • Loading branch information
kibiz0r authored Aug 7, 2024
1 parent c3c1b9a commit 07f20fb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
36 changes: 35 additions & 1 deletion __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,40 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
expect(response).toEqual(expectedResponse)
})

test('headers get lowercased', async () => {
app = express()
router = express.Router()
app.use('/', router)
serverlessExpressInstance = serverlessExpress({ app })
router.get('/foo', (req, res) => {
const xHeaders = Object.fromEntries(
Object.entries(req.headers).filter(([name]) => name.startsWith('x-header-'))
)
res.json({ xHeaders })
})
const event = makeEvent({
eventSourceName: 'apiGatewayV1',
path: '/foo',
httpMethod: 'GET',
multiValueHeaders: undefined,
headers: {
'X-Header-One': 'Value1',
'x-header-two': 'Value2'
}
})
const response = await serverlessExpressInstance(event)
const expectedResponse = makeResponse({
eventSourceName: 'apiGatewayV1',
body: JSON.stringify({
xHeaders: {
'x-header-one': 'Value1',
'x-header-two': 'Value2'
}
})
})
expect(response).toMatchObject(expectedResponse)
})

test('resolutionMode = CALLBACK', (done) => {
const jsonResponse = { data: { name: 'Brett' } }
router.get('/users', (req, res) => {
Expand Down Expand Up @@ -287,7 +321,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
const response = await serverlessExpressInstance(event)
const expectedResponse = makeResponse({
eventSourceName,
body: JSON.stringify({ data: { name: name } }),
body: JSON.stringify({ data: { name } }),
multiValueHeaders: {
'content-length': ['29'],
etag: ['W/"1d-9ERga12t1e/5eBdg3k9zfIvAfWo"']
Expand Down
11 changes: 10 additions & 1 deletion src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ module.exports = class ServerlessRequest extends http.IncomingMessage {
destroy: Function.prototype
})

// IncomingMessage has a lot of logic for when to lowercase or alias well-known header names,
// so we delegate to that logic here
const headerEntries = Object.entries(headers)
const rawHeaders = new Array(headerEntries.length * 2)
for (let i = 0; i < headerEntries.length; i++) {
rawHeaders[i * 2] = headerEntries[i][0]
rawHeaders[i * 2 + 1] = headerEntries[i][1]
}
this._addHeaderLines(rawHeaders, rawHeaders.length)

Object.assign(this, {
ip: remoteAddress,
complete: true,
httpVersion: '1.1',
httpVersionMajor: '1',
httpVersionMinor: '1',
method,
headers,
body,
url
})
Expand Down

0 comments on commit 07f20fb

Please sign in to comment.