Fix unicode body handling.
Only body should be encoded as bytes, not anything going through the serializer (mostly because of bulk)
This commit is contained in:
@@ -21,11 +21,7 @@ class JSONSerializer(object):
|
|||||||
def dumps(self, data):
|
def dumps(self, data):
|
||||||
# don't serialize strings
|
# don't serialize strings
|
||||||
if isinstance(data, (type(''), type(u''))):
|
if isinstance(data, (type(''), type(u''))):
|
||||||
try:
|
return data
|
||||||
return data.encode('utf-8')
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# Python 2 and str, no need to re-encode
|
|
||||||
return data
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return json.dumps(data, default=self.default)
|
return json.dumps(data, default=self.default)
|
||||||
|
|||||||
@@ -234,6 +234,13 @@ class Transport(object):
|
|||||||
params['source'] = body
|
params['source'] = body
|
||||||
body = None
|
body = None
|
||||||
|
|
||||||
|
if body is not None:
|
||||||
|
try:
|
||||||
|
body = body.encode('utf-8')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# Python 2 and str, no need to re-encode
|
||||||
|
pass
|
||||||
|
|
||||||
ignore = ()
|
ignore = ()
|
||||||
if params and 'ignore' in params:
|
if params and 'ignore' in params:
|
||||||
ignore = params.pop('ignore')
|
ignore = params.pop('ignore')
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@@ -26,4 +27,4 @@ class TestJSONSerializer(TestCase):
|
|||||||
self.assertRaises(SerializationError, JSONSerializer().loads, '{{')
|
self.assertRaises(SerializationError, JSONSerializer().loads, '{{')
|
||||||
|
|
||||||
def test_strings_are_left_untouched(self):
|
def test_strings_are_left_untouched(self):
|
||||||
self.assertEquals('Hello World!', JSONSerializer().dumps('Hello World!'))
|
self.assertEquals(u"你好", JSONSerializer().dumps(u"你好"))
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from elasticsearch.transport import Transport
|
from elasticsearch.transport import Transport
|
||||||
@@ -47,7 +48,15 @@ class TestTransport(TestCase):
|
|||||||
|
|
||||||
t.perform_request('GET', '/', body={})
|
t.perform_request('GET', '/', body={})
|
||||||
self.assertEquals(1, len(t.get_connection().calls))
|
self.assertEquals(1, len(t.get_connection().calls))
|
||||||
self.assertEquals(('POST', '/', None, '{}'), t.get_connection().calls[0][0])
|
self.assertEquals(('POST', '/', None, b'{}'), t.get_connection().calls[0][0])
|
||||||
|
|
||||||
|
def test_body_gets_encoded_into_bytes(self):
|
||||||
|
t = Transport([{}], connection_class=DummyConnection)
|
||||||
|
|
||||||
|
t.perform_request('GET', '/', body=u'你好')
|
||||||
|
self.assertEquals(1, len(t.get_connection().calls))
|
||||||
|
self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd'), t.get_connection().calls[0][0])
|
||||||
|
|
||||||
|
|
||||||
def test_kwargs_passed_on_to_connections(self):
|
def test_kwargs_passed_on_to_connections(self):
|
||||||
t = Transport([{'host': 'google.com'}], port=123)
|
t = Transport([{'host': 'google.com'}], port=123)
|
||||||
|
|||||||
Reference in New Issue
Block a user