thrift_json.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. import sys
  20. import unittest
  21. # FIX_FOR_YANDEX remove import thrift._import_local_thrift # noqa
  22. from thrift.protocol.TJSONProtocol import TJSONProtocol
  23. from thrift.transport import TTransport
  24. #
  25. # In order to run the test under Windows. We need to create symbolic link
  26. # name 'thrift' to '../src' folder by using:
  27. #
  28. # mklink /D thrift ..\src
  29. #
  30. class TestJSONString(unittest.TestCase):
  31. def test_escaped_unicode_string(self):
  32. unicode_json = b'"hello \\u0e01\\u0e02\\u0e03\\ud835\\udcab\\udb40\\udc70 unicode"'
  33. unicode_text = u'hello \u0e01\u0e02\u0e03\U0001D4AB\U000E0070 unicode'
  34. buf = TTransport.TMemoryBuffer(unicode_json)
  35. transport = TTransport.TBufferedTransportFactory().getTransport(buf)
  36. protocol = TJSONProtocol(transport)
  37. if sys.version_info[0] == 2:
  38. unicode_text = unicode_text.encode('utf8')
  39. self.assertEqual(protocol.readString(), unicode_text)
  40. if __name__ == '__main__':
  41. unittest.main()