blob: 2f7f1e6b2194d10693d81313253eaf85d882aef1 [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")
33 @trans.read(@trans.available).should == '\u000a'
Jake Farrell6f0f5272012-01-31 03:39:30 +000034
Jake Farrella87810f2012-09-28 01:59:04 +000035 @prot.write_json_escape_char(" ")
36 @trans.read(@trans.available).should == '\u0020'
37 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")
41 @trans.read(@trans.available).should == '\\n'
Jake Farrell6f0f5272012-01-31 03:39:30 +000042
Jake Farrella87810f2012-09-28 01:59:04 +000043 @prot.write_json_char(" ")
44 @trans.read(@trans.available).should == ' '
Jake Farrell6f0f5272012-01-31 03:39:30 +000045
Jake Farrella87810f2012-09-28 01:59:04 +000046 @prot.write_json_char("\\")
47 @trans.read(@trans.available).should == "\\\\"
Jake Farrell6f0f5272012-01-31 03:39:30 +000048
Jake Farrella87810f2012-09-28 01:59:04 +000049 @prot.write_json_char("@")
50 @trans.read(@trans.available).should == '@'
51 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")
55 @trans.read(@trans.available).should == "\"this is a \\\\ json\\nstring\""
56 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")
60 @trans.read(@trans.available).should == "\"\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\\n\"\""
61 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)
65 @trans.read(@trans.available).should == "45"
Jake Farrell6f0f5272012-01-31 03:39:30 +000066
Jake Farrella87810f2012-09-28 01:59:04 +000067 @prot.write_json_integer(33000)
68 @trans.read(@trans.available).should == "33000"
Jake Farrell6f0f5272012-01-31 03:39:30 +000069
Jake Farrella87810f2012-09-28 01:59:04 +000070 @prot.write_json_integer(3000000000)
71 @trans.read(@trans.available).should == "3000000000"
Jake Farrell6f0f5272012-01-31 03:39:30 +000072
Jake Farrella87810f2012-09-28 01:59:04 +000073 @prot.write_json_integer(6000000000)
74 @trans.read(@trans.available).should == "6000000000"
75 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)
79 @trans.read(@trans.available).should == "12.3"
Jake Farrell6f0f5272012-01-31 03:39:30 +000080
Jake Farrella87810f2012-09-28 01:59:04 +000081 @prot.write_json_double(-3.21)
82 @trans.read(@trans.available).should == "-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)))
85 @trans.read(@trans.available).should == "\"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))
88 @trans.read(@trans.available).should == "\"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))
91 @trans.read(@trans.available).should == "\"-Infinity\""
92 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
96 @trans.read(@trans.available).should == "{"
97 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
101 @trans.read(@trans.available).should == "}"
102 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
106 @trans.read(@trans.available).should == "["
107 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
111 @trans.read(@trans.available).should == "]"
112 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)
116 @trans.read(@trans.available).should == "[1,\"name\",12,32"
117 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
121 @trans.read(@trans.available).should == "]"
122 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")
126 @trans.read(@trans.available).should == "{"
127 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
131 @trans.read(@trans.available).should == "}"
132 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)
136 @trans.read(@trans.available).should == "32{\"rec\""
137 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
141 @trans.read(@trans.available).should == "}"
142 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
146 @trans.read(@trans.available).should == ""
147 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)
151 @trans.read(@trans.available).should == "[\"rec\",\"lst\",32,{"
152 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
156 @trans.read(@trans.available).should == "}]"
157 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)
161 @trans.read(@trans.available).should == "[\"rec\",32"
162 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
166 @trans.read(@trans.available).should == "]"
167 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)
171 @trans.read(@trans.available).should == "[\"rec\",32"
172 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
176 @trans.read(@trans.available).should == "]"
177 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)
181 @trans.read(@trans.available).should == "1"
Jake Farrell6f0f5272012-01-31 03:39:30 +0000182
Jake Farrella87810f2012-09-28 01:59:04 +0000183 @prot.write_bool(false)
184 @trans.read(@trans.available).should == "0"
185 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)
189 @trans.read(@trans.available).should == "100"
190 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)
194 @trans.read(@trans.available).should == "1000"
195 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)
199 @trans.read(@trans.available).should == "3000000000"
200 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)
204 @trans.read(@trans.available).should == "6000000000"
205 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)
209 @trans.read(@trans.available).should == "1.23"
Jake Farrell6f0f5272012-01-31 03:39:30 +0000210
Jake Farrella87810f2012-09-28 01:59:04 +0000211 @prot.write_double(-32.1)
212 @trans.read(@trans.available).should == "-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)))
215 @trans.read(@trans.available).should == "\"NaN\""
Jake Farrell6f0f5272012-01-31 03:39:30 +0000216
Jake Farrella87810f2012-09-28 01:59:04 +0000217 @prot.write_double((+1.0/0.0))
218 @trans.read(@trans.available).should == "\"Infinity\""
Jake Farrell6f0f5272012-01-31 03:39:30 +0000219
Jake Farrella87810f2012-09-28 01:59:04 +0000220 @prot.write_double((-1.0/0.0))
221 @trans.read(@trans.available).should == "\"-Infinity\""
222 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)
228 a.should == '"this is a test string"'.force_encoding(Encoding::BINARY)
229 a.encoding.should == Encoding::BINARY
230 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)
235 a.should == "\"this is a test string with unicode characters: \u20AC \u20AD\"".force_encoding(Encoding::BINARY)
236 a.encoding.should == Encoding::BINARY
237 end
238 else
239 it 'should write string' do
240 @prot.write_string('this is a test string')
241 @trans.read(@trans.available).should == '"this is a test string"'
242 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")
247 @trans.read(@trans.available).should == "\"\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\\n\"\""
248 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000249
Jake Farrella87810f2012-09-28 01:59:04 +0000250 it "should get type name for type id" do
251 expect {@prot.get_type_name_for_type_id(Thrift::Types::STOP)}.to raise_error(NotImplementedError)
252 expect {@prot.get_type_name_for_type_id(Thrift::Types::VOID)}.to raise_error(NotImplementedError)
253 @prot.get_type_name_for_type_id(Thrift::Types::BOOL).should == "tf"
254 @prot.get_type_name_for_type_id(Thrift::Types::BYTE).should == "i8"
255 @prot.get_type_name_for_type_id(Thrift::Types::DOUBLE).should == "dbl"
256 @prot.get_type_name_for_type_id(Thrift::Types::I16).should == "i16"
257 @prot.get_type_name_for_type_id(Thrift::Types::I32).should == "i32"
258 @prot.get_type_name_for_type_id(Thrift::Types::I64).should == "i64"
259 @prot.get_type_name_for_type_id(Thrift::Types::STRING).should == "str"
260 @prot.get_type_name_for_type_id(Thrift::Types::STRUCT).should == "rec"
261 @prot.get_type_name_for_type_id(Thrift::Types::MAP).should == "map"
262 @prot.get_type_name_for_type_id(Thrift::Types::SET).should == "set"
263 @prot.get_type_name_for_type_id(Thrift::Types::LIST).should == "lst"
264 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000265
Jake Farrella87810f2012-09-28 01:59:04 +0000266 it "should get type id for type name" do
267 expect {@prot.get_type_id_for_type_name("pp")}.to raise_error(NotImplementedError)
268 @prot.get_type_id_for_type_name("tf").should == Thrift::Types::BOOL
269 @prot.get_type_id_for_type_name("i8").should == Thrift::Types::BYTE
270 @prot.get_type_id_for_type_name("dbl").should == Thrift::Types::DOUBLE
271 @prot.get_type_id_for_type_name("i16").should == Thrift::Types::I16
272 @prot.get_type_id_for_type_name("i32").should == Thrift::Types::I32
273 @prot.get_type_id_for_type_name("i64").should == Thrift::Types::I64
274 @prot.get_type_id_for_type_name("str").should == Thrift::Types::STRING
275 @prot.get_type_id_for_type_name("rec").should == Thrift::Types::STRUCT
276 @prot.get_type_id_for_type_name("map").should == Thrift::Types::MAP
277 @prot.get_type_id_for_type_name("set").should == Thrift::Types::SET
278 @prot.get_type_id_for_type_name("lst").should == Thrift::Types::LIST
279 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000280
Jake Farrella87810f2012-09-28 01:59:04 +0000281 it "should read json syntax char" do
282 @trans.write('F')
283 expect {@prot.read_json_syntax_char('G')}.to raise_error(Thrift::ProtocolException)
284 @trans.write('H')
285 @prot.read_json_syntax_char('H')
286 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000287
Jake Farrella87810f2012-09-28 01:59:04 +0000288 it "should read json escape char" do
289 @trans.write('0054')
290 @prot.read_json_escape_char.should == 'T'
291 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000292
Jake Farrella87810f2012-09-28 01:59:04 +0000293 it "should read json string" do
294 @trans.write("\"\\P")
295 expect {@prot.read_json_string(false)}.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000296
Jake Farrella87810f2012-09-28 01:59:04 +0000297 @trans.write("\"\\n\"")
298 @prot.read_json_string(false).should == "\\n"
Jake Farrell6f0f5272012-01-31 03:39:30 +0000299
Jake Farrella87810f2012-09-28 01:59:04 +0000300 @trans.write("\"this is a test string\"")
301 @prot.read_json_string.should == "this is a test string"
302 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000303
Jake Farrella87810f2012-09-28 01:59:04 +0000304 it "should read json base64" do
305 @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
306 @prot.read_json_base64.should == "this is a test string"
307 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000308
Jake Farrella87810f2012-09-28 01:59:04 +0000309 it "should is json numeric" do
310 @prot.is_json_numeric("A").should == false
311 @prot.is_json_numeric("+").should == true
312 @prot.is_json_numeric("-").should == true
313 @prot.is_json_numeric(".").should == true
314 @prot.is_json_numeric("0").should == true
315 @prot.is_json_numeric("1").should == true
316 @prot.is_json_numeric("2").should == true
317 @prot.is_json_numeric("3").should == true
318 @prot.is_json_numeric("4").should == true
319 @prot.is_json_numeric("5").should == true
320 @prot.is_json_numeric("6").should == true
321 @prot.is_json_numeric("7").should == true
322 @prot.is_json_numeric("8").should == true
323 @prot.is_json_numeric("9").should == true
324 @prot.is_json_numeric("E").should == true
325 @prot.is_json_numeric("e").should == true
326 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000327
Jake Farrella87810f2012-09-28 01:59:04 +0000328 it "should read json numeric chars" do
329 @trans.write("1.453E45T")
330 @prot.read_json_numeric_chars.should == "1.453E45"
331 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000332
Jake Farrella87810f2012-09-28 01:59:04 +0000333 it "should read json integer" do
334 @trans.write("1.45\"\"")
335 expect {@prot.read_json_integer}.to raise_error(Thrift::ProtocolException)
336 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000337
Jake Farrella87810f2012-09-28 01:59:04 +0000338 @trans.write("1453T")
339 @prot.read_json_integer.should == 1453
340 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000341
Jake Farrella87810f2012-09-28 01:59:04 +0000342 it "should read json double" do
343 @trans.write("1.45e3e01\"\"")
344 expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException)
345 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000346
Jake Farrella87810f2012-09-28 01:59:04 +0000347 @trans.write("\"1.453e01\"")
348 expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000349
Jake Farrella87810f2012-09-28 01:59:04 +0000350 @trans.write("1.453e01\"\"")
351 @prot.read_json_double.should == 14.53
352 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000353
Jake Farrella87810f2012-09-28 01:59:04 +0000354 @trans.write("\"NaN\"")
355 @prot.read_json_double.nan?.should == true
Jake Farrell6f0f5272012-01-31 03:39:30 +0000356
Jake Farrella87810f2012-09-28 01:59:04 +0000357 @trans.write("\"Infinity\"")
358 @prot.read_json_double.should == +1.0/0.0
Jake Farrell6f0f5272012-01-31 03:39:30 +0000359
Jake Farrella87810f2012-09-28 01:59:04 +0000360 @trans.write("\"-Infinity\"")
361 @prot.read_json_double.should == -1.0/0.0
362 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000363
Jake Farrella87810f2012-09-28 01:59:04 +0000364 it "should read json object start" do
365 @trans.write("{")
366 @prot.read_json_object_start.should == nil
367 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000368
Jake Farrella87810f2012-09-28 01:59:04 +0000369 it "should read json object end" do
370 @trans.write("}")
371 @prot.read_json_object_end.should == nil
372 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000373
Jake Farrella87810f2012-09-28 01:59:04 +0000374 it "should read json array start" do
375 @trans.write("[")
376 @prot.read_json_array_start.should == nil
377 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000378
Jake Farrella87810f2012-09-28 01:59:04 +0000379 it "should read json array end" do
380 @trans.write("]")
381 @prot.read_json_array_end.should == nil
382 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000383
Jake Farrella87810f2012-09-28 01:59:04 +0000384 it "should read_message_begin" do
385 @trans.write("[2,")
386 expect {@prot.read_message_begin}.to raise_error(Thrift::ProtocolException)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000387
Jake Farrella87810f2012-09-28 01:59:04 +0000388 @trans.write("[1,\"name\",12,32\"\"")
389 @prot.read_message_begin.should == ["name", 12, 32]
390 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000391
Jake Farrella87810f2012-09-28 01:59:04 +0000392 it "should read message end" do
393 @trans.write("]")
394 @prot.read_message_end.should == nil
395 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000396
Jake Farrella87810f2012-09-28 01:59:04 +0000397 it "should read struct begin" do
398 @trans.write("{")
399 @prot.read_struct_begin.should == nil
400 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000401
Jake Farrella87810f2012-09-28 01:59:04 +0000402 it "should read struct end" do
403 @trans.write("}")
404 @prot.read_struct_end.should == nil
405 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000406
Jake Farrella87810f2012-09-28 01:59:04 +0000407 it "should read field begin" do
408 @trans.write("1{\"rec\"")
409 @prot.read_field_begin.should == [nil, 12, 1]
410 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000411
Jake Farrella87810f2012-09-28 01:59:04 +0000412 it "should read field end" do
413 @trans.write("}")
414 @prot.read_field_end.should == nil
415 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000416
Jake Farrella87810f2012-09-28 01:59:04 +0000417 it "should read map begin" do
418 @trans.write("[\"rec\",\"lst\",2,{")
419 @prot.read_map_begin.should == [12, 15, 2]
420 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000421
Jake Farrella87810f2012-09-28 01:59:04 +0000422 it "should read map end" do
423 @trans.write("}]")
424 @prot.read_map_end.should == nil
425 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000426
Jake Farrella87810f2012-09-28 01:59:04 +0000427 it "should read list begin" do
428 @trans.write("[\"rec\",2\"\"")
429 @prot.read_list_begin.should == [12, 2]
430 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000431
Jake Farrella87810f2012-09-28 01:59:04 +0000432 it "should read list end" do
433 @trans.write("]")
434 @prot.read_list_end.should == nil
435 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000436
Jake Farrella87810f2012-09-28 01:59:04 +0000437 it "should read set begin" do
Roger Meier772b2b12013-01-19 21:04:12 +0100438 @trans.write("[\"rec\",2\"\"")
439 @prot.read_set_begin.should == [12, 2]
Jake Farrella87810f2012-09-28 01:59:04 +0000440 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000441
Jake Farrella87810f2012-09-28 01:59:04 +0000442 it "should read set end" do
443 @trans.write("]")
444 @prot.read_set_end.should == nil
445 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000446
Jake Farrella87810f2012-09-28 01:59:04 +0000447 it "should read bool" do
448 @trans.write("0\"\"")
449 @prot.read_bool.should == false
450 @prot.read_string
Jake Farrell6f0f5272012-01-31 03:39:30 +0000451
Jake Farrella87810f2012-09-28 01:59:04 +0000452 @trans.write("1\"\"")
453 @prot.read_bool.should == true
454 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000455
Jake Farrella87810f2012-09-28 01:59:04 +0000456 it "should read byte" do
457 @trans.write("60\"\"")
458 @prot.read_byte.should == 60
459 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000460
Jake Farrella87810f2012-09-28 01:59:04 +0000461 it "should read i16" do
462 @trans.write("1000\"\"")
463 @prot.read_i16.should == 1000
464 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000465
Jake Farrella87810f2012-09-28 01:59:04 +0000466 it "should read i32" do
467 @trans.write("3000000000\"\"")
468 @prot.read_i32.should == 3000000000
469 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000470
Jake Farrella87810f2012-09-28 01:59:04 +0000471 it "should read i64" do
472 @trans.write("6000000000\"\"")
473 @prot.read_i64.should == 6000000000
474 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000475
Jake Farrella87810f2012-09-28 01:59:04 +0000476 it "should read double" do
477 @trans.write("12.23\"\"")
478 @prot.read_double.should == 12.23
479 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000480
Jake Farrellb5a18a12012-10-09 01:10:43 +0000481 if RUBY_VERSION >= '1.9'
482 it 'should read string' do
483 @trans.write('"this is a test string"'.force_encoding(Encoding::BINARY))
484 a = @prot.read_string
485 a.should == 'this is a test string'
486 a.encoding.should == Encoding::UTF_8
487 end
488
489 it 'should read string with unicode characters' do
490 @trans.write('"this is a test string with unicode characters: \u20AC \u20AD"'.force_encoding(Encoding::BINARY))
491 a = @prot.read_string
492 a.should == "this is a test string with unicode characters: \u20AC \u20AD"
493 a.encoding.should == Encoding::UTF_8
494 end
495 else
496 it 'should read string' do
497 @trans.write('"this is a test string"')
498 @prot.read_string.should == 'this is a test string'
499 end
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 binary" do
503 @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
504 @prot.read_binary.should == "this is a test string"
505 end
Jake Farrell6f0f5272012-01-31 03:39:30 +0000506 end
507
Jake Farrella87810f2012-09-28 01:59:04 +0000508 describe Thrift::JsonProtocolFactory do
Jake Farrell6f0f5272012-01-31 03:39:30 +0000509 it "should create a JsonProtocol" do
Jake Farrella87810f2012-09-28 01:59:04 +0000510 Thrift::JsonProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::JsonProtocol)
Jake Farrell6f0f5272012-01-31 03:39:30 +0000511 end
512 end
513end