Skip to content

Added User-Agent header #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'use strict'

const debug = require('debug')('elasticsearch')
const os = require('os')
const once = require('once')
const { createGzip } = require('zlib')
const intoStream = require('into-stream')
Expand All @@ -34,6 +35,9 @@ const {

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 {
constructor (opts = {}) {
if (typeof opts.compression === 'string' && opts.compression !== 'gzip') {
Expand All @@ -46,7 +50,7 @@ class Transport {
this.requestTimeout = toMs(opts.requestTimeout)
this.suggestCompression = opts.suggestCompression === true
this.compression = opts.compression || false
this.headers = opts.headers || {}
this.headers = Object.assign({}, { 'User-Agent': userAgent }, opts.headers)
this.sniffInterval = opts.sniffInterval
this.sniffOnConnectionFault = opts.sniffOnConnectionFault
this.sniffEndpoint = opts.sniffEndpoint
Expand Down
38 changes: 38 additions & 0 deletions test/unit/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
const { test } = require('tap')
const { URL } = require('url')
const { createGunzip } = require('zlib')
const os = require('os')
const intoStream = require('into-stream')
const {
buildServer,
Expand Down Expand Up @@ -2111,6 +2112,43 @@ test('Should accept custom querystring in the optons object', t => {
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 => {
t.plan(3)

Expand Down