Skip to content

Commit

Permalink
fix: add in proper handling defaultHeaders
Browse files Browse the repository at this point in the history
Fix how defaultHeaders were applied to multiValueHeaders

Closes #1256
  • Loading branch information
willfarrell committed Nov 10, 2024
1 parent f7b2841 commit 09be515
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"outdated": "npm outdated --workspaces",
"audit": "npm audit fix --workspaces",
"release:sync": "npm version $npm_package_version --workspaces && find ./packages -name \"package.json\" -exec sed -i '' -E \"s#\\\"@middy/(.*)\\\": ([^,]*)#\\\"@middy/\\1\\\": $(npm pkg get version)#g\" {} \\;",
"release:tag": "git tag $npm_package_version && git push --tags",
"release:tag": "git tag $npm_package_version",
"release:publish": "npm publish --workspaces",
"release:publish:next": "npm publish --tag next --workspaces"
},
Expand Down
32 changes: 16 additions & 16 deletions packages/http-header-normalizer/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ test('It should normalize (lowercase) all the headers with defaults', async (t)
httpHeaderNormalizer({
defaultHeaders: {
'Content-Type': 'application/json',
Accept: '*/*'
Accept: 'application/json,*/*'
}
})
)
Expand Down Expand Up @@ -210,32 +210,32 @@ test('It should normalize (lowercase) all the multiValueHeaders with defaults',
.use(
httpHeaderNormalizer({
defaultHeaders: {
'Content-Type': 'application/json',
Accept: '*/*'
'Content-Type': 'application/json,*/*',
Accept: ['application/json', '*/*']
}
})
)
.handler((event, context) => event)

const event = {
multiValueHeaders: {
'x-aPi-key': '123456',
tcn: 'abc',
te: 'cde',
DNS: 'd',
FOO: 'bar',
Accept: 'application/json'
'x-aPi-key': ['123456'],
tcn: ['abc'],
te: ['cde'],
DNS: ['d'],
FOO: ['bar'],
Accept: ['application/json']
}
}

const expectedHeaders = {
'x-api-key': '123456',
tcn: 'abc',
te: 'cde',
dns: 'd',
foo: 'bar',
accept: 'application/json',
'content-type': 'application/json'
'x-api-key': ['123456'],
tcn: ['abc'],
te: ['cde'],
dns: ['d'],
foo: ['bar'],
accept: ['application/json'],
'content-type': ['application/json', '*/*']
}

const resultingEvent = await handler(event, context)
Expand Down
19 changes: 10 additions & 9 deletions packages/http-header-normalizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,39 @@ const httpHeaderNormalizerMiddleware = (opts = {}) => {
const options = { ...defaults, ...opts }

const defaultHeaders = {}
const defaultMultiValueHeaders = {}
for (const key of Object.keys(options.defaultHeaders)) {
defaultHeaders[options.normalizeHeaderKey(key, options.canonical)] =
options.defaultHeaders[key]
const newKey = options.normalizeHeaderKey(key, options.canonical)
const isArray = Array.isArray(options.defaultHeaders[key])
defaultHeaders[newKey] = isArray
? options.defaultHeaders[key].join(',')
: options.defaultHeaders[key]
defaultMultiValueHeaders[newKey] = isArray
? options.defaultHeaders[key]
: options.defaultHeaders[key].split(',')
}

const httpHeaderNormalizerMiddlewareBefore = async (request) => {
if (request.event.headers) {
// const rawHeaders = {}
const headers = { ...defaultHeaders }

for (const key of Object.keys(request.event.headers)) {
// rawHeaders[key] = request.event.headers[key]
headers[options.normalizeHeaderKey(key, options.canonical)] =
request.event.headers[key]
}

request.event.headers = headers
// request.event.rawHeaders = rawHeaders
}

if (request.event.multiValueHeaders) {
// const rawHeaders = {}
const headers = { ...defaultHeaders }
const headers = { ...defaultMultiValueHeaders }

for (const key of Object.keys(request.event.multiValueHeaders)) {
// rawHeaders[key] = request.event.multiValueHeaders[key]
headers[options.normalizeHeaderKey(key, options.canonical)] =
request.event.multiValueHeaders[key]
}

request.event.multiValueHeaders = headers
// request.event.rawMultiValueHeaders = rawHeaders
}
}

Expand Down

0 comments on commit 09be515

Please sign in to comment.