Body is always sent as utf-8 bytes, reflect it in tests
Thanks johbo for the report!
This commit is contained in:
@@ -44,6 +44,11 @@ class Connection(object):
|
|||||||
# non-json data or a bulk request
|
# non-json data or a bulk request
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
# body has already been serialized to utf-8, deserialize it for logging
|
||||||
|
# TODO: find a better way to avoid (de)encoding the body back and forth
|
||||||
|
if body:
|
||||||
|
body = body.decode('utf-8')
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
'%s %s [status:%s request:%.3fs]', method, full_url,
|
'%s %s [status:%s request:%.3fs]', method, full_url,
|
||||||
status_code, duration
|
status_code, duration
|
||||||
@@ -67,6 +72,12 @@ class Connection(object):
|
|||||||
'%s %s [status:%s request:%.3fs]', method, full_url,
|
'%s %s [status:%s request:%.3fs]', method, full_url,
|
||||||
status_code or 'N/A', duration, exc_info=exception is not None
|
status_code or 'N/A', duration, exc_info=exception is not None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# body has already been serialized to utf-8, deserialize it for logging
|
||||||
|
# TODO: find a better way to avoid (de)encoding the body back and forth
|
||||||
|
if body:
|
||||||
|
body = body.decode('utf-8')
|
||||||
|
|
||||||
logger.info('> %s', body)
|
logger.info('> %s', body)
|
||||||
|
|
||||||
def _raise_error(self, status_code, raw_data):
|
def _raise_error(self, status_code, raw_data):
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ class TestUrllib3Connection(TestCase):
|
|||||||
m = con.pool.urlopen = Mock()
|
m = con.pool.urlopen = Mock()
|
||||||
m.return_value.status = 200
|
m.return_value.status = 200
|
||||||
|
|
||||||
con.perform_request('PUT', '/', body='0123456789')
|
con.perform_request('PUT', '/', body='0123456789'.encode('utf-8'))
|
||||||
m.assert_called_once_with('PUT', '/', '0123456789', headers={'content-length': '10'}, retries=False)
|
m.assert_called_once_with('PUT', '/', '0123456789'.encode('utf-8'), headers={'content-length': '10'}, retries=False)
|
||||||
|
|
||||||
class TestRequestsConnection(TestCase):
|
class TestRequestsConnection(TestCase):
|
||||||
def _get_mock_connection(self, connection_params={}, status_code=200, response_body='{}'):
|
def _get_mock_connection(self, connection_params={}, status_code=200, response_body='{}'):
|
||||||
@@ -77,6 +77,9 @@ class TestRequestsConnection(TestCase):
|
|||||||
return con
|
return con
|
||||||
|
|
||||||
def _get_request(self, connection, *args, **kwargs):
|
def _get_request(self, connection, *args, **kwargs):
|
||||||
|
if 'body' in kwargs:
|
||||||
|
kwargs['body'] = kwargs['body'].encode('utf-8')
|
||||||
|
|
||||||
status, headers, data = connection.perform_request(*args, **kwargs)
|
status, headers, data = connection.perform_request(*args, **kwargs)
|
||||||
self.assertEquals(200, status)
|
self.assertEquals(200, status)
|
||||||
self.assertEquals('{}', data)
|
self.assertEquals('{}', data)
|
||||||
@@ -131,7 +134,7 @@ class TestRequestsConnection(TestCase):
|
|||||||
@patch('elasticsearch.connection.base.logger')
|
@patch('elasticsearch.connection.base.logger')
|
||||||
def test_failed_request_logs_and_traces(self, logger, tracer):
|
def test_failed_request_logs_and_traces(self, logger, tracer):
|
||||||
con = self._get_mock_connection(response_body='{"answer": 42}', status_code=500)
|
con = self._get_mock_connection(response_body='{"answer": 42}', status_code=500)
|
||||||
self.assertRaises(TransportError, con.perform_request, 'GET', '/', {'param': 42}, '{}')
|
self.assertRaises(TransportError, con.perform_request, 'GET', '/', {'param': 42}, '{}'.encode('utf-8'))
|
||||||
|
|
||||||
# no trace request
|
# no trace request
|
||||||
self.assertEquals(0, tracer.info.call_count)
|
self.assertEquals(0, tracer.info.call_count)
|
||||||
@@ -148,7 +151,7 @@ class TestRequestsConnection(TestCase):
|
|||||||
@patch('elasticsearch.connection.base.logger')
|
@patch('elasticsearch.connection.base.logger')
|
||||||
def test_success_logs_and_traces(self, logger, tracer):
|
def test_success_logs_and_traces(self, logger, tracer):
|
||||||
con = self._get_mock_connection(response_body='''{"answer": "that's it!"}''')
|
con = self._get_mock_connection(response_body='''{"answer": "that's it!"}''')
|
||||||
status, headers, data = con.perform_request('GET', '/', {'param': 42}, '''{"question": "what's that?"}''')
|
status, headers, data = con.perform_request('GET', '/', {'param': 42}, '''{"question": "what's that?"}'''.encode('utf-8'))
|
||||||
|
|
||||||
# trace request
|
# trace request
|
||||||
self.assertEquals(1, tracer.info.call_count)
|
self.assertEquals(1, tracer.info.call_count)
|
||||||
@@ -203,7 +206,7 @@ class TestRequestsConnection(TestCase):
|
|||||||
|
|
||||||
self.assertEquals('http://localhost:9200/', request.url)
|
self.assertEquals('http://localhost:9200/', request.url)
|
||||||
self.assertEquals('GET', request.method)
|
self.assertEquals('GET', request.method)
|
||||||
self.assertEquals('{"answer": 42}', request.body)
|
self.assertEquals('{"answer": 42}'.encode('utf-8'), request.body)
|
||||||
|
|
||||||
def test_http_auth_attached(self):
|
def test_http_auth_attached(self):
|
||||||
con = self._get_mock_connection({'http_auth': 'username:secret'})
|
con = self._get_mock_connection({'http_auth': 'username:secret'})
|
||||||
@@ -218,7 +221,7 @@ class TestRequestsConnection(TestCase):
|
|||||||
|
|
||||||
self.assertEquals('http://localhost:9200/some-prefix/_search', request.url)
|
self.assertEquals('http://localhost:9200/some-prefix/_search', request.url)
|
||||||
self.assertEquals('GET', request.method)
|
self.assertEquals('GET', request.method)
|
||||||
self.assertEquals('{"answer": 42}', request.body)
|
self.assertEquals('{"answer": 42}'.encode('utf-8'), request.body)
|
||||||
|
|
||||||
# trace request
|
# trace request
|
||||||
self.assertEquals(1, tracer.info.call_count)
|
self.assertEquals(1, tracer.info.call_count)
|
||||||
|
|||||||
Reference in New Issue
Block a user