55import urllib3
66import warnings
77from requests .auth import AuthBase
8+ from platform import python_version
89
910from elasticsearch .exceptions import (
1011 TransportError ,
1415)
1516from elasticsearch .connection import RequestsHttpConnection , Urllib3HttpConnection
1617from elasticsearch .exceptions import ImproperlyConfigured
18+ from elasticsearch import __versionstr__
1719from .test_cases import TestCase , SkipTest
1820
19-
2021class TestUrllib3Connection (TestCase ):
2122 def test_ssl_context (self ):
2223 try :
@@ -48,14 +49,22 @@ def test_http_compression(self):
4849 self .assertTrue (con .http_compress )
4950 self .assertEquals (con .headers ["content-encoding" ], "gzip" )
5051
52+ def test_default_user_agent (self ):
53+ con = Urllib3HttpConnection ()
54+ self .assertEquals (con ._get_default_user_agent (), "elasticsearch-py/%s (Python %s)" % (__versionstr__ , python_version ()))
55+
5156 def test_timeout_set (self ):
5257 con = Urllib3HttpConnection (timeout = 42 )
5358 self .assertEquals (42 , con .timeout )
5459
5560 def test_keep_alive_is_on_by_default (self ):
5661 con = Urllib3HttpConnection ()
5762 self .assertEquals (
58- {"connection" : "keep-alive" , "content-type" : "application/json" },
63+ {
64+ "connection" : "keep-alive" ,
65+ "content-type" : "application/json" ,
66+ "user-agent" : con ._get_default_user_agent (),
67+ },
5968 con .headers ,
6069 )
6170
@@ -66,6 +75,7 @@ def test_http_auth(self):
6675 "authorization" : "Basic dXNlcm5hbWU6c2VjcmV0" ,
6776 "connection" : "keep-alive" ,
6877 "content-type" : "application/json" ,
78+ "user-agent" : con ._get_default_user_agent (),
6979 },
7080 con .headers ,
7181 )
@@ -77,6 +87,7 @@ def test_http_auth_tuple(self):
7787 "authorization" : "Basic dXNlcm5hbWU6c2VjcmV0" ,
7888 "content-type" : "application/json" ,
7989 "connection" : "keep-alive" ,
90+ "user-agent" : con ._get_default_user_agent (),
8091 },
8192 con .headers ,
8293 )
@@ -88,6 +99,7 @@ def test_http_auth_list(self):
8899 "authorization" : "Basic dXNlcm5hbWU6c2VjcmV0" ,
89100 "content-type" : "application/json" ,
90101 "connection" : "keep-alive" ,
102+ "user-agent" : con ._get_default_user_agent (),
91103 },
92104 con .headers ,
93105 )
@@ -213,6 +225,21 @@ def test_merge_headers(self):
213225 self .assertEquals (req .headers ["h2" ], "v2p" )
214226 self .assertEquals (req .headers ["h3" ], "v3" )
215227
228+ def test_default_headers (self ):
229+ con = self ._get_mock_connection ()
230+ req = self ._get_request (con , "GET" , "/" )
231+ self .assertEquals (req .headers ["content-type" ], "application/json" )
232+ self .assertEquals (req .headers ["user-agent" ], con ._get_default_user_agent ())
233+
234+ def test_custom_headers (self ):
235+ con = self ._get_mock_connection ()
236+ req = self ._get_request (con , "GET" , "/" , headers = {
237+ "content-type" : "application/x-ndjson" ,
238+ "user-agent" : "custom-agent/1.2.3" ,
239+ })
240+ self .assertEquals (req .headers ["content-type" ], "application/x-ndjson" )
241+ self .assertEquals (req .headers ["user-agent" ], "custom-agent/1.2.3" )
242+
216243 def test_http_auth (self ):
217244 con = RequestsHttpConnection (http_auth = "username:secret" )
218245 self .assertEquals (("username" , "secret" ), con .session .auth )
0 commit comments