blob: fe1af7bb2eab695ce79e5c3b21de9ba5eb252c2e [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 Farrell6f0f5272012-01-31 03:39:30 +000024
Jake Farrella87810f2012-09-28 01:59:04 +000025 describe Thrift::JsonProtocol do
26 before(:each) do
27 @trans = Thrift::MemoryBufferTransport.new
28 @prot = Thrift::JsonProtocol.new(@trans)
29 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000030
Jake Farrella87810f2012-09-28 01:59:04 +000031 it "should write json escaped char" do
32 @prot.write_json_escape_char("\n")
James E. King III27247072018-03-22 20:50:23 -040033 expect(@trans.read(@trans.available)).to eq('\u000a')
Jake Farrell6f0f5272012-01-31 03:39:30 +000034
Jake Farrella87810f2012-09-28 01:59:04 +000035 @prot.write_json_escape_char(" ")
James E. King III27247072018-03-22 20:50:23 -040036 expect(@trans.read(@trans.available)).to eq('\u0020')
Jake Farrella87810f2012-09-28 01:59:04 +000037 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000038
Jake Farrella87810f2012-09-28 01:59:04 +000039 it "should write json char" do
40 @prot.write_json_char("\n")
James E. King III27247072018-03-22 20:50:23 -040041 expect(@trans.read(@trans.available)).to eq('\\n')
Jake Farrell6f0f5272012-01-31 03:39:30 +000042
Jake Farrella87810f2012-09-28 01:59:04 +000043 @prot.write_json_char(" ")
James E. King III27247072018-03-22 20:50:23 -040044 expect(@trans.read(@trans.available)).to eq(' ')
Jake Farrell6f0f5272012-01-31 03:39:30 +000045
Jake Farrella87810f2012-09-28 01:59:04 +000046 @prot.write_json_char("\\")
James E. King III27247072018-03-22 20:50:23 -040047 expect(@trans.read(@trans.available)).to eq("\\\\")
Jake Farrell6f0f5272012-01-31 03:39:30 +000048
Jake Farrella87810f2012-09-28 01:59:04 +000049 @prot.write_json_char("@")
James E. King III27247072018-03-22 20:50:23 -040050 expect(@trans.read(@trans.available)).to eq('@')
Jake Farrella87810f2012-09-28 01:59:04 +000051 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000052
Jake Farrella87810f2012-09-28 01:59:04 +000053 it "should write json string" do
54 @prot.write_json_string("this is a \\ json\nstring")
James E. King III27247072018-03-22 20:50:23 -040055 expect(@trans.read(@trans.available)).to eq("\"this is a \\\\ json\\nstring\"")
Jake Farrella87810f2012-09-28 01:59:04 +000056 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000057
Jake Farrella87810f2012-09-28 01:59:04 +000058 it "should write json base64" do
59 @prot.write_json_base64("this is a base64 string")
James E. King III27247072018-03-22 20:50:23 -040060 expect(@trans.read(@trans.available)).to eq("\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\"")
Jake Farrella87810f2012-09-28 01:59:04 +000061 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000062
Jake Farrella87810f2012-09-28 01:59:04 +000063 it "should write json integer" do
64 @prot.write_json_integer(45)
James E. King III27247072018-03-22 20:50:23 -040065 expect(@trans.read(@trans.available)).to eq("45")
Jake Farrell6f0f5272012-01-31 03:39:30 +000066
Jake Farrella87810f2012-09-28 01:59:04 +000067 @prot.write_json_integer(33000)
James E. King III27247072018-03-22 20:50:23 -040068 expect(@trans.read(@trans.available)).to eq("33000")
Jake Farrell6f0f5272012-01-31 03:39:30 +000069
Jake Farrella87810f2012-09-28 01:59:04 +000070 @prot.write_json_integer(3000000000)
James E. King III27247072018-03-22 20:50:23 -040071 expect(@trans.read(@trans.available)).to eq("3000000000")
Jake Farrell6f0f5272012-01-31 03:39:30 +000072
Jake Farrella87810f2012-09-28 01:59:04 +000073 @prot.write_json_integer(6000000000)
James E. King III27247072018-03-22 20:50:23 -040074 expect(@trans.read(@trans.available)).to eq("6000000000")
Jake Farrella87810f2012-09-28 01:59:04 +000075 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000076
Jake Farrella87810f2012-09-28 01:59:04 +000077 it "should write json double" do
78 @prot.write_json_double(12.3)
James E. King III27247072018-03-22 20:50:23 -040079 expect(@trans.read(@trans.available)).to eq("12.3")
Jake Farrell6f0f5272012-01-31 03:39:30 +000080
Jake Farrella87810f2012-09-28 01:59:04 +000081 @prot.write_json_double(-3.21)
James E. King III27247072018-03-22 20:50:23 -040082 expect(@trans.read(@trans.available)).to eq("-3.21")
Jake Farrell6f0f5272012-01-31 03:39:30 +000083
Jake Farrella87810f2012-09-28 01:59:04 +000084 @prot.write_json_double(((+1.0/0.0)/(+1.0/0.0)))
James E. King III27247072018-03-22 20:50:23 -040085 expect(@trans.read(@trans.available)).to eq("\"NaN\"")
Jake Farrell6f0f5272012-01-31 03:39:30 +000086
Jake Farrella87810f2012-09-28 01:59:04 +000087 @prot.write_json_double((+1.0/0.0))
James E. King III27247072018-03-22 20:50:23 -040088 expect(@trans.read(@trans.available)).to eq("\"Infinity\"")
Jake Farrell6f0f5272012-01-31 03:39:30 +000089
Jake Farrella87810f2012-09-28 01:59:04 +000090 @prot.write_json_double((-1.0/0.0))
James E. King III27247072018-03-22 20:50:23 -040091 expect(@trans.read(@trans.available)).to eq("\"-Infinity\"")
Jake Farrella87810f2012-09-28 01:59:04 +000092 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000093
Jake Farrella87810f2012-09-28 01:59:04 +000094 it "should write json object start" do
95 @prot.write_json_object_start
James E. King III27247072018-03-22 20:50:23 -040096 expect(@trans.read(@trans.available)).to eq("{")
Jake Farrella87810f2012-09-28 01:59:04 +000097 end
Jake Farrell6f0f5272012-01-31 03:39:30 +000098
Jake Farrella87810f2012-09-28 01:59:04 +000099 it "should write json object end" do
100 @prot.write_json_object_end
James E. King III27247072018-03-22 20:50:23 -0400101 expect(@trans.read(@trans.available)).to eq("}")
Jake Farrella87810f2012-09-28 01:59:04 +0000102 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000103
Jake Farrella87810f2012-09-28 01:59:04 +0000104 it "should write json array start" do
105 @prot.write_json_array_start
James E. King III27247072018-03-22 20:50:23 -0400106 expect(@trans.read(@trans.available)).to eq("[")
Jake Farrella87810f2012-09-28 01:59:04 +0000107 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000108
Jake Farrella87810f2012-09-28 01:59:04 +0000109 it "should write json array end" do
110 @prot.write_json_array_end
James E. King III27247072018-03-22 20:50:23 -0400111 expect(@trans.read(@trans.available)).to eq("]")
Jake Farrella87810f2012-09-28 01:59:04 +0000112 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000113
Jake Farrella87810f2012-09-28 01:59:04 +0000114 it "should write message begin" do
115 @prot.write_message_begin("name", 12, 32)
James E. King III27247072018-03-22 20:50:23 -0400116 expect(@trans.read(@trans.available)).to eq("[1,\"name\",12,32")
Jake Farrella87810f2012-09-28 01:59:04 +0000117 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000118
Jake Farrella87810f2012-09-28 01:59:04 +0000119 it "should write message end" do
120 @prot.write_message_end
James E. King III27247072018-03-22 20:50:23 -0400121 expect(@trans.read(@trans.available)).to eq("]")
Jake Farrella87810f2012-09-28 01:59:04 +0000122 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000123
Jake Farrella87810f2012-09-28 01:59:04 +0000124 it "should write struct begin" do
125 @prot.write_struct_begin("name")
James E. King III27247072018-03-22 20:50:23 -0400126 expect(@trans.read(@trans.available)).to eq("{")
Jake Farrella87810f2012-09-28 01:59:04 +0000127 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000128
Jake Farrella87810f2012-09-28 01:59:04 +0000129 it "should write struct end" do
130 @prot.write_struct_end
James E. King III27247072018-03-22 20:50:23 -0400131 expect(@trans.read(@trans.available)).to eq("}")
Jake Farrella87810f2012-09-28 01:59:04 +0000132 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000133
Jake Farrella87810f2012-09-28 01:59:04 +0000134 it "should write field begin" do
135 @prot.write_field_begin("name", Thrift::Types::STRUCT, 32)
James E. King III27247072018-03-22 20:50:23 -0400136 expect(@trans.read(@trans.available)).to eq("32{\"rec\"")
Jake Farrella87810f2012-09-28 01:59:04 +0000137 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000138
Jake Farrella87810f2012-09-28 01:59:04 +0000139 it "should write field end" do
140 @prot.write_field_end
James E. King III27247072018-03-22 20:50:23 -0400141 expect(@trans.read(@trans.available)).to eq("}")
Jake Farrella87810f2012-09-28 01:59:04 +0000142 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000143
Jake Farrella87810f2012-09-28 01:59:04 +0000144 it "should write field stop" do
145 @prot.write_field_stop
James E. King III27247072018-03-22 20:50:23 -0400146 expect(@trans.read(@trans.available)).to eq("")
Jake Farrella87810f2012-09-28 01:59:04 +0000147 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000148
Jake Farrella87810f2012-09-28 01:59:04 +0000149 it "should write map begin" do
150 @prot.write_map_begin(Thrift::Types::STRUCT, Thrift::Types::LIST, 32)
James E. King III27247072018-03-22 20:50:23 -0400151 expect(@trans.read(@trans.available)).to eq("[\"rec\",\"lst\",32,{")
Jake Farrella87810f2012-09-28 01:59:04 +0000152 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000153
Jake Farrella87810f2012-09-28 01:59:04 +0000154 it "should write map end" do
155 @prot.write_map_end
James E. King III27247072018-03-22 20:50:23 -0400156 expect(@trans.read(@trans.available)).to eq("}]")
Jake Farrella87810f2012-09-28 01:59:04 +0000157 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000158
Jake Farrella87810f2012-09-28 01:59:04 +0000159 it "should write list begin" do
160 @prot.write_list_begin(Thrift::Types::STRUCT, 32)
James E. King III27247072018-03-22 20:50:23 -0400161 expect(@trans.read(@trans.available)).to eq("[\"rec\",32")
Jake Farrella87810f2012-09-28 01:59:04 +0000162 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000163
Jake Farrella87810f2012-09-28 01:59:04 +0000164 it "should write list end" do
165 @prot.write_list_end
James E. King III27247072018-03-22 20:50:23 -0400166 expect(@trans.read(@trans.available)).to eq("]")
Jake Farrella87810f2012-09-28 01:59:04 +0000167 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000168
Jake Farrella87810f2012-09-28 01:59:04 +0000169 it "should write set begin" do
170 @prot.write_set_begin(Thrift::Types::STRUCT, 32)
James E. King III27247072018-03-22 20:50:23 -0400171 expect(@trans.read(@trans.available)).to eq("[\"rec\",32")
Jake Farrella87810f2012-09-28 01:59:04 +0000172 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000173
Jake Farrella87810f2012-09-28 01:59:04 +0000174 it "should write set end" do
175 @prot.write_set_end
James E. King III27247072018-03-22 20:50:23 -0400176 expect(@trans.read(@trans.available)).to eq("]")
Jake Farrella87810f2012-09-28 01:59:04 +0000177 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000178
Jake Farrella87810f2012-09-28 01:59:04 +0000179 it "should write bool" do
180 @prot.write_bool(true)
James E. King III27247072018-03-22 20:50:23 -0400181 expect(@trans.read(@trans.available)).to eq("1")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000182
Jake Farrella87810f2012-09-28 01:59:04 +0000183 @prot.write_bool(false)
James E. King III27247072018-03-22 20:50:23 -0400184 expect(@trans.read(@trans.available)).to eq("0")
Jake Farrella87810f2012-09-28 01:59:04 +0000185 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000186
Jake Farrella87810f2012-09-28 01:59:04 +0000187 it "should write byte" do
188 @prot.write_byte(100)
James E. King III27247072018-03-22 20:50:23 -0400189 expect(@trans.read(@trans.available)).to eq("100")
Jake Farrella87810f2012-09-28 01:59:04 +0000190 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000191
Jake Farrella87810f2012-09-28 01:59:04 +0000192 it "should write i16" do
193 @prot.write_i16(1000)
James E. King III27247072018-03-22 20:50:23 -0400194 expect(@trans.read(@trans.available)).to eq("1000")
Jake Farrella87810f2012-09-28 01:59:04 +0000195 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000196
Jake Farrella87810f2012-09-28 01:59:04 +0000197 it "should write i32" do
198 @prot.write_i32(3000000000)
James E. King III27247072018-03-22 20:50:23 -0400199 expect(@trans.read(@trans.available)).to eq("3000000000")
Jake Farrella87810f2012-09-28 01:59:04 +0000200 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000201
Jake Farrella87810f2012-09-28 01:59:04 +0000202 it "should write i64" do
203 @prot.write_i64(6000000000)
James E. King III27247072018-03-22 20:50:23 -0400204 expect(@trans.read(@trans.available)).to eq("6000000000")
Jake Farrella87810f2012-09-28 01:59:04 +0000205 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000206
Jake Farrella87810f2012-09-28 01:59:04 +0000207 it "should write double" do
208 @prot.write_double(1.23)
James E. King III27247072018-03-22 20:50:23 -0400209 expect(@trans.read(@trans.available)).to eq("1.23")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000210
Jake Farrella87810f2012-09-28 01:59:04 +0000211 @prot.write_double(-32.1)
James E. King III27247072018-03-22 20:50:23 -0400212 expect(@trans.read(@trans.available)).to eq("-32.1")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000213
Jake Farrella87810f2012-09-28 01:59:04 +0000214 @prot.write_double(((+1.0/0.0)/(+1.0/0.0)))
James E. King III27247072018-03-22 20:50:23 -0400215 expect(@trans.read(@trans.available)).to eq("\"NaN\"")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000216
Jake Farrella87810f2012-09-28 01:59:04 +0000217 @prot.write_double((+1.0/0.0))
James E. King III27247072018-03-22 20:50:23 -0400218 expect(@trans.read(@trans.available)).to eq("\"Infinity\"")
Jake Farrell6f0f5272012-01-31 03:39:30 +0000219
Jake Farrella87810f2012-09-28 01:59:04 +0000220 @prot.write_double((-1.0/0.0))
James E. King III27247072018-03-22 20:50:23 -0400221 expect(@trans.read(@trans.available)).to eq("\"-Infinity\"")
Jake Farrella87810f2012-09-28 01:59:04 +0000222 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000223
Jake Farrellb5a18a12012-10-09 01:10:43 +0000224 if RUBY_VERSION >= '1.9'
225 it 'should write string' do
226 @prot.write_string('this is a test string')
227 a = @trans.read(@trans.available)
James E. King III27247072018-03-22 20:50:23 -0400228 expect(a).to eq('"this is a test string"'.force_encoding(Encoding::BINARY))
229 expect(a.encoding).to eq(Encoding::BINARY)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000230 end
231
232 it 'should write string with unicode characters' do
233 @prot.write_string("this is a test string with unicode characters: \u20AC \u20AD")
234 a = @trans.read(@trans.available)
James E. King III27247072018-03-22 20:50:23 -0400235 expect(a).to eq("\"this is a test string with unicode characters: \u20AC \u20AD\"".force_encoding(Encoding::BINARY))
236 expect(a.encoding).to eq(Encoding::BINARY)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000237 end
238 else
239 it 'should write string' do
240 @prot.write_string('this is a test string')
James E. King III27247072018-03-22 20:50:23 -0400241 expect(@trans.read(@trans.available)).to eq('"this is a test string"')
Jake Farrellb5a18a12012-10-09 01:10:43 +0000242 end
Jake Farrella87810f2012-09-28 01:59:04 +0000243 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000244
Jake Farrella87810f2012-09-28 01:59:04 +0000245 it "should write binary" do
246 @prot.write_binary("this is a base64 string")
James E. King III27247072018-03-22 20:50:23 -0400247 expect(@trans.read(@trans.available)).to eq("\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\"")
Jens Geyer123258b2015-10-02 00:38:17 +0200248 end
249
250 it "should write long binary" do
251 @prot.write_binary((0...256).to_a.pack('C*'))
James E. King III27247072018-03-22 20:50:23 -0400252 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 +0000253 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000254
Jake Farrella87810f2012-09-28 01:59:04 +0000255 it "should get type name for type id" do
256 expect {@prot.get_type_name_for_type_id(Thrift::Types::STOP)}.to raise_error(NotImplementedError)
257 expect {@prot.get_type_name_for_type_id(Thrift::Types::VOID)}.to raise_error(NotImplementedError)
James E. King III27247072018-03-22 20:50:23 -0400258 expect(@prot.get_type_name_for_type_id(Thrift::Types::BOOL)).to eq("tf")
259 expect(@prot.get_type_name_for_type_id(Thrift::Types::BYTE)).to eq("i8")
260 expect(@prot.get_type_name_for_type_id(Thrift::Types::DOUBLE)).to eq("dbl")
261 expect(@prot.get_type_name_for_type_id(Thrift::Types::I16)).to eq("i16")
262 expect(@prot.get_type_name_for_type_id(Thrift::Types::I32)).to eq("i32")
263 expect(@prot.get_type_name_for_type_id(Thrift::Types::I64)).to eq("i64")
264 expect(@prot.get_type_name_for_type_id(Thrift::Types::STRING)).to eq("str")
265 expect(@prot.get_type_name_for_type_id(Thrift::Types::STRUCT)).to eq("rec")
266 expect(@prot.get_type_name_for_type_id(Thrift::Types::MAP)).to eq("map")
267 expect(@prot.get_type_name_for_type_id(Thrift::Types::SET)).to eq("set")
268 expect(@prot.get_type_name_for_type_id(Thrift::Types::LIST)).to eq("lst")
Jake Farrella87810f2012-09-28 01:59:04 +0000269 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000270
Jake Farrella87810f2012-09-28 01:59:04 +0000271 it "should get type id for type name" do
272 expect {@prot.get_type_id_for_type_name("pp")}.to raise_error(NotImplementedError)
James E. King III27247072018-03-22 20:50:23 -0400273 expect(@prot.get_type_id_for_type_name("tf")).to eq(Thrift::Types::BOOL)
274 expect(@prot.get_type_id_for_type_name("i8")).to eq(Thrift::Types::BYTE)
275 expect(@prot.get_type_id_for_type_name("dbl")).to eq(Thrift::Types::DOUBLE)
276 expect(@prot.get_type_id_for_type_name("i16")).to eq(Thrift::Types::I16)
277 expect(@prot.get_type_id_for_type_name("i32")).to eq(Thrift::Types::I32)
278 expect(@prot.get_type_id_for_type_name("i64")).to eq(Thrift::Types::I64)
279 expect(@prot.get_type_id_for_type_name("str")).to eq(Thrift::Types::STRING)
280 expect(@prot.get_type_id_for_type_name("rec")).to eq(Thrift::Types::STRUCT)
281 expect(@prot.get_type_id_for_type_name("map")).to eq(Thrift::Types::MAP)
282 expect(@prot.get_type_id_for_type_name("set")).to eq(Thrift::Types::SET)
283 expect(@prot.get_type_id_for_type_name("lst")).to eq(Thrift::Types::LIST)
Jake Farrella87810f2012-09-28 01:59:04 +0000284 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000285
Jake Farrella87810f2012-09-28 01:59:04 +0000286 it "should read json syntax char" do
287 @trans.write('F')
288 expect {@prot.read_json_syntax_char('G')}.to raise_error(Thrift::ProtocolException)
289 @trans.write('H')
290 @prot.read_json_syntax_char('H')
291 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000292
Jake Farrella87810f2012-09-28 01:59:04 +0000293 it "should read json escape char" do
294 @trans.write('0054')
James E. King III27247072018-03-22 20:50:23 -0400295 expect(@prot.read_json_escape_char).to eq('T')
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("\"\\/\"")
James E. King III27247072018-03-22 20:50:23 -0400304 expect(@prot.read_json_string(false)).to eq("\/")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900305
306 @trans.write("\"\\b\"")
James E. King III27247072018-03-22 20:50:23 -0400307 expect(@prot.read_json_string(false)).to eq("\b")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900308
309 @trans.write("\"\\f\"")
James E. King III27247072018-03-22 20:50:23 -0400310 expect(@prot.read_json_string(false)).to eq("\f")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900311
312 @trans.write("\"\\n\"")
James E. King III27247072018-03-22 20:50:23 -0400313 expect(@prot.read_json_string(false)).to eq("\n")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900314
315 @trans.write("\"\\r\"")
James E. King III27247072018-03-22 20:50:23 -0400316 expect(@prot.read_json_string(false)).to eq("\r")
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900317
318 @trans.write("\"\\t\"")
James E. King III27247072018-03-22 20:50:23 -0400319 expect(@prot.read_json_string(false)).to eq("\t")
Jake Farrella87810f2012-09-28 01:59:04 +0000320 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000321
Jake Farrella87810f2012-09-28 01:59:04 +0000322 it "should read json string" do
323 @trans.write("\"\\P")
324 expect {@prot.read_json_string(false)}.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000325
Jake Farrella87810f2012-09-28 01:59:04 +0000326 @trans.write("\"this is a test string\"")
James E. King III27247072018-03-22 20:50:23 -0400327 expect(@prot.read_json_string).to eq("this is a test string")
Jake Farrella87810f2012-09-28 01:59:04 +0000328 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000329
Jake Farrella87810f2012-09-28 01:59:04 +0000330 it "should read json base64" do
331 @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
James E. King III27247072018-03-22 20:50:23 -0400332 expect(@prot.read_json_base64).to eq("this is a test string")
Jake Farrella87810f2012-09-28 01:59:04 +0000333 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000334
Jake Farrella87810f2012-09-28 01:59:04 +0000335 it "should is json numeric" do
James E. King III27247072018-03-22 20:50:23 -0400336 expect(@prot.is_json_numeric("A")).to eq(false)
337 expect(@prot.is_json_numeric("+")).to eq(true)
338 expect(@prot.is_json_numeric("-")).to eq(true)
339 expect(@prot.is_json_numeric(".")).to eq(true)
340 expect(@prot.is_json_numeric("0")).to eq(true)
341 expect(@prot.is_json_numeric("1")).to eq(true)
342 expect(@prot.is_json_numeric("2")).to eq(true)
343 expect(@prot.is_json_numeric("3")).to eq(true)
344 expect(@prot.is_json_numeric("4")).to eq(true)
345 expect(@prot.is_json_numeric("5")).to eq(true)
346 expect(@prot.is_json_numeric("6")).to eq(true)
347 expect(@prot.is_json_numeric("7")).to eq(true)
348 expect(@prot.is_json_numeric("8")).to eq(true)
349 expect(@prot.is_json_numeric("9")).to eq(true)
350 expect(@prot.is_json_numeric("E")).to eq(true)
351 expect(@prot.is_json_numeric("e")).to eq(true)
Jake Farrella87810f2012-09-28 01:59:04 +0000352 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000353
Jake Farrella87810f2012-09-28 01:59:04 +0000354 it "should read json numeric chars" do
355 @trans.write("1.453E45T")
James E. King III27247072018-03-22 20:50:23 -0400356 expect(@prot.read_json_numeric_chars).to eq("1.453E45")
Jake Farrella87810f2012-09-28 01:59:04 +0000357 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000358
Jake Farrella87810f2012-09-28 01:59:04 +0000359 it "should read json integer" do
360 @trans.write("1.45\"\"")
361 expect {@prot.read_json_integer}.to raise_error(Thrift::ProtocolException)
362 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000363
Jake Farrella87810f2012-09-28 01:59:04 +0000364 @trans.write("1453T")
James E. King III27247072018-03-22 20:50:23 -0400365 expect(@prot.read_json_integer).to eq(1453)
Jake Farrella87810f2012-09-28 01:59:04 +0000366 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000367
Jake Farrella87810f2012-09-28 01:59:04 +0000368 it "should read json double" do
369 @trans.write("1.45e3e01\"\"")
370 expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException)
371 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000372
Jake Farrella87810f2012-09-28 01:59:04 +0000373 @trans.write("\"1.453e01\"")
374 expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000375
Jake Farrella87810f2012-09-28 01:59:04 +0000376 @trans.write("1.453e01\"\"")
James E. King III27247072018-03-22 20:50:23 -0400377 expect(@prot.read_json_double).to eq(14.53)
Jake Farrella87810f2012-09-28 01:59:04 +0000378 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000379
Jake Farrella87810f2012-09-28 01:59:04 +0000380 @trans.write("\"NaN\"")
James E. King III27247072018-03-22 20:50:23 -0400381 expect(@prot.read_json_double.nan?).to eq(true)
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 Farrell6f0f5272012-01-31 03:39:30 +0000385
Jake Farrella87810f2012-09-28 01:59:04 +0000386 @trans.write("\"-Infinity\"")
James E. King III27247072018-03-22 20:50:23 -0400387 expect(@prot.read_json_double).to eq(-1.0/0.0)
Jake Farrella87810f2012-09-28 01:59:04 +0000388 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000389
Jake Farrella87810f2012-09-28 01:59:04 +0000390 it "should read json object start" do
391 @trans.write("{")
James E. King III27247072018-03-22 20:50:23 -0400392 expect(@prot.read_json_object_start).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000393 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000394
Jake Farrella87810f2012-09-28 01:59:04 +0000395 it "should read json object end" do
396 @trans.write("}")
James E. King III27247072018-03-22 20:50:23 -0400397 expect(@prot.read_json_object_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000398 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000399
Jake Farrella87810f2012-09-28 01:59:04 +0000400 it "should read json array start" do
401 @trans.write("[")
James E. King III27247072018-03-22 20:50:23 -0400402 expect(@prot.read_json_array_start).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000403 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000404
Jake Farrella87810f2012-09-28 01:59:04 +0000405 it "should read json array end" do
406 @trans.write("]")
James E. King III27247072018-03-22 20:50:23 -0400407 expect(@prot.read_json_array_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000408 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000409
Jake Farrella87810f2012-09-28 01:59:04 +0000410 it "should read_message_begin" do
411 @trans.write("[2,")
412 expect {@prot.read_message_begin}.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000413
Jake Farrella87810f2012-09-28 01:59:04 +0000414 @trans.write("[1,\"name\",12,32\"\"")
James E. King III27247072018-03-22 20:50:23 -0400415 expect(@prot.read_message_begin).to eq(["name", 12, 32])
Jake Farrella87810f2012-09-28 01:59:04 +0000416 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000417
Jake Farrella87810f2012-09-28 01:59:04 +0000418 it "should read message end" do
419 @trans.write("]")
James E. King III27247072018-03-22 20:50:23 -0400420 expect(@prot.read_message_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000421 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000422
Jake Farrella87810f2012-09-28 01:59:04 +0000423 it "should read struct begin" do
424 @trans.write("{")
James E. King III27247072018-03-22 20:50:23 -0400425 expect(@prot.read_struct_begin).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000426 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000427
Jake Farrella87810f2012-09-28 01:59:04 +0000428 it "should read struct end" do
429 @trans.write("}")
James E. King III27247072018-03-22 20:50:23 -0400430 expect(@prot.read_struct_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000431 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000432
Jake Farrella87810f2012-09-28 01:59:04 +0000433 it "should read field begin" do
434 @trans.write("1{\"rec\"")
James E. King III27247072018-03-22 20:50:23 -0400435 expect(@prot.read_field_begin).to eq([nil, 12, 1])
Jake Farrella87810f2012-09-28 01:59:04 +0000436 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000437
Jake Farrella87810f2012-09-28 01:59:04 +0000438 it "should read field end" do
439 @trans.write("}")
James E. King III27247072018-03-22 20:50:23 -0400440 expect(@prot.read_field_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000441 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000442
Jake Farrella87810f2012-09-28 01:59:04 +0000443 it "should read map begin" do
444 @trans.write("[\"rec\",\"lst\",2,{")
James E. King III27247072018-03-22 20:50:23 -0400445 expect(@prot.read_map_begin).to eq([12, 15, 2])
Jake Farrella87810f2012-09-28 01:59:04 +0000446 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000447
Jake Farrella87810f2012-09-28 01:59:04 +0000448 it "should read map end" do
449 @trans.write("}]")
James E. King III27247072018-03-22 20:50:23 -0400450 expect(@prot.read_map_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000451 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000452
Jake Farrella87810f2012-09-28 01:59:04 +0000453 it "should read list begin" do
454 @trans.write("[\"rec\",2\"\"")
James E. King III27247072018-03-22 20:50:23 -0400455 expect(@prot.read_list_begin).to eq([12, 2])
Jake Farrella87810f2012-09-28 01:59:04 +0000456 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000457
Jake Farrella87810f2012-09-28 01:59:04 +0000458 it "should read list end" do
459 @trans.write("]")
James E. King III27247072018-03-22 20:50:23 -0400460 expect(@prot.read_list_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000461 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000462
Jake Farrella87810f2012-09-28 01:59:04 +0000463 it "should read set begin" do
Roger Meier772b2b12013-01-19 21:04:12 +0100464 @trans.write("[\"rec\",2\"\"")
James E. King III27247072018-03-22 20:50:23 -0400465 expect(@prot.read_set_begin).to eq([12, 2])
Jake Farrella87810f2012-09-28 01:59:04 +0000466 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000467
Jake Farrella87810f2012-09-28 01:59:04 +0000468 it "should read set end" do
469 @trans.write("]")
James E. King III27247072018-03-22 20:50:23 -0400470 expect(@prot.read_set_end).to eq(nil)
Jake Farrella87810f2012-09-28 01:59:04 +0000471 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000472
Jake Farrella87810f2012-09-28 01:59:04 +0000473 it "should read bool" do
474 @trans.write("0\"\"")
James E. King III27247072018-03-22 20:50:23 -0400475 expect(@prot.read_bool).to eq(false)
Jake Farrella87810f2012-09-28 01:59:04 +0000476 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000477
Jake Farrella87810f2012-09-28 01:59:04 +0000478 @trans.write("1\"\"")
James E. King III27247072018-03-22 20:50:23 -0400479 expect(@prot.read_bool).to eq(true)
Jake Farrella87810f2012-09-28 01:59:04 +0000480 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000481
Jake Farrella87810f2012-09-28 01:59:04 +0000482 it "should read byte" do
483 @trans.write("60\"\"")
James E. King III27247072018-03-22 20:50:23 -0400484 expect(@prot.read_byte).to eq(60)
Jake Farrella87810f2012-09-28 01:59:04 +0000485 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000486
Jake Farrella87810f2012-09-28 01:59:04 +0000487 it "should read i16" do
488 @trans.write("1000\"\"")
James E. King III27247072018-03-22 20:50:23 -0400489 expect(@prot.read_i16).to eq(1000)
Jake Farrella87810f2012-09-28 01:59:04 +0000490 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000491
Jake Farrella87810f2012-09-28 01:59:04 +0000492 it "should read i32" do
493 @trans.write("3000000000\"\"")
James E. King III27247072018-03-22 20:50:23 -0400494 expect(@prot.read_i32).to eq(3000000000)
Jake Farrella87810f2012-09-28 01:59:04 +0000495 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000496
Jake Farrella87810f2012-09-28 01:59:04 +0000497 it "should read i64" do
498 @trans.write("6000000000\"\"")
James E. King III27247072018-03-22 20:50:23 -0400499 expect(@prot.read_i64).to eq(6000000000)
Jake Farrella87810f2012-09-28 01:59:04 +0000500 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000501
Jake Farrella87810f2012-09-28 01:59:04 +0000502 it "should read double" do
503 @trans.write("12.23\"\"")
James E. King III27247072018-03-22 20:50:23 -0400504 expect(@prot.read_double).to eq(12.23)
Jake Farrella87810f2012-09-28 01:59:04 +0000505 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000506
Jake Farrellb5a18a12012-10-09 01:10:43 +0000507 if RUBY_VERSION >= '1.9'
508 it 'should read string' do
509 @trans.write('"this is a test string"'.force_encoding(Encoding::BINARY))
510 a = @prot.read_string
James E. King III27247072018-03-22 20:50:23 -0400511 expect(a).to eq('this is a test string')
512 expect(a.encoding).to eq(Encoding::UTF_8)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000513 end
514
515 it 'should read string with unicode characters' do
516 @trans.write('"this is a test string with unicode characters: \u20AC \u20AD"'.force_encoding(Encoding::BINARY))
517 a = @prot.read_string
James E. King III27247072018-03-22 20:50:23 -0400518 expect(a).to eq("this is a test string with unicode characters: \u20AC \u20AD")
519 expect(a.encoding).to eq(Encoding::UTF_8)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000520 end
521 else
522 it 'should read string' do
523 @trans.write('"this is a test string"')
James E. King III27247072018-03-22 20:50:23 -0400524 expect(@prot.read_string).to eq('this is a test string')
Jake Farrellb5a18a12012-10-09 01:10:43 +0000525 end
Jake Farrella87810f2012-09-28 01:59:04 +0000526 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000527
Jake Farrella87810f2012-09-28 01:59:04 +0000528 it "should read binary" do
529 @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
James E. King III27247072018-03-22 20:50:23 -0400530 expect(@prot.read_binary).to eq("this is a test string")
Jake Farrella87810f2012-09-28 01:59:04 +0000531 end
Jens Geyer123258b2015-10-02 00:38:17 +0200532
533 it "should read long binary" do
534 @trans.write("\"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==\"")
James E. King III27247072018-03-22 20:50:23 -0400535 expect(@prot.read_binary.bytes.to_a).to eq((0...256).to_a)
Jens Geyer123258b2015-10-02 00:38:17 +0200536 end
James E. King III9aaf2952018-03-20 15:06:08 -0400537
538 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