blob: bf2b8086170f8b433d4c68c667eb4793757ad44f [file] [log] [blame]
Nobuaki Sukegawaad835862015-12-23 23:32:09 +09001#
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 Phuttha7f01e2a2015-11-06 15:46:50 +070019
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +070020import unittest
21
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090022import _import_local_thrift # noqa
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090023from thrift.protocol.TJSONProtocol import TJSONProtocol
24from thrift.transport import TTransport
25
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +070026#
27# In order to run the test under Windows. We need to create symbolic link
28# name 'thrift' to '../src' folder by using:
29#
30# mklink /D thrift ..\src
31#
32
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090033
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +070034class TestJSONString(unittest.TestCase):
35
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090036 def test_escaped_unicode_string(self):
37 unicode_json = b'"hello \\u0e01\\u0e02\\u0e03\\ud835\\udcab\\udb40\\udc70 unicode"'
38 unicode_text = u'hello \u0e01\u0e02\u0e03\U0001D4AB\U000E0070 unicode'
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +070039
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090040 buf = TTransport.TMemoryBuffer(unicode_json)
41 transport = TTransport.TBufferedTransportFactory().getTransport(buf)
42 protocol = TJSONProtocol(transport)
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +070043
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090044 self.assertEqual(protocol.readString(), unicode_text)
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +070045
zeshuai007b83ae2c2020-04-24 11:35:55 +080046 def test_TJSONProtocol_write(self):
47 write_data = '{"software":"thrift","1":[23,1.2010000000000001,32767,2147483647,9223372036854775807],"base64":"aGVsbG8gdGhyaWZ0","bool":0}'
48
49 buff = TTransport.TMemoryBuffer()
50 transport = TTransport.TBufferedTransportFactory().getTransport(buff)
51 protocol = TJSONProtocol(transport)
52 protocol.writeJSONObjectStart()
53 protocol.writeJSONString("software")
54 protocol.writeJSONString("thrift")
55 protocol.writeJSONString("1")
56 protocol.writeJSONArrayStart()
57 protocol.writeJSONNumber(23)
58 protocol.writeDouble(1.201)
59 protocol.writeI16(32767)
60 protocol.writeI32(2147483647)
61 protocol.writeI64(9223372036854775807)
62 protocol.writeJSONArrayEnd()
63 protocol.writeJSONString("base64")
64 protocol.writeJSONBase64("hello thrift".encode('utf-8'))
65 protocol.writeJSONString("bool")
66 protocol.writeBool(0)
67 protocol.writeJSONObjectEnd()
68
69 transport.flush()
70 value = buff.getvalue()
71
72 self.assertEqual(write_data, value.decode('utf-8'))
73
74 def test_TJSONProtol_read(self):
75 expected = "{'software':'thrift','1':[23,1.2010000000000001,32767,2147483647,9223372036854775807],'base64':'hello thrift','bool':False}"
76 read_data = '{"software":"thrift","1":[23,1.2010000000000001,32767,2147483647,9223372036854775807],"base64":"aGVsbG8gdGhyaWZ0","bool":0}'
77
78 buff = TTransport.TMemoryBuffer(read_data.encode('utf-8'))
79 transport = TTransport.TBufferedTransportFactory().getTransport(buff)
80 protocol = TJSONProtocol(transport)
81 protocol.readJSONObjectStart()
82 u_1 = protocol.readString()
83 u_2 = protocol.readString()
84 u_3 = protocol.readString()
85 protocol.readJSONArrayStart()
86 u_4 = protocol.readNumber()
87 u_5 = protocol.readDouble()
88 u_6 = protocol.readI16()
89 u_7 = protocol.readI32()
90 u_8 = protocol.readI64()
91 protocol.readJSONArrayEnd()
92 u_9 = protocol.readString()
93 u_10 = protocol.readJSONBase64()
94 u_11 = protocol.readString()
95 u_12 = protocol.readBool()
96 protocol.writeJSONObjectEnd()
97
98 result_read = {}
99 result_read[u_1] = u_2
100 result_read[u_3] = []
101 result_read[u_3].append(u_4)
102 result_read[u_3].append(u_5)
103 result_read[u_3].append(u_6)
104 result_read[u_3].append(u_7)
105 result_read[u_3].append(u_8)
106 result_read[u_9] = u_10.decode('utf-8')
107 result_read[u_11] = u_12
108
109 self.assertEqual(eval(expected), result_read)
110
James E. King, III0ad20bd2017-09-30 15:44:16 -0700111
Phongphan Phuttha7f01e2a2015-11-06 15:46:50 +0700112if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900113 unittest.main()