blob: 8da8e0e74ce9a75d6098eb67d35dc78bed01e55a [file] [log] [blame]
Jake Farrellb5a18a12012-10-09 01:10:43 +00001# encoding: UTF-8
Jake Farrell6f0f5272012-01-31 03:39:30 +00002#
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements. See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership. The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License. You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied. See the License for the
17# specific language governing permissions and limitations
18# under the License.
19#
20
Jake Farrella87810f2012-09-28 01:59:04 +000021require 'spec_helper'
Jake Farrell6f0f5272012-01-31 03:39:30 +000022
Jake Farrella87810f2012-09-28 01:59:04 +000023describe 'JsonProtocol' do
Jake Farrella87810f2012-09-28 01:59:04 +000024 describe Thrift::JsonProtocol do
25 before(:each) do
26 @trans = Thrift::MemoryBufferTransport.new
27 @prot = Thrift::JsonProtocol.new(@trans)
28 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000029
Jake Farrella87810f2012-09-28 01:59:04 +000030 it "should write json escaped char" do
31 @prot.write_json_escape_char("\n")
James E. King III27247072018-03-22 20:50:23 -040032 expect(@trans.read(@trans.available)).to eq('\u000a')
Jake Farrell6f0f5272012-01-31 03:39:30 +000033
Jake Farrella87810f2012-09-28 01:59:04 +000034 @prot.write_json_escape_char(" ")
James E. King III27247072018-03-22 20:50:23 -040035 expect(@trans.read(@trans.available)).to eq('\u0020')
Jake Farrella87810f2012-09-28 01:59:04 +000036 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000037
Jake Farrella87810f2012-09-28 01:59:04 +000038 it "should write json char" do
39 @prot.write_json_char("\n")
James E. King III27247072018-03-22 20:50:23 -040040 expect(@trans.read(@trans.available)).to eq('\\n')
Jake Farrell6f0f5272012-01-31 03:39:30 +000041
Jake Farrella87810f2012-09-28 01:59:04 +000042 @prot.write_json_char(" ")
James E. King III27247072018-03-22 20:50:23 -040043 expect(@trans.read(@trans.available)).to eq(' ')
Jake Farrell6f0f5272012-01-31 03:39:30 +000044
Jake Farrella87810f2012-09-28 01:59:04 +000045 @prot.write_json_char("\\")
James E. King III27247072018-03-22 20:50:23 -040046 expect(@trans.read(@trans.available)).to eq("\\\\")
Jake Farrell6f0f5272012-01-31 03:39:30 +000047
Jake Farrella87810f2012-09-28 01:59:04 +000048 @prot.write_json_char("@")
James E. King III27247072018-03-22 20:50:23 -040049 expect(@trans.read(@trans.available)).to eq('@')
Jake Farrella87810f2012-09-28 01:59:04 +000050 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000051
Jake Farrella87810f2012-09-28 01:59:04 +000052 it "should write json string" do
53 @prot.write_json_string("this is a \\ json\nstring")
James E. King III27247072018-03-22 20:50:23 -040054 expect(@trans.read(@trans.available)).to eq("\"this is a \\\\ json\\nstring\"")
Jake Farrella87810f2012-09-28 01:59:04 +000055 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000056
Jake Farrella87810f2012-09-28 01:59:04 +000057 it "should write json base64" do
58 @prot.write_json_base64("this is a base64 string")
James E. King III27247072018-03-22 20:50:23 -040059 expect(@trans.read(@trans.available)).to eq("\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\"")
Jake Farrella87810f2012-09-28 01:59:04 +000060 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000061
Jake Farrella87810f2012-09-28 01:59:04 +000062 it "should write json integer" do
63 @prot.write_json_integer(45)
James E. King III27247072018-03-22 20:50:23 -040064 expect(@trans.read(@trans.available)).to eq("45")
Jake Farrell6f0f5272012-01-31 03:39:30 +000065
Jake Farrella87810f2012-09-28 01:59:04 +000066 @prot.write_json_integer(33000)
James E. King III27247072018-03-22 20:50:23 -040067 expect(@trans.read(@trans.available)).to eq("33000")
Jake Farrell6f0f5272012-01-31 03:39:30 +000068
Jake Farrella87810f2012-09-28 01:59:04 +000069 @prot.write_json_integer(3000000000)
James E. King III27247072018-03-22 20:50:23 -040070 expect(@trans.read(@trans.available)).to eq("3000000000")
Jake Farrell6f0f5272012-01-31 03:39:30 +000071
Jake Farrella87810f2012-09-28 01:59:04 +000072 @prot.write_json_integer(6000000000)
James E. King III27247072018-03-22 20:50:23 -040073 expect(@trans.read(@trans.available)).to eq("6000000000")
Jake Farrella87810f2012-09-28 01:59:04 +000074 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000075
Jake Farrella87810f2012-09-28 01:59:04 +000076 it "should write json double" do
77 @prot.write_json_double(12.3)
James E. King III27247072018-03-22 20:50:23 -040078 expect(@trans.read(@trans.available)).to eq("12.3")
Jake Farrell6f0f5272012-01-31 03:39:30 +000079
Jake Farrella87810f2012-09-28 01:59:04 +000080 @prot.write_json_double(-3.21)
James E. King III27247072018-03-22 20:50:23 -040081 expect(@trans.read(@trans.available)).to eq("-3.21")
Jake Farrell6f0f5272012-01-31 03:39:30 +000082
Jake Farrella87810f2012-09-28 01:59:04 +000083 @prot.write_json_double(((+1.0/0.0)/(+1.0/0.0)))
James E. King III27247072018-03-22 20:50:23 -040084 expect(@trans.read(@trans.available)).to eq("\"NaN\"")
Jake Farrell6f0f5272012-01-31 03:39:30 +000085
Jake Farrella87810f2012-09-28 01:59:04 +000086 @prot.write_json_double((+1.0/0.0))
James E. King III27247072018-03-22 20:50:23 -040087 expect(@trans.read(@trans.available)).to eq("\"Infinity\"")
Jake Farrell6f0f5272012-01-31 03:39:30 +000088
Jake Farrella87810f2012-09-28 01:59:04 +000089 @prot.write_json_double((-1.0/0.0))
James E. King III27247072018-03-22 20:50:23 -040090 expect(@trans.read(@trans.available)).to eq("\"-Infinity\"")
Jake Farrella87810f2012-09-28 01:59:04 +000091 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000092
Jake Farrella87810f2012-09-28 01:59:04 +000093 it "should write json object start" do
94 @prot.write_json_object_start
James E. King III27247072018-03-22 20:50:23 -040095 expect(@trans.read(@trans.available)).to eq("{")
Jake Farrella87810f2012-09-28 01:59:04 +000096 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000097
Jake Farrella87810f2012-09-28 01:59:04 +000098 it "should write json object end" do
99 @prot.write_json_object_end
James E. King III27247072018-03-22 20:50:23 -0400100 expect(@trans.read(@trans.available)).to eq("}")
Jake Farrella87810f2012-09-28 01:59:04 +0000101 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000102
Jake Farrella87810f2012-09-28 01:59:04 +0000103 it "should write json array start" do
104 @prot.write_json_array_start
James E. King III27247072018-03-22 20:50:23 -0400105 expect(@trans.read(@trans.available)).to eq("[")
Jake Farrella87810f2012-09-28 01:59:04 +0000106 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000107
Jake Farrella87810f2012-09-28 01:59:04 +0000108 it "should write json array end" do
109 @prot.write_json_array_end
James E. King III27247072018-03-22 20:50:23 -0400110 expect(@trans.read(@trans.available)).to eq("]")
Jake Farrella87810f2012-09-28 01:59:04 +0000111 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000112
Jake Farrella87810f2012-09-28 01:59:04 +0000113 it "should write message begin" do
114 @prot.write_message_begin("name", 12, 32)
James E. King III27247072018-03-22 20:50:23 -0400115 expect(@trans.read(@trans.available)).to eq("[1,\"name\",12,32")
Jake Farrella87810f2012-09-28 01:59:04 +0000116 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000117
Jake Farrella87810f2012-09-28 01:59:04 +0000118 it "should write message end" do
119 @prot.write_message_end
James E. King III27247072018-03-22 20:50:23 -0400120 expect(@trans.read(@trans.available)).to eq("]")
Jake Farrella87810f2012-09-28 01:59:04 +0000121 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000122
Jake Farrella87810f2012-09-28 01:59:04 +0000123 it "should write struct begin" do
124 @prot.write_struct_begin("name")
James E. King III27247072018-03-22 20:50:23 -0400125 expect(@trans.read(@trans.available)).to eq("{")
Jake Farrella87810f2012-09-28 01:59:04 +0000126 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000127
Jake Farrella87810f2012-09-28 01:59:04 +0000128 it "should write struct end" do
129 @prot.write_struct_end
James E. King III27247072018-03-22 20:50:23 -0400130 expect(@trans.read(@trans.available)).to eq("}")
Jake Farrella87810f2012-09-28 01:59:04 +0000131 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000132
Jake Farrella87810f2012-09-28 01:59:04 +0000133 it "should write field begin" do
134 @prot.write_field_begin("name", Thrift::Types::STRUCT, 32)
James E. King III27247072018-03-22 20:50:23 -0400135 expect(@trans.read(@trans.available)).to eq("32{\"rec\"")
Jake Farrella87810f2012-09-28 01:59:04 +0000136 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000137
Jake Farrella87810f2012-09-28 01:59:04 +0000138 it "should write field end" do
139 @prot.write_field_end
James E. King III27247072018-03-22 20:50:23 -0400140 expect(@trans.read(@trans.available)).to eq("}")
Jake Farrella87810f2012-09-28 01:59:04 +0000141 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000142
Jake Farrella87810f2012-09-28 01:59:04 +0000143 it "should write field stop" do
144 @prot.write_field_stop
James E. King III27247072018-03-22 20:50:23 -0400145 expect(@trans.read(@trans.available)).to eq("")
Jake Farrella87810f2012-09-28 01:59:04 +0000146 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000147
Jake Farrella87810f2012-09-28 01:59:04 +0000148 it "should write map begin" do
149 @prot.write_map_begin(Thrift::Types::STRUCT, Thrift::Types::LIST, 32)
James E. King III27247072018-03-22 20:50:23 -0400150 expect(@trans.read(@trans.available)).to eq("[\"rec\",\"lst\",32,{")
Jake Farrella87810f2012-09-28 01:59:04 +0000151 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000152
Jake Farrella87810f2012-09-28 01:59:04 +0000153 it "should write map end" do
154 @prot.write_map_end
James E. King III27247072018-03-22 20:50:23 -0400155 expect(@trans.read(@trans.available)).to eq("}]")
Jake Farrella87810f2012-09-28 01:59:04 +0000156 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000157
Jake Farrella87810f2012-09-28 01:59:04 +0000158 it "should write list begin" do
159 @prot.write_list_begin(Thrift::Types::STRUCT, 32)
James E. King III27247072018-03-22 20:50:23 -0400160 expect(@trans.read(@trans.available)).to eq("[\"rec\",32")
Jake Farrella87810f2012-09-28 01:59:04 +0000161 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000162
Jake Farrella87810f2012-09-28 01:59:04 +0000163 it "should write list end" do
164 @prot.write_list_end
James E. King III27247072018-03-22 20:50:23 -0400165 expect(@trans.read(@trans.available)).to eq("]")
Jake Farrella87810f2012-09-28 01:59:04 +0000166 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000167
Jake Farrella87810f2012-09-28 01:59:04 +0000168 it "should write set begin" do
169 @prot.write_set_begin(Thrift::Types::STRUCT, 32)
James E. King III27247072018-03-22 20:50:23 -0400170 expect(@trans.read(@trans.available)).to eq("[\"rec\",32")
Jake Farrella87810f2012-09-28 01:59:04 +0000171 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000172
Jake Farrella87810f2012-09-28 01:59:04 +0000173 it "should write set end" do
174 @prot.write_set_end
James E. King III27247072018-03-22 20:50:23 -0400175 expect(@trans.read(@trans.available)).to eq("]")
Jake Farrella87810f2012-09-28 01:59:04 +0000176 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000177
Jake Farrella87810f2012-09-28 01:59:04 +0000178 it "should write bool" do
179 @prot.write_bool(true)
James E. King III27247072018-03-22 20:50:23 -0400180 expect(@trans.read(@trans.available)).to eq("1")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000181
Jake Farrella87810f2012-09-28 01:59:04 +0000182 @prot.write_bool(false)
James E. King III27247072018-03-22 20:50:23 -0400183 expect(@trans.read(@trans.available)).to eq("0")
Jake Farrella87810f2012-09-28 01:59:04 +0000184 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000185
Jake Farrella87810f2012-09-28 01:59:04 +0000186 it "should write byte" do
187 @prot.write_byte(100)
James E. King III27247072018-03-22 20:50:23 -0400188 expect(@trans.read(@trans.available)).to eq("100")
Jake Farrella87810f2012-09-28 01:59:04 +0000189 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000190
Jake Farrella87810f2012-09-28 01:59:04 +0000191 it "should write i16" do
192 @prot.write_i16(1000)
James E. King III27247072018-03-22 20:50:23 -0400193 expect(@trans.read(@trans.available)).to eq("1000")
Jake Farrella87810f2012-09-28 01:59:04 +0000194 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000195
Jake Farrella87810f2012-09-28 01:59:04 +0000196 it "should write i32" do
197 @prot.write_i32(3000000000)
James E. King III27247072018-03-22 20:50:23 -0400198 expect(@trans.read(@trans.available)).to eq("3000000000")
Jake Farrella87810f2012-09-28 01:59:04 +0000199 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000200
Jake Farrella87810f2012-09-28 01:59:04 +0000201 it "should write i64" do
202 @prot.write_i64(6000000000)
James E. King III27247072018-03-22 20:50:23 -0400203 expect(@trans.read(@trans.available)).to eq("6000000000")
Jake Farrella87810f2012-09-28 01:59:04 +0000204 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000205
Jake Farrella87810f2012-09-28 01:59:04 +0000206 it "should write double" do
207 @prot.write_double(1.23)
James E. King III27247072018-03-22 20:50:23 -0400208 expect(@trans.read(@trans.available)).to eq("1.23")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000209
Jake Farrella87810f2012-09-28 01:59:04 +0000210 @prot.write_double(-32.1)
James E. King III27247072018-03-22 20:50:23 -0400211 expect(@trans.read(@trans.available)).to eq("-32.1")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000212
Jake Farrella87810f2012-09-28 01:59:04 +0000213 @prot.write_double(((+1.0/0.0)/(+1.0/0.0)))
James E. King III27247072018-03-22 20:50:23 -0400214 expect(@trans.read(@trans.available)).to eq("\"NaN\"")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000215
Jake Farrella87810f2012-09-28 01:59:04 +0000216 @prot.write_double((+1.0/0.0))
James E. King III27247072018-03-22 20:50:23 -0400217 expect(@trans.read(@trans.available)).to eq("\"Infinity\"")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000218
Jake Farrella87810f2012-09-28 01:59:04 +0000219 @prot.write_double((-1.0/0.0))
James E. King III27247072018-03-22 20:50:23 -0400220 expect(@trans.read(@trans.available)).to eq("\"-Infinity\"")
Jake Farrella87810f2012-09-28 01:59:04 +0000221 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000222
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -0500223 it 'should write string' do
224 @prot.write_string('this is a test string')
225 a = @trans.read(@trans.available)
226 expect(a).to eq('"this is a test string"'.force_encoding(Encoding::BINARY))
227 expect(a.encoding).to eq(Encoding::BINARY)
228 end
Jake Farrellb5a18a12012-10-09 01:10:43 +0000229
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -0500230 it 'should write string with unicode characters' do
231 @prot.write_string("this is a test string with unicode characters: \u20AC \u20AD")
232 a = @trans.read(@trans.available)
233 expect(a).to eq("\"this is a test string with unicode characters: \u20AC \u20AD\"".force_encoding(Encoding::BINARY))
234 expect(a.encoding).to eq(Encoding::BINARY)
Jake Farrella87810f2012-09-28 01:59:04 +0000235 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000236
Jake Farrella87810f2012-09-28 01:59:04 +0000237 it "should write binary" do
238 @prot.write_binary("this is a base64 string")
James E. King III27247072018-03-22 20:50:23 -0400239 expect(@trans.read(@trans.available)).to eq("\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\"")
Jens Geyer123258b2015-10-02 00:38:17 +0200240 end
241
242 it "should write long binary" do
243 @prot.write_binary((0...256).to_a.pack('C*'))
James E. King III27247072018-03-22 20:50:23 -0400244 expect(@trans.read(@trans.available)).to eq("\"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==\"")
Jake Farrella87810f2012-09-28 01:59:04 +0000245 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000246
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500247 it "should write a uuid" do
248 @prot.write_uuid("00112233-4455-6677-8899-aabbccddeeff")
249 expect(@trans.read(@trans.available)).to eq("\"00112233-4455-6677-8899-aabbccddeeff\"")
250 end
251
Jake Farrella87810f2012-09-28 01:59:04 +0000252 it "should get type name for type id" do
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400253 expect { @prot.get_type_name_for_type_id(Thrift::Types::STOP) }.to raise_error(NotImplementedError)
254 expect { @prot.get_type_name_for_type_id(Thrift::Types::VOID) }.to raise_error(NotImplementedError)
James E. King III27247072018-03-22 20:50:23 -0400255 expect(@prot.get_type_name_for_type_id(Thrift::Types::BOOL)).to eq("tf")
256 expect(@prot.get_type_name_for_type_id(Thrift::Types::BYTE)).to eq("i8")
257 expect(@prot.get_type_name_for_type_id(Thrift::Types::DOUBLE)).to eq("dbl")
258 expect(@prot.get_type_name_for_type_id(Thrift::Types::I16)).to eq("i16")
259 expect(@prot.get_type_name_for_type_id(Thrift::Types::I32)).to eq("i32")
260 expect(@prot.get_type_name_for_type_id(Thrift::Types::I64)).to eq("i64")
261 expect(@prot.get_type_name_for_type_id(Thrift::Types::STRING)).to eq("str")
262 expect(@prot.get_type_name_for_type_id(Thrift::Types::STRUCT)).to eq("rec")
263 expect(@prot.get_type_name_for_type_id(Thrift::Types::MAP)).to eq("map")
264 expect(@prot.get_type_name_for_type_id(Thrift::Types::SET)).to eq("set")
265 expect(@prot.get_type_name_for_type_id(Thrift::Types::LIST)).to eq("lst")
Jake Farrella87810f2012-09-28 01:59:04 +0000266 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000267
Jake Farrella87810f2012-09-28 01:59:04 +0000268 it "should get type id for type name" do
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400269 expect { @prot.get_type_id_for_type_name("pp") }.to raise_error(NotImplementedError)
James E. King III27247072018-03-22 20:50:23 -0400270 expect(@prot.get_type_id_for_type_name("tf")).to eq(Thrift::Types::BOOL)
271 expect(@prot.get_type_id_for_type_name("i8")).to eq(Thrift::Types::BYTE)
272 expect(@prot.get_type_id_for_type_name("dbl")).to eq(Thrift::Types::DOUBLE)
273 expect(@prot.get_type_id_for_type_name("i16")).to eq(Thrift::Types::I16)
274 expect(@prot.get_type_id_for_type_name("i32")).to eq(Thrift::Types::I32)
275 expect(@prot.get_type_id_for_type_name("i64")).to eq(Thrift::Types::I64)
276 expect(@prot.get_type_id_for_type_name("str")).to eq(Thrift::Types::STRING)
277 expect(@prot.get_type_id_for_type_name("rec")).to eq(Thrift::Types::STRUCT)
278 expect(@prot.get_type_id_for_type_name("map")).to eq(Thrift::Types::MAP)
279 expect(@prot.get_type_id_for_type_name("set")).to eq(Thrift::Types::SET)
280 expect(@prot.get_type_id_for_type_name("lst")).to eq(Thrift::Types::LIST)
Jake Farrella87810f2012-09-28 01:59:04 +0000281 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000282
Jake Farrella87810f2012-09-28 01:59:04 +0000283 it "should read json syntax char" do
284 @trans.write('F')
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400285 expect { @prot.read_json_syntax_char('G') }.to raise_error(Thrift::ProtocolException)
Jake Farrella87810f2012-09-28 01:59:04 +0000286 @trans.write('H')
287 @prot.read_json_syntax_char('H')
288 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000289
Jake Farrella87810f2012-09-28 01:59:04 +0000290 it "should read json escape char" do
291 @trans.write('0054')
James E. King III27247072018-03-22 20:50:23 -0400292 expect(@prot.read_json_escape_char).to eq('T')
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900293
294 @trans.write("\"\\\"\"")
James E. King III27247072018-03-22 20:50:23 -0400295 expect(@prot.read_json_string(false)).to eq("\"")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900296
297 @trans.write("\"\\\\\"")
James E. King III27247072018-03-22 20:50:23 -0400298 expect(@prot.read_json_string(false)).to eq("\\")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900299
300 @trans.write("\"\\/\"")
James E. King III27247072018-03-22 20:50:23 -0400301 expect(@prot.read_json_string(false)).to eq("\/")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900302
303 @trans.write("\"\\b\"")
James E. King III27247072018-03-22 20:50:23 -0400304 expect(@prot.read_json_string(false)).to eq("\b")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900305
306 @trans.write("\"\\f\"")
James E. King III27247072018-03-22 20:50:23 -0400307 expect(@prot.read_json_string(false)).to eq("\f")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900308
309 @trans.write("\"\\n\"")
James E. King III27247072018-03-22 20:50:23 -0400310 expect(@prot.read_json_string(false)).to eq("\n")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900311
312 @trans.write("\"\\r\"")
James E. King III27247072018-03-22 20:50:23 -0400313 expect(@prot.read_json_string(false)).to eq("\r")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900314
315 @trans.write("\"\\t\"")
James E. King III27247072018-03-22 20:50:23 -0400316 expect(@prot.read_json_string(false)).to eq("\t")
Jake Farrella87810f2012-09-28 01:59:04 +0000317 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000318
Jake Farrella87810f2012-09-28 01:59:04 +0000319 it "should read json string" do
320 @trans.write("\"\\P")
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400321 expect { @prot.read_json_string(false) }.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000322
Jake Farrella87810f2012-09-28 01:59:04 +0000323 @trans.write("\"this is a test string\"")
James E. King III27247072018-03-22 20:50:23 -0400324 expect(@prot.read_json_string).to eq("this is a test string")
Jake Farrella87810f2012-09-28 01:59:04 +0000325 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000326
Jake Farrella87810f2012-09-28 01:59:04 +0000327 it "should read json base64" do
328 @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
James E. King III27247072018-03-22 20:50:23 -0400329 expect(@prot.read_json_base64).to eq("this is a test string")
Jake Farrella87810f2012-09-28 01:59:04 +0000330 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000331
Jake Farrella87810f2012-09-28 01:59:04 +0000332 it "should is json numeric" do
James E. King III27247072018-03-22 20:50:23 -0400333 expect(@prot.is_json_numeric("A")).to eq(false)
334 expect(@prot.is_json_numeric("+")).to eq(true)
335 expect(@prot.is_json_numeric("-")).to eq(true)
336 expect(@prot.is_json_numeric(".")).to eq(true)
337 expect(@prot.is_json_numeric("0")).to eq(true)
338 expect(@prot.is_json_numeric("1")).to eq(true)
339 expect(@prot.is_json_numeric("2")).to eq(true)
340 expect(@prot.is_json_numeric("3")).to eq(true)
341 expect(@prot.is_json_numeric("4")).to eq(true)
342 expect(@prot.is_json_numeric("5")).to eq(true)
343 expect(@prot.is_json_numeric("6")).to eq(true)
344 expect(@prot.is_json_numeric("7")).to eq(true)
345 expect(@prot.is_json_numeric("8")).to eq(true)
346 expect(@prot.is_json_numeric("9")).to eq(true)
347 expect(@prot.is_json_numeric("E")).to eq(true)
348 expect(@prot.is_json_numeric("e")).to eq(true)
Jake Farrella87810f2012-09-28 01:59:04 +0000349 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000350
Jake Farrella87810f2012-09-28 01:59:04 +0000351 it "should read json numeric chars" do
352 @trans.write("1.453E45T")
James E. King III27247072018-03-22 20:50:23 -0400353 expect(@prot.read_json_numeric_chars).to eq("1.453E45")
Jake Farrella87810f2012-09-28 01:59:04 +0000354 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000355
Jake Farrella87810f2012-09-28 01:59:04 +0000356 it "should read json integer" do
357 @trans.write("1.45\"\"")
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400358 expect { @prot.read_json_integer }.to raise_error(Thrift::ProtocolException)
Jake Farrella87810f2012-09-28 01:59:04 +0000359 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000360
Jake Farrella87810f2012-09-28 01:59:04 +0000361 @trans.write("1453T")
James E. King III27247072018-03-22 20:50:23 -0400362 expect(@prot.read_json_integer).to eq(1453)
Jake Farrella87810f2012-09-28 01:59:04 +0000363 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000364
Jake Farrella87810f2012-09-28 01:59:04 +0000365 it "should read json double" do
366 @trans.write("1.45e3e01\"\"")
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400367 expect { @prot.read_json_double }.to raise_error(Thrift::ProtocolException)
Jake Farrella87810f2012-09-28 01:59:04 +0000368 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000369
Jake Farrella87810f2012-09-28 01:59:04 +0000370 @trans.write("\"1.453e01\"")
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400371 expect { @prot.read_json_double }.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000372
Jake Farrella87810f2012-09-28 01:59:04 +0000373 @trans.write("1.453e01\"\"")
James E. King III27247072018-03-22 20:50:23 -0400374 expect(@prot.read_json_double).to eq(14.53)
Jake Farrella87810f2012-09-28 01:59:04 +0000375 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000376
Jake Farrella87810f2012-09-28 01:59:04 +0000377 @trans.write("\"NaN\"")
James E. King III27247072018-03-22 20:50:23 -0400378 expect(@prot.read_json_double.nan?).to eq(true)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000379
Jake Farrella87810f2012-09-28 01:59:04 +0000380 @trans.write("\"Infinity\"")
James E. King III27247072018-03-22 20:50:23 -0400381 expect(@prot.read_json_double).to eq(+1.0/0.0)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000382
Jake Farrella87810f2012-09-28 01:59:04 +0000383 @trans.write("\"-Infinity\"")
James E. King III27247072018-03-22 20:50:23 -0400384 expect(@prot.read_json_double).to eq(-1.0/0.0)
Jake Farrella87810f2012-09-28 01:59:04 +0000385 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000386
Jake Farrella87810f2012-09-28 01:59:04 +0000387 it "should read json object start" do
388 @trans.write("{")
James E. King III27247072018-03-22 20:50:23 -0400389 expect(@prot.read_json_object_start).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000390 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000391
Jake Farrella87810f2012-09-28 01:59:04 +0000392 it "should read json object end" do
393 @trans.write("}")
James E. King III27247072018-03-22 20:50:23 -0400394 expect(@prot.read_json_object_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000395 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000396
Jake Farrella87810f2012-09-28 01:59:04 +0000397 it "should read json array start" do
398 @trans.write("[")
James E. King III27247072018-03-22 20:50:23 -0400399 expect(@prot.read_json_array_start).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000400 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000401
Jake Farrella87810f2012-09-28 01:59:04 +0000402 it "should read json array end" do
403 @trans.write("]")
James E. King III27247072018-03-22 20:50:23 -0400404 expect(@prot.read_json_array_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000405 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000406
Jake Farrella87810f2012-09-28 01:59:04 +0000407 it "should read_message_begin" do
408 @trans.write("[2,")
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400409 expect { @prot.read_message_begin }.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000410
Jake Farrella87810f2012-09-28 01:59:04 +0000411 @trans.write("[1,\"name\",12,32\"\"")
James E. King III27247072018-03-22 20:50:23 -0400412 expect(@prot.read_message_begin).to eq(["name", 12, 32])
Jake Farrella87810f2012-09-28 01:59:04 +0000413 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000414
Jake Farrella87810f2012-09-28 01:59:04 +0000415 it "should read message end" do
416 @trans.write("]")
James E. King III27247072018-03-22 20:50:23 -0400417 expect(@prot.read_message_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000418 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000419
Jake Farrella87810f2012-09-28 01:59:04 +0000420 it "should read struct begin" do
421 @trans.write("{")
James E. King III27247072018-03-22 20:50:23 -0400422 expect(@prot.read_struct_begin).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000423 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000424
Jake Farrella87810f2012-09-28 01:59:04 +0000425 it "should read struct end" do
426 @trans.write("}")
James E. King III27247072018-03-22 20:50:23 -0400427 expect(@prot.read_struct_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000428 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000429
Jake Farrella87810f2012-09-28 01:59:04 +0000430 it "should read field begin" do
431 @trans.write("1{\"rec\"")
James E. King III27247072018-03-22 20:50:23 -0400432 expect(@prot.read_field_begin).to eq([nil, 12, 1])
Jake Farrella87810f2012-09-28 01:59:04 +0000433 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000434
Jake Farrella87810f2012-09-28 01:59:04 +0000435 it "should read field end" do
436 @trans.write("}")
James E. King III27247072018-03-22 20:50:23 -0400437 expect(@prot.read_field_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000438 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000439
Jake Farrella87810f2012-09-28 01:59:04 +0000440 it "should read map begin" do
441 @trans.write("[\"rec\",\"lst\",2,{")
James E. King III27247072018-03-22 20:50:23 -0400442 expect(@prot.read_map_begin).to eq([12, 15, 2])
Jake Farrella87810f2012-09-28 01:59:04 +0000443 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000444
Jake Farrella87810f2012-09-28 01:59:04 +0000445 it "should read map end" do
446 @trans.write("}]")
James E. King III27247072018-03-22 20:50:23 -0400447 expect(@prot.read_map_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000448 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000449
Jake Farrella87810f2012-09-28 01:59:04 +0000450 it "should read list begin" do
451 @trans.write("[\"rec\",2\"\"")
James E. King III27247072018-03-22 20:50:23 -0400452 expect(@prot.read_list_begin).to eq([12, 2])
Jake Farrella87810f2012-09-28 01:59:04 +0000453 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000454
Jake Farrella87810f2012-09-28 01:59:04 +0000455 it "should read list end" do
456 @trans.write("]")
James E. King III27247072018-03-22 20:50:23 -0400457 expect(@prot.read_list_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000458 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000459
Jake Farrella87810f2012-09-28 01:59:04 +0000460 it "should read set begin" do
Roger Meier772b2b12013-01-19 21:04:12 +0100461 @trans.write("[\"rec\",2\"\"")
James E. King III27247072018-03-22 20:50:23 -0400462 expect(@prot.read_set_begin).to eq([12, 2])
Jake Farrella87810f2012-09-28 01:59:04 +0000463 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000464
Jake Farrella87810f2012-09-28 01:59:04 +0000465 it "should read set end" do
466 @trans.write("]")
James E. King III27247072018-03-22 20:50:23 -0400467 expect(@prot.read_set_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000468 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000469
Jake Farrella87810f2012-09-28 01:59:04 +0000470 it "should read bool" do
471 @trans.write("0\"\"")
James E. King III27247072018-03-22 20:50:23 -0400472 expect(@prot.read_bool).to eq(false)
Jake Farrella87810f2012-09-28 01:59:04 +0000473 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000474
Jake Farrella87810f2012-09-28 01:59:04 +0000475 @trans.write("1\"\"")
James E. King III27247072018-03-22 20:50:23 -0400476 expect(@prot.read_bool).to eq(true)
Jake Farrella87810f2012-09-28 01:59:04 +0000477 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000478
Jake Farrella87810f2012-09-28 01:59:04 +0000479 it "should read byte" do
480 @trans.write("60\"\"")
James E. King III27247072018-03-22 20:50:23 -0400481 expect(@prot.read_byte).to eq(60)
Jake Farrella87810f2012-09-28 01:59:04 +0000482 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000483
Jake Farrella87810f2012-09-28 01:59:04 +0000484 it "should read i16" do
485 @trans.write("1000\"\"")
James E. King III27247072018-03-22 20:50:23 -0400486 expect(@prot.read_i16).to eq(1000)
Jake Farrella87810f2012-09-28 01:59:04 +0000487 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000488
Jake Farrella87810f2012-09-28 01:59:04 +0000489 it "should read i32" do
490 @trans.write("3000000000\"\"")
James E. King III27247072018-03-22 20:50:23 -0400491 expect(@prot.read_i32).to eq(3000000000)
Jake Farrella87810f2012-09-28 01:59:04 +0000492 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000493
Jake Farrella87810f2012-09-28 01:59:04 +0000494 it "should read i64" do
495 @trans.write("6000000000\"\"")
James E. King III27247072018-03-22 20:50:23 -0400496 expect(@prot.read_i64).to eq(6000000000)
Jake Farrella87810f2012-09-28 01:59:04 +0000497 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000498
Jake Farrella87810f2012-09-28 01:59:04 +0000499 it "should read double" do
500 @trans.write("12.23\"\"")
James E. King III27247072018-03-22 20:50:23 -0400501 expect(@prot.read_double).to eq(12.23)
Jake Farrella87810f2012-09-28 01:59:04 +0000502 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000503
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -0500504 it 'should read string' do
505 @trans.write('"this is a test string"'.force_encoding(Encoding::BINARY))
506 a = @prot.read_string
507 expect(a).to eq('this is a test string')
508 expect(a.encoding).to eq(Encoding::UTF_8)
509 end
Jake Farrellb5a18a12012-10-09 01:10:43 +0000510
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -0500511 it 'should read string with unicode characters' do
512 @trans.write('"this is a test string with unicode characters: \u20AC \u20AD"'.force_encoding(Encoding::BINARY))
513 a = @prot.read_string
514 expect(a).to eq("this is a test string with unicode characters: \u20AC \u20AD")
515 expect(a.encoding).to eq(Encoding::UTF_8)
Jake Farrella87810f2012-09-28 01:59:04 +0000516 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000517
Jake Farrella87810f2012-09-28 01:59:04 +0000518 it "should read binary" do
519 @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
James E. King III27247072018-03-22 20:50:23 -0400520 expect(@prot.read_binary).to eq("this is a test string")
Jake Farrella87810f2012-09-28 01:59:04 +0000521 end
Jens Geyer123258b2015-10-02 00:38:17 +0200522
523 it "should read long binary" do
524 @trans.write("\"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==\"")
James E. King III27247072018-03-22 20:50:23 -0400525 expect(@prot.read_binary.bytes.to_a).to eq((0...256).to_a)
Jens Geyer123258b2015-10-02 00:38:17 +0200526 end
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500527
528 it "should read a uuid" do
529 @trans.write("\"00112233-4455-6677-8899-aabbccddeeff\"")
530 expect(@prot.read_uuid).to eq("00112233-4455-6677-8899-aabbccddeeff")
531 end
532
533 it "should normalize uppercase uuid on read" do
534 @trans.write("\"00112233-4455-6677-8899-AABBCCDDEEFF\"")
535 expect(@prot.read_uuid).to eq("00112233-4455-6677-8899-aabbccddeeff")
536 end
537
James E. King III9aaf2952018-03-20 15:06:08 -0400538 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -0400539 expect(@prot.to_s).to eq("json(memory)")
James E. King III9aaf2952018-03-20 15:06:08 -0400540 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000541 end
542
Jake Farrella87810f2012-09-28 01:59:04 +0000543 describe Thrift::JsonProtocolFactory do
Jake Farrell6f0f5272012-01-31 03:39:30 +0000544 it "should create a JsonProtocol" do
James E. King III27247072018-03-22 20:50:23 -0400545 expect(Thrift::JsonProtocolFactory.new.get_protocol(double("MockTransport"))).to be_instance_of(Thrift::JsonProtocol)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000546 end
James E. King III9aaf2952018-03-20 15:06:08 -0400547
548 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -0400549 expect(Thrift::JsonProtocolFactory.new.to_s).to eq("json")
James E. King III9aaf2952018-03-20 15:06:08 -0400550 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000551 end
552end