Added User-Agent header (#807)
* Added User-Agent header * Updated test * Updated user-agent format * Updated test
This commit is contained in:
committed by
GitHub
parent
215cc036c3
commit
802f7902a4
@ -20,6 +20,7 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const debug = require('debug')('elasticsearch')
|
const debug = require('debug')('elasticsearch')
|
||||||
|
const os = require('os')
|
||||||
const once = require('once')
|
const once = require('once')
|
||||||
const { createGzip } = require('zlib')
|
const { createGzip } = require('zlib')
|
||||||
const intoStream = require('into-stream')
|
const intoStream = require('into-stream')
|
||||||
@ -34,6 +35,9 @@ const {
|
|||||||
|
|
||||||
const noop = () => {}
|
const noop = () => {}
|
||||||
|
|
||||||
|
const clientVersion = require('../package.json').version
|
||||||
|
const userAgent = `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${process.version})`
|
||||||
|
|
||||||
class Transport {
|
class Transport {
|
||||||
constructor (opts = {}) {
|
constructor (opts = {}) {
|
||||||
if (typeof opts.compression === 'string' && opts.compression !== 'gzip') {
|
if (typeof opts.compression === 'string' && opts.compression !== 'gzip') {
|
||||||
@ -46,7 +50,7 @@ class Transport {
|
|||||||
this.requestTimeout = toMs(opts.requestTimeout)
|
this.requestTimeout = toMs(opts.requestTimeout)
|
||||||
this.suggestCompression = opts.suggestCompression === true
|
this.suggestCompression = opts.suggestCompression === true
|
||||||
this.compression = opts.compression || false
|
this.compression = opts.compression || false
|
||||||
this.headers = opts.headers || {}
|
this.headers = Object.assign({}, { 'User-Agent': userAgent }, opts.headers)
|
||||||
this.sniffInterval = opts.sniffInterval
|
this.sniffInterval = opts.sniffInterval
|
||||||
this.sniffOnConnectionFault = opts.sniffOnConnectionFault
|
this.sniffOnConnectionFault = opts.sniffOnConnectionFault
|
||||||
this.sniffEndpoint = opts.sniffEndpoint
|
this.sniffEndpoint = opts.sniffEndpoint
|
||||||
|
|||||||
@ -22,6 +22,7 @@
|
|||||||
const { test } = require('tap')
|
const { test } = require('tap')
|
||||||
const { URL } = require('url')
|
const { URL } = require('url')
|
||||||
const { createGunzip } = require('zlib')
|
const { createGunzip } = require('zlib')
|
||||||
|
const os = require('os')
|
||||||
const intoStream = require('into-stream')
|
const intoStream = require('into-stream')
|
||||||
const {
|
const {
|
||||||
buildServer,
|
buildServer,
|
||||||
@ -2111,6 +2112,43 @@ test('Should accept custom querystring in the optons object', t => {
|
|||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('Should add an User-Agent header', t => {
|
||||||
|
t.plan(2)
|
||||||
|
const clientVersion = require('../../package.json').version
|
||||||
|
const userAgent = `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${process.version})`
|
||||||
|
|
||||||
|
function handler (req, res) {
|
||||||
|
t.match(req.headers, {
|
||||||
|
'user-agent': userAgent
|
||||||
|
})
|
||||||
|
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||||
|
res.end(JSON.stringify({ hello: 'world' }))
|
||||||
|
}
|
||||||
|
|
||||||
|
buildServer(handler, ({ port }, server) => {
|
||||||
|
const pool = new ConnectionPool({ Connection })
|
||||||
|
pool.addConnection(`http://localhost:${port}`)
|
||||||
|
|
||||||
|
const transport = new Transport({
|
||||||
|
emit: () => {},
|
||||||
|
connectionPool: pool,
|
||||||
|
serializer: new Serializer(),
|
||||||
|
maxRetries: 3,
|
||||||
|
requestTimeout: 30000,
|
||||||
|
sniffInterval: false,
|
||||||
|
sniffOnStart: false
|
||||||
|
})
|
||||||
|
|
||||||
|
transport.request({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/hello'
|
||||||
|
}, (err, { body }) => {
|
||||||
|
t.error(err)
|
||||||
|
server.stop()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
test('Should pass request params and options to generateRequestId', t => {
|
test('Should pass request params and options to generateRequestId', t => {
|
||||||
t.plan(3)
|
t.plan(3)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user