2017-08-16 21:39:32 -06:00
|
|
|
import sys
|
2013-05-02 15:36:53 +02:00
|
|
|
import re
|
2017-08-16 21:39:32 -06:00
|
|
|
import ssl
|
2013-05-02 00:34:46 +02:00
|
|
|
from mock import Mock, patch
|
2013-09-24 23:07:14 +02:00
|
|
|
import urllib3
|
2015-01-29 23:46:01 +01:00
|
|
|
import warnings
|
2015-03-26 14:17:10 +01:00
|
|
|
from requests.auth import AuthBase
|
2013-05-01 21:55:44 +02:00
|
|
|
|
2013-11-14 01:08:19 +01:00
|
|
|
from elasticsearch.exceptions import TransportError, ConflictError, RequestError, NotFoundError
|
2013-09-25 21:31:10 +02:00
|
|
|
from elasticsearch.connection import RequestsHttpConnection, \
|
2015-10-07 17:47:05 +02:00
|
|
|
Urllib3HttpConnection
|
2017-08-16 21:39:32 -06:00
|
|
|
from elasticsearch.exceptions import ImproperlyConfigured
|
|
|
|
|
from elasticsearch.connection.http_urllib3 import create_ssl_context
|
|
|
|
|
from .test_cases import TestCase, SkipTest
|
2013-09-25 21:31:10 +02:00
|
|
|
|
2013-08-28 19:11:28 +02:00
|
|
|
|
2013-09-24 22:55:04 +02:00
|
|
|
class TestUrllib3Connection(TestCase):
|
2014-01-23 08:48:42 +01:00
|
|
|
def test_timeout_set(self):
|
|
|
|
|
con = Urllib3HttpConnection(timeout=42)
|
|
|
|
|
self.assertEquals(42, con.timeout)
|
|
|
|
|
|
2015-06-22 20:39:58 +02:00
|
|
|
def test_keep_alive_is_on_by_default(self):
|
|
|
|
|
con = Urllib3HttpConnection()
|
2017-02-08 20:47:57 +01:00
|
|
|
self.assertEquals({'connection': 'keep-alive',
|
|
|
|
|
'content-type': 'application/json'}, con.headers)
|
2015-06-22 20:39:58 +02:00
|
|
|
|
2013-09-24 22:55:04 +02:00
|
|
|
def test_http_auth(self):
|
|
|
|
|
con = Urllib3HttpConnection(http_auth='username:secret')
|
2017-02-08 20:47:57 +01:00
|
|
|
self.assertEquals({
|
|
|
|
|
'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0',
|
|
|
|
|
'connection': 'keep-alive',
|
|
|
|
|
'content-type': 'application/json'
|
|
|
|
|
}, con.headers)
|
2013-09-24 22:55:04 +02:00
|
|
|
|
|
|
|
|
def test_http_auth_tuple(self):
|
|
|
|
|
con = Urllib3HttpConnection(http_auth=('username', 'secret'))
|
2015-06-22 20:39:58 +02:00
|
|
|
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0',
|
2017-02-08 20:47:57 +01:00
|
|
|
'content-type': 'application/json',
|
2015-06-22 20:39:58 +02:00
|
|
|
'connection': 'keep-alive'}, con.headers)
|
2013-09-24 22:55:04 +02:00
|
|
|
|
2013-10-01 23:41:00 +02:00
|
|
|
def test_http_auth_list(self):
|
|
|
|
|
con = Urllib3HttpConnection(http_auth=['username', 'secret'])
|
2015-06-22 20:39:58 +02:00
|
|
|
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0',
|
2017-02-08 20:47:57 +01:00
|
|
|
'content-type': 'application/json',
|
2015-06-22 20:39:58 +02:00
|
|
|
'connection': 'keep-alive'}, con.headers)
|
2013-10-01 23:41:00 +02:00
|
|
|
|
2016-10-17 14:16:56 +02:00
|
|
|
def test_uses_https_if_verify_certs_is_off(self):
|
2017-08-16 21:39:32 -06:00
|
|
|
if (
|
|
|
|
|
sys.version_info >= (3,0) and sys.version_info <= (3,4)
|
|
|
|
|
) or (
|
|
|
|
|
sys.version_info >= (2,6) and sys.version_info <= (2,7)
|
|
|
|
|
):
|
|
|
|
|
raise SkipTest("SSL Context not supported in this version of python")
|
2015-01-29 23:46:01 +01:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2016-10-17 14:16:56 +02:00
|
|
|
con = Urllib3HttpConnection(use_ssl=True, verify_certs=False)
|
2015-01-29 23:46:01 +01:00
|
|
|
self.assertEquals(1, len(w))
|
|
|
|
|
self.assertEquals('Connecting to localhost using SSL with verify_certs=False is insecure.', str(w[0].message))
|
|
|
|
|
|
2013-09-24 23:07:14 +02:00
|
|
|
self.assertIsInstance(con.pool, urllib3.HTTPSConnectionPool)
|
|
|
|
|
|
|
|
|
|
def test_doesnt_use_https_if_not_specified(self):
|
|
|
|
|
con = Urllib3HttpConnection()
|
|
|
|
|
self.assertIsInstance(con.pool, urllib3.HTTPConnectionPool)
|
2013-09-24 22:55:04 +02:00
|
|
|
|
2017-08-16 21:39:32 -06:00
|
|
|
def test_ssl_context_and_depreicated_values(self):
|
|
|
|
|
try:
|
|
|
|
|
ctx = create_ssl_context()
|
|
|
|
|
except AttributeError:
|
|
|
|
|
raise SkipTest("SSL Context not supported in this version of python")
|
|
|
|
|
self.assertRaises(ImproperlyConfigured, Urllib3HttpConnection, ssl_context=ctx, use_ssl=True)
|
|
|
|
|
self.assertRaises(ImproperlyConfigured, Urllib3HttpConnection, ssl_context=ctx, verify_certs=True)
|
|
|
|
|
self.assertRaises(ImproperlyConfigured, Urllib3HttpConnection, ssl_context=ctx, ca_certs="/some/path/to/cert.crt")
|
|
|
|
|
self.assertRaises(ImproperlyConfigured, Urllib3HttpConnection, ssl_context=ctx, ssl_version=ssl.PROTOCOL_SSLv23)
|
|
|
|
|
|
2013-05-01 21:55:44 +02:00
|
|
|
class TestRequestsConnection(TestCase):
|
2014-02-21 16:53:56 +01:00
|
|
|
def _get_mock_connection(self, connection_params={}, status_code=200, response_body='{}'):
|
2013-05-01 21:55:44 +02:00
|
|
|
con = RequestsHttpConnection(**connection_params)
|
2013-10-02 00:39:19 +02:00
|
|
|
def _dummy_send(*args, **kwargs):
|
|
|
|
|
dummy_response = Mock()
|
|
|
|
|
dummy_response.headers = {}
|
|
|
|
|
dummy_response.status_code = status_code
|
|
|
|
|
dummy_response.text = response_body
|
|
|
|
|
dummy_response.request = args[0]
|
2013-10-06 17:43:45 +02:00
|
|
|
dummy_response.cookies = {}
|
2013-10-02 00:39:19 +02:00
|
|
|
_dummy_send.call_args = (args, kwargs)
|
|
|
|
|
return dummy_response
|
|
|
|
|
con.session.send = _dummy_send
|
2013-05-01 21:55:44 +02:00
|
|
|
return con
|
|
|
|
|
|
|
|
|
|
def _get_request(self, connection, *args, **kwargs):
|
2014-05-09 18:11:50 +02:00
|
|
|
if 'body' in kwargs:
|
|
|
|
|
kwargs['body'] = kwargs['body'].encode('utf-8')
|
|
|
|
|
|
2013-12-13 19:36:21 +01:00
|
|
|
status, headers, data = connection.perform_request(*args, **kwargs)
|
2013-05-01 21:55:44 +02:00
|
|
|
self.assertEquals(200, status)
|
2014-02-21 16:53:56 +01:00
|
|
|
self.assertEquals('{}', data)
|
2013-05-01 21:55:44 +02:00
|
|
|
|
2013-05-24 01:00:29 +02:00
|
|
|
timeout = kwargs.pop('timeout', connection.timeout)
|
2013-05-01 21:55:44 +02:00
|
|
|
args, kwargs = connection.session.send.call_args
|
2013-10-02 00:39:19 +02:00
|
|
|
self.assertEquals(timeout, kwargs['timeout'])
|
2013-05-01 21:55:44 +02:00
|
|
|
self.assertEquals(1, len(args))
|
|
|
|
|
return args[0]
|
|
|
|
|
|
2015-03-26 14:17:10 +01:00
|
|
|
def test_custom_http_auth_is_allowed(self):
|
|
|
|
|
auth = AuthBase()
|
|
|
|
|
c = RequestsHttpConnection(http_auth=auth)
|
|
|
|
|
|
|
|
|
|
self.assertEquals(auth, c.session.auth)
|
|
|
|
|
|
2014-01-23 08:48:42 +01:00
|
|
|
def test_timeout_set(self):
|
|
|
|
|
con = RequestsHttpConnection(timeout=42)
|
|
|
|
|
self.assertEquals(42, con.timeout)
|
|
|
|
|
|
2016-10-17 14:16:56 +02:00
|
|
|
def test_uses_https_if_verify_certs_is_off(self):
|
2015-01-29 23:46:01 +01:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2016-10-17 14:16:56 +02:00
|
|
|
con = self._get_mock_connection({'use_ssl': True, 'url_prefix': 'url', 'verify_certs': False})
|
2015-01-29 23:46:01 +01:00
|
|
|
self.assertEquals(1, len(w))
|
|
|
|
|
self.assertEquals('Connecting to https://localhost:9200/url using SSL with verify_certs=False is insecure.', str(w[0].message))
|
|
|
|
|
|
2013-09-24 23:07:14 +02:00
|
|
|
request = self._get_request(con, 'GET', '/')
|
|
|
|
|
|
|
|
|
|
self.assertEquals('https://localhost:9200/url/', request.url)
|
|
|
|
|
self.assertEquals('GET', request.method)
|
|
|
|
|
self.assertEquals(None, request.body)
|
|
|
|
|
|
2017-08-07 23:25:34 -04:00
|
|
|
def test_merge_headers(self):
|
|
|
|
|
con = self._get_mock_connection(connection_params={'headers': {'h1': 'v1', 'h2': 'v2'}})
|
|
|
|
|
req = self._get_request(con, 'GET', '/', headers={'h2': 'v2p', 'h3': 'v3'})
|
|
|
|
|
self.assertEquals(req.headers['h1'], 'v1')
|
|
|
|
|
self.assertEquals(req.headers['h2'], 'v2p')
|
|
|
|
|
self.assertEquals(req.headers['h3'], 'v3')
|
|
|
|
|
|
2013-09-24 22:55:04 +02:00
|
|
|
def test_http_auth(self):
|
|
|
|
|
con = RequestsHttpConnection(http_auth='username:secret')
|
|
|
|
|
self.assertEquals(('username', 'secret'), con.session.auth)
|
|
|
|
|
|
|
|
|
|
def test_http_auth_tuple(self):
|
|
|
|
|
con = RequestsHttpConnection(http_auth=('username', 'secret'))
|
|
|
|
|
self.assertEquals(('username', 'secret'), con.session.auth)
|
|
|
|
|
|
2013-10-01 23:40:34 +02:00
|
|
|
def test_http_auth_list(self):
|
|
|
|
|
con = RequestsHttpConnection(http_auth=['username', 'secret'])
|
|
|
|
|
self.assertEquals(('username', 'secret'), con.session.auth)
|
|
|
|
|
|
2013-05-02 15:41:22 +02:00
|
|
|
def test_repr(self):
|
|
|
|
|
con = self._get_mock_connection({"host": "elasticsearch.com", "port": 443})
|
|
|
|
|
self.assertEquals('<RequestsHttpConnection: http://elasticsearch.com:443>', repr(con))
|
|
|
|
|
|
2013-11-14 01:08:19 +01:00
|
|
|
def test_conflict_error_is_returned_on_409(self):
|
|
|
|
|
con = self._get_mock_connection(status_code=409)
|
|
|
|
|
self.assertRaises(ConflictError, con.perform_request, 'GET', '/', {}, '')
|
|
|
|
|
|
|
|
|
|
def test_not_found_error_is_returned_on_404(self):
|
|
|
|
|
con = self._get_mock_connection(status_code=404)
|
|
|
|
|
self.assertRaises(NotFoundError, con.perform_request, 'GET', '/', {}, '')
|
|
|
|
|
|
|
|
|
|
def test_request_error_is_returned_on_400(self):
|
|
|
|
|
con = self._get_mock_connection(status_code=400)
|
|
|
|
|
self.assertRaises(RequestError, con.perform_request, 'GET', '/', {}, '')
|
|
|
|
|
|
2016-04-27 13:21:47 +02:00
|
|
|
@patch('elasticsearch.connection.base.logger')
|
|
|
|
|
def test_head_with_404_doesnt_get_logged(self, logger):
|
|
|
|
|
con = self._get_mock_connection(status_code=404)
|
|
|
|
|
self.assertRaises(NotFoundError, con.perform_request, 'HEAD', '/', {}, '')
|
|
|
|
|
self.assertEquals(0, logger.warning.call_count)
|
|
|
|
|
|
2013-08-25 16:39:56 +02:00
|
|
|
@patch('elasticsearch.connection.base.tracer')
|
|
|
|
|
@patch('elasticsearch.connection.base.logger')
|
2013-05-02 00:41:27 +02:00
|
|
|
def test_failed_request_logs_and_traces(self, logger, tracer):
|
|
|
|
|
con = self._get_mock_connection(response_body='{"answer": 42}', status_code=500)
|
2014-05-09 18:11:50 +02:00
|
|
|
self.assertRaises(TransportError, con.perform_request, 'GET', '/', {'param': 42}, '{}'.encode('utf-8'))
|
2013-05-02 00:41:27 +02:00
|
|
|
|
2016-10-19 15:12:44 +02:00
|
|
|
# trace request
|
|
|
|
|
self.assertEquals(1, tracer.info.call_count)
|
|
|
|
|
# trace response
|
|
|
|
|
self.assertEquals(1, tracer.debug.call_count)
|
2013-05-02 00:41:27 +02:00
|
|
|
# log url and duration
|
|
|
|
|
self.assertEquals(1, logger.warning.call_count)
|
2013-05-02 15:36:53 +02:00
|
|
|
self.assertTrue(re.match(
|
|
|
|
|
'^GET http://localhost:9200/\?param=42 \[status:500 request:0.[0-9]{3}s\]',
|
2013-05-02 00:41:27 +02:00
|
|
|
logger.warning.call_args[0][0] % logger.warning.call_args[0][1:]
|
2013-05-02 15:36:53 +02:00
|
|
|
))
|
2013-05-02 00:34:46 +02:00
|
|
|
|
2013-08-25 16:39:56 +02:00
|
|
|
@patch('elasticsearch.connection.base.tracer')
|
|
|
|
|
@patch('elasticsearch.connection.base.logger')
|
2013-05-02 00:34:46 +02:00
|
|
|
def test_success_logs_and_traces(self, logger, tracer):
|
2013-12-11 14:41:42 +01:00
|
|
|
con = self._get_mock_connection(response_body='''{"answer": "that's it!"}''')
|
2014-05-09 18:11:50 +02:00
|
|
|
status, headers, data = con.perform_request('GET', '/', {'param': 42}, '''{"question": "what's that?"}'''.encode('utf-8'))
|
2013-05-02 00:34:46 +02:00
|
|
|
|
|
|
|
|
# trace request
|
|
|
|
|
self.assertEquals(1, tracer.info.call_count)
|
|
|
|
|
self.assertEquals(
|
2017-03-05 11:19:01 -08:00
|
|
|
"""curl -H 'Content-Type: application/json' -XGET 'http://localhost:9200/?pretty¶m=42' -d '{\n "question": "what\\u0027s that?"\n}'""",
|
2013-05-02 00:34:46 +02:00
|
|
|
tracer.info.call_args[0][0] % tracer.info.call_args[0][1:]
|
|
|
|
|
)
|
|
|
|
|
# trace response
|
|
|
|
|
self.assertEquals(1, tracer.debug.call_count)
|
2013-05-02 15:36:53 +02:00
|
|
|
self.assertTrue(re.match(
|
2013-12-11 14:41:42 +01:00
|
|
|
'#\[200\] \(0.[0-9]{3}s\)\n#\{\n# "answer": "that\\\\u0027s it!"\n#\}',
|
2013-05-02 00:34:46 +02:00
|
|
|
tracer.debug.call_args[0][0] % tracer.debug.call_args[0][1:]
|
2013-05-02 15:36:53 +02:00
|
|
|
))
|
2013-05-02 00:34:46 +02:00
|
|
|
|
|
|
|
|
# log url and duration
|
|
|
|
|
self.assertEquals(1, logger.info.call_count)
|
2013-05-02 15:36:53 +02:00
|
|
|
self.assertTrue(re.match(
|
|
|
|
|
'GET http://localhost:9200/\?param=42 \[status:200 request:0.[0-9]{3}s\]',
|
2013-05-02 00:34:46 +02:00
|
|
|
logger.info.call_args[0][0] % logger.info.call_args[0][1:]
|
2013-05-02 15:36:53 +02:00
|
|
|
))
|
2013-05-02 00:34:46 +02:00
|
|
|
# log request body and response
|
|
|
|
|
self.assertEquals(2, logger.debug.call_count)
|
|
|
|
|
req, resp = logger.debug.call_args_list
|
|
|
|
|
self.assertEquals(
|
2013-12-11 14:41:42 +01:00
|
|
|
'> {"question": "what\'s that?"}',
|
2013-05-02 00:34:46 +02:00
|
|
|
req[0][0] % req[0][1:]
|
|
|
|
|
)
|
|
|
|
|
self.assertEquals(
|
2013-12-11 14:41:42 +01:00
|
|
|
'< {"answer": "that\'s it!"}',
|
2013-05-02 00:34:46 +02:00
|
|
|
resp[0][0] % resp[0][1:]
|
|
|
|
|
)
|
|
|
|
|
|
2013-05-01 21:55:44 +02:00
|
|
|
def test_defaults(self):
|
|
|
|
|
con = self._get_mock_connection()
|
|
|
|
|
request = self._get_request(con, 'GET', '/')
|
|
|
|
|
|
|
|
|
|
self.assertEquals('http://localhost:9200/', request.url)
|
|
|
|
|
self.assertEquals('GET', request.method)
|
|
|
|
|
self.assertEquals(None, request.body)
|
|
|
|
|
|
|
|
|
|
def test_params_properly_encoded(self):
|
|
|
|
|
con = self._get_mock_connection()
|
|
|
|
|
request = self._get_request(con, 'GET', '/', params={'param': 'value with spaces'})
|
|
|
|
|
|
|
|
|
|
self.assertEquals('http://localhost:9200/?param=value+with+spaces', request.url)
|
|
|
|
|
self.assertEquals('GET', request.method)
|
|
|
|
|
self.assertEquals(None, request.body)
|
|
|
|
|
|
|
|
|
|
def test_body_attached(self):
|
|
|
|
|
con = self._get_mock_connection()
|
|
|
|
|
request = self._get_request(con, 'GET', '/', body='{"answer": 42}')
|
|
|
|
|
|
|
|
|
|
self.assertEquals('http://localhost:9200/', request.url)
|
|
|
|
|
self.assertEquals('GET', request.method)
|
2014-05-09 18:11:50 +02:00
|
|
|
self.assertEquals('{"answer": 42}'.encode('utf-8'), request.body)
|
2013-05-23 22:15:55 +02:00
|
|
|
|
2013-10-01 23:40:34 +02:00
|
|
|
def test_http_auth_attached(self):
|
2013-10-02 01:03:27 +02:00
|
|
|
con = self._get_mock_connection({'http_auth': 'username:secret'})
|
2013-10-01 23:40:34 +02:00
|
|
|
request = self._get_request(con, 'GET', '/')
|
|
|
|
|
|
|
|
|
|
self.assertEquals(request.headers['authorization'], 'Basic dXNlcm5hbWU6c2VjcmV0')
|
|
|
|
|
|
2013-08-25 16:39:56 +02:00
|
|
|
@patch('elasticsearch.connection.base.tracer')
|
2013-05-23 22:15:55 +02:00
|
|
|
def test_url_prefix(self, tracer):
|
|
|
|
|
con = self._get_mock_connection({"url_prefix": "/some-prefix/"})
|
2013-05-24 01:00:29 +02:00
|
|
|
request = self._get_request(con, 'GET', '/_search', body='{"answer": 42}', timeout=0.1)
|
2013-05-23 22:15:55 +02:00
|
|
|
|
|
|
|
|
self.assertEquals('http://localhost:9200/some-prefix/_search', request.url)
|
|
|
|
|
self.assertEquals('GET', request.method)
|
2014-05-09 18:11:50 +02:00
|
|
|
self.assertEquals('{"answer": 42}'.encode('utf-8'), request.body)
|
2013-05-23 22:15:55 +02:00
|
|
|
|
|
|
|
|
# trace request
|
|
|
|
|
self.assertEquals(1, tracer.info.call_count)
|
|
|
|
|
self.assertEquals(
|
2017-03-05 11:19:01 -08:00
|
|
|
"curl -H 'Content-Type: application/json' -XGET 'http://localhost:9200/_search?pretty' -d '{\n \"answer\": 42\n}'",
|
2013-05-23 22:15:55 +02:00
|
|
|
tracer.info.call_args[0][0] % tracer.info.call_args[0][1:]
|
|
|
|
|
)
|