Force lowercase in all headers (#1187)

This commit is contained in:
Tomas Della Vedova
2020-05-14 16:10:18 +02:00
committed by GitHub
parent 22de806b67
commit 191b8fb9fb
4 changed files with 105 additions and 22 deletions

View File

@ -1029,3 +1029,41 @@ test('Opaque Id support', t => {
t.end()
})
test('Correctly handles the same header cased differently', t => {
t.plan(4)
function handler (req, res) {
t.strictEqual(req.headers['authorization'], 'Basic foobar')
t.strictEqual(req.headers['foo'], 'baz')
res.setHeader('Content-Type', 'application/json;utf=8')
res.end(JSON.stringify({ hello: 'world' }))
}
buildServer(handler, ({ port }, server) => {
const client = new Client({
node: `http://localhost:${port}`,
auth: {
username: 'hello',
password: 'world'
},
headers: {
Authorization: 'Basic foobar',
Foo: 'bar'
}
})
client.search({
index: 'test',
q: 'foo:bar'
}, {
headers: {
foo: 'baz'
}
}, (err, { body }) => {
t.error(err)
t.deepEqual(body, { hello: 'world' })
server.stop()
})
})
})

View File

@ -25,7 +25,7 @@ test('bulk index', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
const [action, payload] = params.body.split('\n')
t.deepEqual(JSON.parse(action), { index: { _index: 'test' } })
t.deepEqual(JSON.parse(payload), dataset[count++])
@ -67,7 +67,7 @@ test('bulk index', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
const [action, payload] = params.body.split('\n')
t.deepEqual(JSON.parse(action), { index: { _index: 'test' } })
t.deepEqual(JSON.parse(payload), dataset[count++])
@ -108,7 +108,7 @@ test('bulk index', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
t.strictEqual(params.body.split('\n').filter(Boolean).length, 6)
return { body: { errors: false, items: new Array(3).fill({}) } }
}
@ -152,7 +152,7 @@ test('bulk index', t => {
return { body: { acknowledged: true } }
} else {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
const [action, payload] = params.body.split('\n')
t.deepEqual(JSON.parse(action), { index: { _index: 'test' } })
t.deepEqual(JSON.parse(payload), dataset[count++])
@ -193,7 +193,7 @@ test('bulk index', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
const [action, payload] = params.body.split('\n')
t.deepEqual(JSON.parse(action), { index: { _index: 'test', _id: count } })
t.deepEqual(JSON.parse(payload), dataset[count++])
@ -607,7 +607,7 @@ test('bulk index', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
const [action, payload] = params.body.split('\n')
t.deepEqual(JSON.parse(action), { index: { _index: 'test', _id: count } })
t.deepEqual(JSON.parse(payload), dataset[count++])
@ -659,7 +659,7 @@ test('bulk index', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
const [action, payload] = params.body.split('\n')
t.deepEqual(JSON.parse(action), { index: { _index: 'test' } })
t.deepEqual(JSON.parse(payload), dataset[count++])
@ -715,7 +715,7 @@ test('bulk create', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
const [action, payload] = params.body.split('\n')
t.deepEqual(JSON.parse(action), { create: { _index: 'test', _id: count } })
t.deepEqual(JSON.parse(payload), dataset[count++])
@ -764,7 +764,7 @@ test('bulk update', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
const [action, payload] = params.body.split('\n')
t.deepEqual(JSON.parse(action), { update: { _index: 'test', _id: count } })
t.deepEqual(JSON.parse(payload), { doc: dataset[count++], doc_as_upsert: true })
@ -815,7 +815,7 @@ test('bulk delete', t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.strictEqual(params.path, '/_bulk')
t.match(params.headers, { 'Content-Type': 'application/x-ndjson' })
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
t.deepEqual(JSON.parse(params.body), { delete: { _index: 'test', _id: count++ } })
return { body: { errors: false, items: [{}] } }
}

View File

@ -2416,3 +2416,32 @@ test('Secure json parsing', t => {
t.end()
})
test('Lowercase headers utilty', t => {
t.plan(4)
const { lowerCaseHeaders } = Transport.internals
t.deepEqual(lowerCaseHeaders({
Foo: 'bar',
Faz: 'baz',
'X-Hello': 'world'
}), {
foo: 'bar',
faz: 'baz',
'x-hello': 'world'
})
t.deepEqual(lowerCaseHeaders({
Foo: 'bar',
faz: 'baz',
'X-hello': 'world'
}), {
foo: 'bar',
faz: 'baz',
'x-hello': 'world'
})
t.strictEqual(lowerCaseHeaders(null), null)
t.strictEqual(lowerCaseHeaders(undefined), undefined)
})