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 20
20
'use strict'
21
21
22
22
const debug = require ( 'debug' ) ( 'elasticsearch' )
23
+ const os = require ( 'os' )
23
24
const once = require ( 'once' )
24
25
const { createGzip } = require ( 'zlib' )
25
26
const intoStream = require ( 'into-stream' )
@@ -34,6 +35,9 @@ const {
34
35
35
36
const noop = ( ) => { }
36
37
38
+ const clientVersion = require ( '../package.json' ) . version
39
+ const userAgent = `elasticsearch-js/${ clientVersion } (${ os . platform ( ) } ${ os . release ( ) } -${ os . arch ( ) } ; Node.js ${ process . version } )`
40
+
37
41
class Transport {
38
42
constructor ( opts = { } ) {
39
43
if ( typeof opts . compression === 'string' && opts . compression !== 'gzip' ) {
@@ -46,7 +50,7 @@ class Transport {
46
50
this . requestTimeout = toMs ( opts . requestTimeout )
47
51
this . suggestCompression = opts . suggestCompression === true
48
52
this . compression = opts . compression || false
49
- this . headers = opts . headers || { }
53
+ this . headers = Object . assign ( { } , { 'User-Agent' : userAgent } , opts . headers )
50
54
this . sniffInterval = opts . sniffInterval
51
55
this . sniffOnConnectionFault = opts . sniffOnConnectionFault
52
56
this . sniffEndpoint = opts . sniffEndpoint
Original file line number Diff line number Diff line change 22
22
const { test } = require ( 'tap' )
23
23
const { URL } = require ( 'url' )
24
24
const { createGunzip } = require ( 'zlib' )
25
+ const os = require ( 'os' )
25
26
const intoStream = require ( 'into-stream' )
26
27
const {
27
28
buildServer,
@@ -2111,6 +2112,43 @@ test('Should accept custom querystring in the optons object', t => {
2111
2112
t . end ( )
2112
2113
} )
2113
2114
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
+
2114
2152
test ( 'Should pass request params and options to generateRequestId' , t => {
2115
2153
t . plan ( 3 )
2116
2154
You can’t perform that action at this time.
0 commit comments