File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 2020'use strict'
2121
2222const debug = require ( 'debug' ) ( 'elasticsearch' )
23+ const os = require ( 'os' )
2324const once = require ( 'once' )
2425const { createGzip } = require ( 'zlib' )
2526const intoStream = require ( 'into-stream' )
@@ -34,6 +35,9 @@ const {
3435
3536const 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+
3741class 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
Original file line number Diff line number Diff line change 2222const { test } = require ( 'tap' )
2323const { URL } = require ( 'url' )
2424const { createGunzip } = require ( 'zlib' )
25+ const os = require ( 'os' )
2526const intoStream = require ( 'into-stream' )
2627const {
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+
21142152test ( 'Should pass request params and options to generateRequestId' , t => {
21152153 t . plan ( 3 )
21162154
You can’t perform that action at this time.
0 commit comments