From f10d7f2da2444e171e6d90b6d8ad700043b10920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Fri, 9 May 2014 18:11:50 +0200 Subject: [PATCH] Body is always sent as utf-8 bytes, reflect it in tests Thanks johbo for the report! --- elasticsearch/connection/base.py | 11 +++++++++++ test_elasticsearch/test_connection.py | 15 +++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/elasticsearch/connection/base.py b/elasticsearch/connection/base.py index 16c0b97a..50822c70 100644 --- a/elasticsearch/connection/base.py +++ b/elasticsearch/connection/base.py @@ -44,6 +44,11 @@ class Connection(object): # non-json data or a bulk request 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( '%s %s [status:%s request:%.3fs]', method, full_url, status_code, duration @@ -67,6 +72,12 @@ class Connection(object): '%s %s [status:%s request:%.3fs]', method, full_url, 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) def _raise_error(self, status_code, raw_data): diff --git a/test_elasticsearch/test_connection.py b/test_elasticsearch/test_connection.py index db4ca7e6..d9866353 100644 --- a/test_elasticsearch/test_connection.py +++ b/test_elasticsearch/test_connection.py @@ -58,8 +58,8 @@ class TestUrllib3Connection(TestCase): m = con.pool.urlopen = Mock() m.return_value.status = 200 - con.perform_request('PUT', '/', body='0123456789') - m.assert_called_once_with('PUT', '/', '0123456789', headers={'content-length': '10'}, retries=False) + con.perform_request('PUT', '/', body='0123456789'.encode('utf-8')) + m.assert_called_once_with('PUT', '/', '0123456789'.encode('utf-8'), headers={'content-length': '10'}, retries=False) class TestRequestsConnection(TestCase): def _get_mock_connection(self, connection_params={}, status_code=200, response_body='{}'): @@ -77,6 +77,9 @@ class TestRequestsConnection(TestCase): return con 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) self.assertEquals(200, status) self.assertEquals('{}', data) @@ -131,7 +134,7 @@ class TestRequestsConnection(TestCase): @patch('elasticsearch.connection.base.logger') def test_failed_request_logs_and_traces(self, logger, tracer): 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 self.assertEquals(0, tracer.info.call_count) @@ -148,7 +151,7 @@ class TestRequestsConnection(TestCase): @patch('elasticsearch.connection.base.logger') def test_success_logs_and_traces(self, logger, tracer): 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 self.assertEquals(1, tracer.info.call_count) @@ -203,7 +206,7 @@ class TestRequestsConnection(TestCase): self.assertEquals('http://localhost:9200/', request.url) 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): 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('GET', request.method) - self.assertEquals('{"answer": 42}', request.body) + self.assertEquals('{"answer": 42}'.encode('utf-8'), request.body) # trace request self.assertEquals(1, tracer.info.call_count)