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

Lowercasing request headers #34

Merged
merged 2 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import url from 'url'
import {
assign,
setprototypeof,
appendAdditionalProps
appendAdditionalProps,
convertKeysToLowerCase
} from './util'
import DEFAULT_APP from './app'

Expand Down Expand Up @@ -74,7 +75,7 @@ export default class Request implements IRequest {
this.socket = opts.socket
this.app = opts.app
this.res = opts.res
this.headers = opts.headers
this.headers = opts.headers ? convertKeysToLowerCase(opts.headers) : {}
this.body = opts.body
this.params = opts.params
this.connection = opts.connection
Expand Down
10 changes: 10 additions & 0 deletions src/main/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ export function appendAdditionalProps (target: Object, props: Object): void {
export function concat (...strings: Array<?string>): string {
return strings.join('')
}

export function convertKeysToLowerCase (map: Object): Object {
const newMap = {}

for (const key in map) {
newMap[key.toLowerCase()] = map[key]
}

return newMap
}
7 changes: 7 additions & 0 deletions src/test/js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ describe('request', () => {
expect(req.header('foo')).toBe('bar')
expect(req.get('BAZ')).toBe('qux')
})

it('lowercases incoming headers', () => {
const req = new Request({ headers: { 'BaZ-Header': 'qux' } })

expect(req.get('Baz-Header')).toBe('qux')
expect(req.headers['baz-header']).toBe('qux')
})
})

describe('ip', () => {
Expand Down
21 changes: 21 additions & 0 deletions src/test/js/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { convertKeysToLowerCase } from '../../main/js/util'

describe('util', () => {
describe('convertKeysToLowerCase', () => {
it('converts object keys to lower case keys', () => {
const map = {
prop: 'value',
standard_Notation: 42,
'BaZ-Header': 'qux'
}

const lowerCasedMap = convertKeysToLowerCase(map)

expect(map.standard_Notation).toEqual(42, 'original map is not modified')

expect(lowerCasedMap.prop).toEqual('value')
expect(lowerCasedMap.standard_notation).toEqual(42)
expect(lowerCasedMap['baz-header']).toEqual('qux')
})
})
})