Skip to content

Commit 802f790

Browse files
authored
Added User-Agent header (#807)
* Added User-Agent header * Updated test * Updated user-agent format * Updated test
1 parent 215cc03 commit 802f790

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/Transport.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'use strict'
2121

2222
const debug = require('debug')('elasticsearch')
23+
const os = require('os')
2324
const once = require('once')
2425
const { createGzip } = require('zlib')
2526
const intoStream = require('into-stream')
@@ -34,6 +35,9 @@ const {
3435

3536
const noop = () => {}
3637

38+
const clientVersion = require('../package.json').version
39+
const userAgent = `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${process.version})`
40+
3741
class Transport {
3842
constructor (opts = {}) {
3943
if (typeof opts.compression === 'string' && opts.compression !== 'gzip') {
@@ -46,7 +50,7 @@ class Transport {
4650
this.requestTimeout = toMs(opts.requestTimeout)
4751
this.suggestCompression = opts.suggestCompression === true
4852
this.compression = opts.compression || false
49-
this.headers = opts.headers || {}
53+
this.headers = Object.assign({}, { 'User-Agent': userAgent }, opts.headers)
5054
this.sniffInterval = opts.sniffInterval
5155
this.sniffOnConnectionFault = opts.sniffOnConnectionFault
5256
this.sniffEndpoint = opts.sniffEndpoint

test/unit/transport.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
const { test } = require('tap')
2323
const { URL } = require('url')
2424
const { createGunzip } = require('zlib')
25+
const os = require('os')
2526
const intoStream = require('into-stream')
2627
const {
2728
buildServer,
@@ -2111,6 +2112,43 @@ test('Should accept custom querystring in the optons object', t => {
21112112
t.end()
21122113
})
21132114

2115+
test('Should add an User-Agent header', t => {
2116+
t.plan(2)
2117+
const clientVersion = require('../../package.json').version
2118+
const userAgent = `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${process.version})`
2119+
2120+
function handler (req, res) {
2121+
t.match(req.headers, {
2122+
'user-agent': userAgent
2123+
})
2124+
res.setHeader('Content-Type', 'application/json;utf=8')
2125+
res.end(JSON.stringify({ hello: 'world' }))
2126+
}
2127+
2128+
buildServer(handler, ({ port }, server) => {
2129+
const pool = new ConnectionPool({ Connection })
2130+
pool.addConnection(`http://localhost:${port}`)
2131+
2132+
const transport = new Transport({
2133+
emit: () => {},
2134+
connectionPool: pool,
2135+
serializer: new Serializer(),
2136+
maxRetries: 3,
2137+
requestTimeout: 30000,
2138+
sniffInterval: false,
2139+
sniffOnStart: false
2140+
})
2141+
2142+
transport.request({
2143+
method: 'GET',
2144+
path: '/hello'
2145+
}, (err, { body }) => {
2146+
t.error(err)
2147+
server.stop()
2148+
})
2149+
})
2150+
})
2151+
21142152
test('Should pass request params and options to generateRequestId', t => {
21152153
t.plan(3)
21162154

0 commit comments

Comments
 (0)