Skip to content

Fixes non UTF-8 surrogateescapes #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 11, 2017
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