Added x-elastic-client-meta header (#1373)

This commit is contained in:
Tomas Della Vedova
2020-12-16 10:42:26 +01:00
committed by GitHub
parent 4cace4c234
commit 61eee69424
8 changed files with 115 additions and 8 deletions

View File

@ -26,6 +26,8 @@ const intoStream = require('into-stream')
const { Client, ConnectionPool, Transport, Connection, errors } = require('../../index')
const { CloudConnectionPool } = require('../../lib/pool')
const { buildServer } = require('../utils')
const clientVersion = require('../../package.json').version
const nodeVersion = process.versions.node
test('Configure host', t => {
t.test('Single string', t => {
@ -1300,3 +1302,62 @@ test('Content length too big (string)', t => {
t.strictEqual(result.meta.attempts, 0)
})
})
test('Meta header enabled', t => {
t.plan(2)
class MockConnection extends Connection {
request (params, callback) {
t.match(params.headers, { 'x-elastic-client-meta': `es=${clientVersion},js=${nodeVersion},t=${clientVersion},hc=${nodeVersion}` })
const stream = intoStream(JSON.stringify({ hello: 'world' }))
stream.statusCode = 200
stream.headers = {
'content-type': 'application/json;utf=8',
'content-length': '17',
connection: 'keep-alive',
date: new Date().toISOString()
}
process.nextTick(callback, null, stream)
return { abort () {} }
}
}
const client = new Client({
node: 'http://localhost:9200',
Connection: MockConnection
})
client.info((err, result) => {
t.error(err)
})
})
test('Meta header disabled', t => {
t.plan(2)
class MockConnection extends Connection {
request (params, callback) {
t.notMatch(params.headers, { 'x-elastic-client-meta': `es=${clientVersion},js=${nodeVersion},t=${clientVersion},hc=${nodeVersion}` })
const stream = intoStream(JSON.stringify({ hello: 'world' }))
stream.statusCode = 200
stream.headers = {
'content-type': 'application/json;utf=8',
'content-length': '17',
connection: 'keep-alive',
date: new Date().toISOString()
}
process.nextTick(callback, null, stream)
return { abort () {} }
}
}
const client = new Client({
node: 'http://localhost:9200',
Connection: MockConnection,
enableMetaHeader: false
})
client.info((err, result) => {
t.error(err)
})
})