Skip to content

Commit

Permalink
compatible API
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Dec 6, 2023
1 parent 8abb9c9 commit 8324d21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class RequestHandler extends AsyncResource {

removeSignal(this)

util.parseHeaders(trailers, false, this.trailers)
util.parseHeaders(trailers, this.trailers)

res.push(null)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ function headerNameToStringUnsafe (value) {

/**
* @param {Record<any, any> | (Buffer | string | (Buffer | string)[])[]} headers
* @param {boolean} [allowUnsafe]
* @param {Record<any, any>} [obj]
* @param {boolean} [allowUnsafe]
* @returns
*/
function parseHeaders (headers, allowUnsafe, obj) {
function parseHeaders (headers, obj, allowUnsafe) {
// For H2 support
if (!Array.isArray(headers)) return headers

Expand Down
25 changes: 13 additions & 12 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,23 @@ test('validateHandler', (t) => {

test('parseHeaders', (t) => {
t.plan(6)
t.same(util.parseHeaders(['key', 'value'], false), { key: 'value' })
t.same(util.parseHeaders([Buffer.from('key'), Buffer.from('value')], false), { key: 'value' })
t.same(util.parseHeaders(['Key', 'Value'], false), { key: 'Value' })
t.same(util.parseHeaders(['Key', 'value', 'key', 'Value'], false), { key: ['value', 'Value'] })
t.same(util.parseHeaders(['key', ['value1', 'value2', 'value3']], false), { key: ['value1', 'value2', 'value3'] })
t.same(util.parseHeaders([Buffer.from('key'), [Buffer.from('value1'), Buffer.from('value2'), Buffer.from('value3')]], false), { key: ['value1', 'value2', 'value3'] })
t.same(util.parseHeaders(['key', 'value']), { key: 'value' })
t.same(util.parseHeaders([Buffer.from('key'), Buffer.from('value')]), { key: 'value' })
t.same(util.parseHeaders(['Key', 'Value']), { key: 'Value' })
t.same(util.parseHeaders(['Key', 'value', 'key', 'Value']), { key: ['value', 'Value'] })
t.same(util.parseHeaders(['key', ['value1', 'value2', 'value3']]), { key: ['value1', 'value2', 'value3'] })
t.same(util.parseHeaders([Buffer.from('key'), [Buffer.from('value1'), Buffer.from('value2'), Buffer.from('value3')]]), { key: ['value1', 'value2', 'value3'] })
})

test('parseHeaders allowUnsafe', (t) => {
t.plan(6)
t.same(util.parseHeaders(['key', 'value'], true), { key: 'value' })
t.same(util.parseHeaders([Buffer.from('key'), Buffer.from('value')], true), { key: 'value' })
t.same(util.parseHeaders(['Key', 'Value'], true), { key: 'Value' })
t.same(util.parseHeaders(['Key', 'value', 'key', 'Value'], true), { key: ['value', 'Value'] })
t.same(util.parseHeaders(['key', ['value1', 'value2', 'value3']], true), { key: ['value1', 'value2', 'value3'] })
t.same(util.parseHeaders([Buffer.from('key'), [Buffer.from('value1'), Buffer.from('value2'), Buffer.from('value3')]], true), { key: ['value1', 'value2', 'value3'] })
const parseHeaders = (a, b) => util.parseHeaders(a, b, true)
t.same(parseHeaders(['key', 'value']), { key: 'value' })
t.same(parseHeaders([Buffer.from('key'), Buffer.from('value')]), { key: 'value' })
t.same(parseHeaders(['Key', 'Value']), { key: 'Value' })
t.same(parseHeaders(['Key', 'value', 'key', 'Value']), { key: ['value', 'Value'] })
t.same(parseHeaders(['key', ['value1', 'value2', 'value3']]), { key: ['value1', 'value2', 'value3'] })
t.same(parseHeaders([Buffer.from('key'), [Buffer.from('value1'), Buffer.from('value2'), Buffer.from('value3')]]), { key: ['value1', 'value2', 'value3'] })
})

test('parseRawHeaders', (t) => {
Expand Down

0 comments on commit 8324d21

Please sign in to comment.