diff --git a/trytond/protocols/jsonrpc.py b/trytond/protocols/jsonrpc.py index 3c75cf30bc..62adae83d9 100644 --- a/trytond/protocols/jsonrpc.py +++ b/trytond/protocols/jsonrpc.py @@ -55,7 +55,8 @@ JSONDecoder.register('date', JSONDecoder.register('time', lambda dct: datetime.time(dct['hour'], dct['minute'], dct['second'], dct['microsecond'])) -JSONDecoder.register('buffer', buffer(base64.decodestring(dct['base64']))) +JSONDecoder.register('buffer', lambda dct: + buffer(base64.decodestring(dct['base64']))) JSONDecoder.register('Decimal', lambda dct: Decimal(dct['decimal'])) diff --git a/trytond/protocols/xmlrpc.py b/trytond/protocols/xmlrpc.py index c3d97e4372..6bb488f186 100644 --- a/trytond/protocols/xmlrpc.py +++ b/trytond/protocols/xmlrpc.py @@ -118,6 +118,14 @@ def _end_dateTime(self, data): xmlrpclib.Unmarshaller.dispatch["dateTime.iso8601"] = _end_dateTime +def _end_base64(self, data): + value = xmlrpclib.Binary() + value.decode(data) + self.append(buffer(value.data)) + self._value = 0 +xmlrpclib.Unmarshaller.dispatch['base64'] = _end_base64 + + class GenericXMLRPCRequestHandler: def _dispatch(self, method, params): diff --git a/trytond/tests/test_protocols.py b/trytond/tests/test_protocols.py new file mode 100644 index 0000000000..d7f6cf505a --- /dev/null +++ b/trytond/tests/test_protocols.py @@ -0,0 +1,72 @@ +#This file is part of Tryton. The COPYRIGHT file at the top level of +#this repository contains the full copyright notices and license terms. + +import unittest +import json +import datetime +from decimal import Decimal + +from trytond.protocols.jsonrpc import JSONEncoder, JSONDecoder +from trytond.protocols.xmlrpc import xmlrpclib + + +class JSONTestCase(unittest.TestCase): + 'Test JSON' + + def dumps_loads(self, value): + self.assertEqual(json.loads( + json.dumps(value, cls=JSONEncoder), + object_hook=JSONDecoder()), value) + + def test_datetime(self): + 'Test datetime' + self.dumps_loads(datetime.datetime.now()) + + def test_date(self): + 'Test date' + self.dumps_loads(datetime.date.today()) + + def test_time(self): + 'Test time' + self.dumps_loads(datetime.datetime.now().time()) + + def test_buffer(self): + 'Test buffer' + self.dumps_loads(buffer('foo')) + + def test_decimal(self): + 'Test Decimal' + self.dumps_loads(Decimal('3.141592653589793')) + + +class XMLTestCase(unittest.TestCase): + 'Test XML' + + def dumps_loads(self, value): + s = xmlrpclib.dumps((value,)) + result, _ = xmlrpclib.loads(s) + result, = result + self.assertEqual(value, result) + + def test_decimal(self): + 'Test Decimal' + self.dumps_loads(Decimal('3.141592653589793')) + + def test_buffer(self): + 'Test buffer' + self.dumps_loads(buffer('foo')) + + def test_date(self): + 'Test date' + self.dumps_loads(datetime.date.today()) + + def test_time(self): + 'Test time' + self.dumps_loads(datetime.datetime.now().time()) + + +def suite(): + suite_ = unittest.TestSuite() + suite_.addTests(unittest.TestLoader().loadTestsFromTestCase(JSONTestCase)) + suite_.addTests(unittest.TestLoader().loadTestsFromTestCase(XMLTestCase)) + return suite_