Skip to content
2 changes: 1 addition & 1 deletion elasticsearch/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test_elasticsearch/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down