Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 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 | # |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 19 | |
| 20 | import sys |
| 21 | import unittest |
| 22 | |
Nobuaki Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame] | 23 | import _import_local_thrift # noqa |
Nobuaki Sukegawa | ad83586 | 2015-12-23 23:32:09 +0900 | [diff] [blame] | 24 | from thrift.protocol.TJSONProtocol import TJSONProtocol |
| 25 | from thrift.transport import TTransport |
| 26 | |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 27 | # |
| 28 | # In order to run the test under Windows. We need to create symbolic link |
| 29 | # name 'thrift' to '../src' folder by using: |
| 30 | # |
| 31 | # mklink /D thrift ..\src |
| 32 | # |
| 33 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 34 | |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 35 | class TestJSONString(unittest.TestCase): |
| 36 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 37 | def test_escaped_unicode_string(self): |
| 38 | unicode_json = b'"hello \\u0e01\\u0e02\\u0e03\\ud835\\udcab\\udb40\\udc70 unicode"' |
| 39 | unicode_text = u'hello \u0e01\u0e02\u0e03\U0001D4AB\U000E0070 unicode' |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 40 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | buf = TTransport.TMemoryBuffer(unicode_json) |
| 42 | transport = TTransport.TBufferedTransportFactory().getTransport(buf) |
| 43 | protocol = TJSONProtocol(transport) |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 44 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 45 | if sys.version_info[0] == 2: |
| 46 | unicode_text = unicode_text.encode('utf8') |
| 47 | self.assertEqual(protocol.readString(), unicode_text) |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 48 | |
zeshuai007 | b83ae2c | 2020-04-24 11:35:55 +0800 | [diff] [blame^] | 49 | def test_TJSONProtocol_write(self): |
| 50 | write_data = '{"software":"thrift","1":[23,1.2010000000000001,32767,2147483647,9223372036854775807],"base64":"aGVsbG8gdGhyaWZ0","bool":0}' |
| 51 | |
| 52 | buff = TTransport.TMemoryBuffer() |
| 53 | transport = TTransport.TBufferedTransportFactory().getTransport(buff) |
| 54 | protocol = TJSONProtocol(transport) |
| 55 | protocol.writeJSONObjectStart() |
| 56 | protocol.writeJSONString("software") |
| 57 | protocol.writeJSONString("thrift") |
| 58 | protocol.writeJSONString("1") |
| 59 | protocol.writeJSONArrayStart() |
| 60 | protocol.writeJSONNumber(23) |
| 61 | protocol.writeDouble(1.201) |
| 62 | protocol.writeI16(32767) |
| 63 | protocol.writeI32(2147483647) |
| 64 | protocol.writeI64(9223372036854775807) |
| 65 | protocol.writeJSONArrayEnd() |
| 66 | protocol.writeJSONString("base64") |
| 67 | protocol.writeJSONBase64("hello thrift".encode('utf-8')) |
| 68 | protocol.writeJSONString("bool") |
| 69 | protocol.writeBool(0) |
| 70 | protocol.writeJSONObjectEnd() |
| 71 | |
| 72 | transport.flush() |
| 73 | value = buff.getvalue() |
| 74 | |
| 75 | self.assertEqual(write_data, value.decode('utf-8')) |
| 76 | |
| 77 | def test_TJSONProtol_read(self): |
| 78 | expected = "{'software':'thrift','1':[23,1.2010000000000001,32767,2147483647,9223372036854775807],'base64':'hello thrift','bool':False}" |
| 79 | read_data = '{"software":"thrift","1":[23,1.2010000000000001,32767,2147483647,9223372036854775807],"base64":"aGVsbG8gdGhyaWZ0","bool":0}' |
| 80 | |
| 81 | buff = TTransport.TMemoryBuffer(read_data.encode('utf-8')) |
| 82 | transport = TTransport.TBufferedTransportFactory().getTransport(buff) |
| 83 | protocol = TJSONProtocol(transport) |
| 84 | protocol.readJSONObjectStart() |
| 85 | u_1 = protocol.readString() |
| 86 | u_2 = protocol.readString() |
| 87 | u_3 = protocol.readString() |
| 88 | protocol.readJSONArrayStart() |
| 89 | u_4 = protocol.readNumber() |
| 90 | u_5 = protocol.readDouble() |
| 91 | u_6 = protocol.readI16() |
| 92 | u_7 = protocol.readI32() |
| 93 | u_8 = protocol.readI64() |
| 94 | protocol.readJSONArrayEnd() |
| 95 | u_9 = protocol.readString() |
| 96 | u_10 = protocol.readJSONBase64() |
| 97 | u_11 = protocol.readString() |
| 98 | u_12 = protocol.readBool() |
| 99 | protocol.writeJSONObjectEnd() |
| 100 | |
| 101 | result_read = {} |
| 102 | result_read[u_1] = u_2 |
| 103 | result_read[u_3] = [] |
| 104 | result_read[u_3].append(u_4) |
| 105 | result_read[u_3].append(u_5) |
| 106 | result_read[u_3].append(u_6) |
| 107 | result_read[u_3].append(u_7) |
| 108 | result_read[u_3].append(u_8) |
| 109 | result_read[u_9] = u_10.decode('utf-8') |
| 110 | result_read[u_11] = u_12 |
| 111 | |
| 112 | self.assertEqual(eval(expected), result_read) |
| 113 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 114 | |
Phongphan Phuttha | 7f01e2a | 2015-11-06 15:46:50 +0700 | [diff] [blame] | 115 | if __name__ == '__main__': |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 116 | unittest.main() |