diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index 012c5a3c3..dc8cd8911 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -292,7 +292,7 @@ def perform_request(self, method, url, params=None, body=None): if body is not None: try: - body = body.encode('utf-8') + body = body.encode('utf-8', 'surrogatepass') except (UnicodeDecodeError, AttributeError): # bytes/str - no need to re-encode pass diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 74f52fd43..328325c1c 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -105,6 +105,13 @@ def test_body_bytes_get_passed_untouched(self): self.assertEquals(1, len(t.get_connection().calls)) self.assertEquals(('GET', '/', None, body), t.get_connection().calls[0][0]) + def test_body_surrogates_replaced_encoded_into_bytes(self): + t = Transport([{}], connection_class=DummyConnection) + + t.perform_request('GET', '/', body='你好\uda6a') + self.assertEquals(1, len(t.get_connection().calls)) + self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa'), t.get_connection().calls[0][0]) + def test_kwargs_passed_on_to_connections(self): t = Transport([{'host': 'google.com'}], port=123) self.assertEquals(1, len(t.connection_pool.connections))