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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Duplicate Headers #2264

Closed
wants to merge 6 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
12 changes: 6 additions & 6 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function isJSONContent(headers) {
/**
* Return a new object with all field names of the headers lower-cased.
*
* Duplicates throw an error.
* Duplicates take the last value provided.
*/
function headersFieldNamesToLowerCase(headers) {
if (!isPlainObject(headers)) {
Expand All @@ -202,12 +202,12 @@ function headersFieldNamesToLowerCase(headers) {
const lowerCaseHeaders = {}
Object.entries(headers).forEach(([fieldName, fieldValue]) => {
const key = fieldName.toLowerCase()
if (lowerCaseHeaders[key] !== undefined) {
throw Error(
`Failed to convert header keys to lower case due to field name conflict: ${key}`
)
const regex = new RegExp(`^${fieldValue}$`, 'i');
if(fieldValue instanceof RegExp && lowerCaseHeaders[key] === undefined) {
lowerCaseHeaders[key] = fieldValue
} else if (!regex.test(lowerCaseHeaders[key])) {
lowerCaseHeaders[key] = fieldValue
}
lowerCaseHeaders[key] = fieldValue
})

return lowerCaseHeaders
Expand Down
22 changes: 13 additions & 9 deletions tests/test_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,19 @@ describe('`headersFieldNamesToLowerCase()`', () => {
expect(input).to.deep.equal(inputClone) // assert the input is not mutated
})

it('throws on conflicting keys', () => {
expect(() =>
common.headersFieldNamesToLowerCase({
HoSt: 'example.test',
HOST: 'example.test',
})
).to.throw(
'Failed to convert header keys to lower case due to field name conflict: host'
)
it('handles duplicates of conflicting keys using last value provided', () => {
const results = common.headersFieldNamesToLowerCase({
hOsT: 'example.test',
HoSt: 'example.test',
HOST: 'final.example.test',
hosts: 'final.example.test'
})
const expected = {
host: 'final.example.test',
hosts: 'final.example.test'
}

expect(results).to.deep.equal(expected)
})
})

Expand Down