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

dup-keys-non-strings #3166

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 12 additions & 0 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ function processHeader (request, key, val) {
return
}

if (typeof key !== 'string') {
throw new InvalidArgumentError('invalid header key, needs to be a "String"')
}

let headerName = headerNameLowerCasedRecord[key]

if (headerName === undefined) {
Expand All @@ -331,6 +335,14 @@ function processHeader (request, key, val) {
throw new InvalidArgumentError('invalid header key')
}
}
const existingHeaderKey = request.headers.find((header, i) => {
const isValArray = Array.isArray(val)
return header === key && i % 2 === 0 && !isValArray
})

if (existingHeaderKey) {
throw new InvalidArgumentError(`duplicated header key: ${key}`)
}

if (Array.isArray(val)) {
const arr = []
Expand Down
103 changes: 103 additions & 0 deletions test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,107 @@ describe('Should include headers from iterable objects', scope => {
t.strictEqual(err.message, 'headers must be in key-value pair format')
})
})

test('Should throw error if headers are duplicates', async t => {
t = tspl(t, { plan: 2 })

const server = createServer((req, res) => {
res.end('hello')
})

const headers = {
* [Symbol.iterator] () {
yield ['hello', 'world']
yield ['hello', 'Pluto']
}
}

after(() => server.close())

await new Promise((resolve, reject) => {
server.listen(0, (err) => {
if (err != null) reject(err)
else resolve()
})
})

await request({
method: 'GET',
origin: `http://localhost:${server.address().port}`,
reset: true,
headers
}).then((res) => {
t.fail('Should not succeed')
}).catch((err) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.strictEqual(err.message, 'duplicated header key: hello')
})
})
test('Should NOT throw error if headers are duplicates AND val is a list', async t => {
t = tspl(t, { plan: 1 })

const server = createServer((req, res) => {
res.end('hello')
})

const headers = {
* [Symbol.iterator] () {
yield ['hello', 'world']
yield ['hello', ['world', 'Pluto']]
}
}

after(() => server.close())

await new Promise((resolve, reject) => {
server.listen(0, (err) => {
if (err != null) reject(err)
else resolve()
})
})

await request({
method: 'GET',
origin: `http://localhost:${server.address().port}`,
reset: true,
headers
}).then((res) => {
t.ok(res, 'Should succeed')
})
})
test('Should throw error if headers are NOT strings', async t => {
t = tspl(t, { plan: 2 })

const server = createServer((req, res) => {
res.end('hello')
})

const headers = {
* [Symbol.iterator] () {
yield [{ hello: 'world' }, 'world']
yield [0, 'Pluto']
}
}

after(() => server.close())

await new Promise((resolve, reject) => {
server.listen(0, (err) => {
if (err != null) reject(err)
else resolve()
})
})

await request({
method: 'GET',
origin: `http://localhost:${server.address().port}`,
reset: true,
headers
}).then((res) => {
t.fail('Should not succeed')
}).catch((err) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.strictEqual(err.message, 'invalid header key, needs to be a "String"')
})
})
})