blob: ce64aa8a8b71f11901f6b76b81851e34957e646c [file] [log] [blame]
Jake Farrell6f0f5272012-01-31 03:39:30 +00001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
20require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
21
22class ThriftJsonProtocolSpec < Spec::ExampleGroup
23 include Thrift
24
25 before(:each) do
26 @trans = Thrift::MemoryBufferTransport.new
27 @prot = JsonProtocol.new(@trans)
28 end
29
30 it "should write json escaped char" do
31 @prot.write_json_escape_char("\n")
32 @trans.read(@trans.available).should == '\u000a'
33
34 @prot.write_json_escape_char(" ")
35 @trans.read(@trans.available).should == '\u0020'
36 end
37
38 it "should write json char" do
39 @prot.write_json_char("\n")
40 @trans.read(@trans.available).should == '\\n'
41
42 @prot.write_json_char(" ")
43 @trans.read(@trans.available).should == ' '
44
45 @prot.write_json_char("\\")
46 @trans.read(@trans.available).should == "\\\\"
47
48 @prot.write_json_char("@")
49 @trans.read(@trans.available).should == '@'
50 end
51
52 it "should write json string" do
53 @prot.write_json_string("this is a \\ json\nstring")
54 @trans.read(@trans.available).should == "\"this is a \\\\ json\\nstring\""
55 end
56
57 it "should write json base64" do
58 @prot.write_json_base64("this is a base64 string")
59 @trans.read(@trans.available).should == "\"\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\\n\"\""
60 end
61
62 it "should write json integer" do
63 @prot.write_json_integer(45)
64 @trans.read(@trans.available).should == "45"
65
66 @prot.write_json_integer(33000)
67 @trans.read(@trans.available).should == "33000"
68
69 @prot.write_json_integer(3000000000)
70 @trans.read(@trans.available).should == "3000000000"
71
72 @prot.write_json_integer(6000000000)
73 @trans.read(@trans.available).should == "6000000000"
74 end
75
76 it "should write json double" do
77 @prot.write_json_double(12.3)
78 @trans.read(@trans.available).should == "12.3"
79
80 @prot.write_json_double(-3.21)
81 @trans.read(@trans.available).should == "-3.21"
82
83 @prot.write_json_double(((+1.0/0.0)/(+1.0/0.0)))
84 @trans.read(@trans.available).should == "\"NaN\""
85
86 @prot.write_json_double((+1.0/0.0))
87 @trans.read(@trans.available).should == "\"Infinity\""
88
89 @prot.write_json_double((-1.0/0.0))
90 @trans.read(@trans.available).should == "\"-Infinity\""
91 end
92
93 it "should write json object start" do
94 @prot.write_json_object_start
95 @trans.read(@trans.available).should == "{"
96 end
97
98 it "should write json object end" do
99 @prot.write_json_object_end
100 @trans.read(@trans.available).should == "}"
101 end
102
103 it "should write json array start" do
104 @prot.write_json_array_start
105 @trans.read(@trans.available).should == "["
106 end
107
108 it "should write json array end" do
109 @prot.write_json_array_end
110 @trans.read(@trans.available).should == "]"
111 end
112
113 it "should write message begin" do
114 @prot.write_message_begin("name", 12, 32)
115 @trans.read(@trans.available).should == "[1,\"name\",12,32"
116 end
117
118 it "should write message end" do
119 @prot.write_message_end
120 @trans.read(@trans.available).should == "]"
121 end
122
123 it "should write struct begin" do
124 @prot.write_struct_begin("name")
125 @trans.read(@trans.available).should == "{"
126 end
127
128 it "should write struct end" do
129 @prot.write_struct_end
130 @trans.read(@trans.available).should == "}"
131 end
132
133 it "should write field begin" do
134 @prot.write_field_begin("name", Types::STRUCT, 32)
135 @trans.read(@trans.available).should == "32{\"rec\""
136 end
137
138 it "should write field end" do
139 @prot.write_field_end
140 @trans.read(@trans.available).should == "}"
141 end
142
143 it "should write field stop" do
144 @prot.write_field_stop
145 @trans.read(@trans.available).should == ""
146 end
147
148 it "should write map begin" do
149 @prot.write_map_begin(Types::STRUCT, Types::LIST, 32)
150 @trans.read(@trans.available).should == "[\"rec\",\"lst\",32,{"
151 end
152
153 it "should write map end" do
154 @prot.write_map_end
155 @trans.read(@trans.available).should == "}]"
156 end
157
158 it "should write list begin" do
159 @prot.write_list_begin(Types::STRUCT, 32)
160 @trans.read(@trans.available).should == "[\"rec\",32"
161 end
162
163 it "should write list end" do
164 @prot.write_list_end
165 @trans.read(@trans.available).should == "]"
166 end
167
168 it "should write set begin" do
169 @prot.write_set_begin(Types::STRUCT, 32)
170 @trans.read(@trans.available).should == "[\"rec\",32"
171 end
172
173 it "should write set end" do
174 @prot.write_set_end
175 @trans.read(@trans.available).should == "]"
176 end
177
178 it "should write bool" do
179 @prot.write_bool(true)
180 @trans.read(@trans.available).should == "1"
181
182 @prot.write_bool(false)
183 @trans.read(@trans.available).should == "0"
184 end
185
186 it "should write byte" do
187 @prot.write_byte(100)
188 @trans.read(@trans.available).should == "100"
189 end
190
191 it "should write i16" do
192 @prot.write_i16(1000)
193 @trans.read(@trans.available).should == "1000"
194 end
195
196 it "should write i32" do
197 @prot.write_i32(3000000000)
198 @trans.read(@trans.available).should == "3000000000"
199 end
200
201 it "should write i64" do
202 @prot.write_i64(6000000000)
203 @trans.read(@trans.available).should == "6000000000"
204 end
205
206 it "should write double" do
207 @prot.write_double(1.23)
208 @trans.read(@trans.available).should == "1.23"
209
210 @prot.write_double(-32.1)
211 @trans.read(@trans.available).should == "-32.1"
212
213 @prot.write_double(((+1.0/0.0)/(+1.0/0.0)))
214 @trans.read(@trans.available).should == "\"NaN\""
215
216 @prot.write_double((+1.0/0.0))
217 @trans.read(@trans.available).should == "\"Infinity\""
218
219 @prot.write_double((-1.0/0.0))
220 @trans.read(@trans.available).should == "\"-Infinity\""
221 end
222
223 it "should write string" do
224 @prot.write_string("this is a test string")
225 @trans.read(@trans.available).should == "\"this is a test string\""
226 end
227
228 it "should write binary" do
229 @prot.write_binary("this is a base64 string")
230 @trans.read(@trans.available).should == "\"\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\\n\"\""
231 end
232
233 it "should get type name for type id" do
234 expect {@prot.get_type_name_for_type_id(Types::STOP)}.to raise_error(NotImplementedError)
235 expect {@prot.get_type_name_for_type_id(Types::VOID)}.to raise_error(NotImplementedError)
236 @prot.get_type_name_for_type_id(Types::BOOL).should == "tf"
237 @prot.get_type_name_for_type_id(Types::BYTE).should == "i8"
238 @prot.get_type_name_for_type_id(Types::DOUBLE).should == "dbl"
239 @prot.get_type_name_for_type_id(Types::I16).should == "i16"
240 @prot.get_type_name_for_type_id(Types::I32).should == "i32"
241 @prot.get_type_name_for_type_id(Types::I64).should == "i64"
242 @prot.get_type_name_for_type_id(Types::STRING).should == "str"
243 @prot.get_type_name_for_type_id(Types::STRUCT).should == "rec"
244 @prot.get_type_name_for_type_id(Types::MAP).should == "map"
245 @prot.get_type_name_for_type_id(Types::SET).should == "set"
246 @prot.get_type_name_for_type_id(Types::LIST).should == "lst"
247 end
248
249 it "should get type id for type name" do
250 expect {@prot.get_type_id_for_type_name("pp")}.to raise_error(NotImplementedError)
251 @prot.get_type_id_for_type_name("tf").should == Types::BOOL
252 @prot.get_type_id_for_type_name("i8").should == Types::BYTE
253 @prot.get_type_id_for_type_name("dbl").should == Types::DOUBLE
254 @prot.get_type_id_for_type_name("i16").should == Types::I16
255 @prot.get_type_id_for_type_name("i32").should == Types::I32
256 @prot.get_type_id_for_type_name("i64").should == Types::I64
257 @prot.get_type_id_for_type_name("str").should == Types::STRING
258 @prot.get_type_id_for_type_name("rec").should == Types::STRUCT
259 @prot.get_type_id_for_type_name("map").should == Types::MAP
260 @prot.get_type_id_for_type_name("set").should == Types::SET
261 @prot.get_type_id_for_type_name("lst").should == Types::LIST
262 end
263
264 it "should read json syntax char" do
265 @trans.write('F')
266 expect {@prot.read_json_syntax_char('G')}.to raise_error(ProtocolException)
267 @trans.write('H')
268 @prot.read_json_syntax_char('H')
269 end
270
271 it "should read json escape char" do
272 @trans.write('0054')
273 @prot.read_json_escape_char.should == 'T'
274 end
275
276 it "should read json string" do
277 @trans.write("\"\\P")
278 expect {@prot.read_json_string(false)}.to raise_error(ProtocolException)
279
280 @trans.write("\"\\n\"")
281 @prot.read_json_string(false).should == "\\n"
282
283 @trans.write("\"this is a test string\"")
284 @prot.read_json_string.should == "this is a test string"
285 end
286
287 it "should read json base64" do
288 @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
289 @prot.read_json_base64.should == "this is a test string"
290 end
291
292 it "should is json numeric" do
293 @prot.is_json_numeric("A").should == false
294 @prot.is_json_numeric("+").should == true
295 @prot.is_json_numeric("-").should == true
296 @prot.is_json_numeric(".").should == true
297 @prot.is_json_numeric("0").should == true
298 @prot.is_json_numeric("1").should == true
299 @prot.is_json_numeric("2").should == true
300 @prot.is_json_numeric("3").should == true
301 @prot.is_json_numeric("4").should == true
302 @prot.is_json_numeric("5").should == true
303 @prot.is_json_numeric("6").should == true
304 @prot.is_json_numeric("7").should == true
305 @prot.is_json_numeric("8").should == true
306 @prot.is_json_numeric("9").should == true
307 @prot.is_json_numeric("E").should == true
308 @prot.is_json_numeric("e").should == true
309 end
310
311 it "should read json numeric chars" do
312 @trans.write("1.453E45T")
313 @prot.read_json_numeric_chars.should == "1.453E45"
314 end
315
316 it "should read json integer" do
317 @trans.write("1.45\"\"")
318 expect {@prot.read_json_integer}.to raise_error(ProtocolException)
319 @prot.read_string
320
321 @trans.write("1453T")
322 @prot.read_json_integer.should == 1453
323 end
324
325 it "should read json double" do
326 @trans.write("1.45e3e01\"\"")
327 expect {@prot.read_json_double}.to raise_error(ProtocolException)
328 @prot.read_string
329
330 @trans.write("\"1.453e01\"")
331 expect {@prot.read_json_double}.to raise_error(ProtocolException)
332
333 @trans.write("1.453e01\"\"")
334 @prot.read_json_double.should == 14.53
335 @prot.read_string
336
337 @trans.write("\"NaN\"")
338 @prot.read_json_double.nan?.should == true
339
340 @trans.write("\"Infinity\"")
341 @prot.read_json_double.should == +1.0/0.0
342
343 @trans.write("\"-Infinity\"")
344 @prot.read_json_double.should == -1.0/0.0
345 end
346
347 it "should read json object start" do
348 @trans.write("{")
349 @prot.read_json_object_start.should == nil
350 end
351
352 it "should read json object end" do
353 @trans.write("}")
354 @prot.read_json_object_end.should == nil
355 end
356
357 it "should read json array start" do
358 @trans.write("[")
359 @prot.read_json_array_start.should == nil
360 end
361
362 it "should read json array end" do
363 @trans.write("]")
364 @prot.read_json_array_end.should == nil
365 end
366
367 it "should read_message_begin" do
368 @trans.write("[2,")
369 expect {@prot.read_message_begin}.to raise_error(ProtocolException)
370
371 @trans.write("[1,\"name\",12,32\"\"")
372 @prot.read_message_begin.should == ["name", 12, 32]
373 end
374
375 it "should read message end" do
376 @trans.write("]")
377 @prot.read_message_end.should == nil
378 end
379
380 it "should read struct begin" do
381 @trans.write("{")
382 @prot.read_struct_begin.should == nil
383 end
384
385 it "should read struct end" do
386 @trans.write("}")
387 @prot.read_struct_end.should == nil
388 end
389
390 it "should read field begin" do
391 @trans.write("1{\"rec\"")
392 @prot.read_field_begin.should == [nil, 12, 1]
393 end
394
395 it "should read field end" do
396 @trans.write("}")
397 @prot.read_field_end.should == nil
398 end
399
400 it "should read map begin" do
401 @trans.write("[\"rec\",\"lst\",2,{")
402 @prot.read_map_begin.should == [12, 15, 2]
403 end
404
405 it "should read map end" do
406 @trans.write("}]")
407 @prot.read_map_end.should == nil
408 end
409
410 it "should read list begin" do
411 @trans.write("[\"rec\",2\"\"")
412 @prot.read_list_begin.should == [12, 2]
413 end
414
415 it "should read list end" do
416 @trans.write("]")
417 @prot.read_list_end.should == nil
418 end
419
420 it "should read set begin" do
421 @trans.write("[")
422 @prot.read_set_begin.should == nil
423 end
424
425 it "should read set end" do
426 @trans.write("]")
427 @prot.read_set_end.should == nil
428 end
429
430 it "should read bool" do
431 @trans.write("0\"\"")
432 @prot.read_bool.should == false
433 @prot.read_string
434
435 @trans.write("1\"\"")
436 @prot.read_bool.should == true
437 end
438
439 it "should read byte" do
440 @trans.write("60\"\"")
441 @prot.read_byte.should == 60
442 end
443
444 it "should read i16" do
445 @trans.write("1000\"\"")
446 @prot.read_i16.should == 1000
447 end
448
449 it "should read i32" do
450 @trans.write("3000000000\"\"")
451 @prot.read_i32.should == 3000000000
452 end
453
454 it "should read i64" do
455 @trans.write("6000000000\"\"")
456 @prot.read_i64.should == 6000000000
457 end
458
459 it "should read double" do
460 @trans.write("12.23\"\"")
461 @prot.read_double.should == 12.23
462 end
463
464 it "should read string" do
465 @trans.write("\"this is a test string\"")
466 @prot.read_string.should == "this is a test string"
467 end
468
469 it "should read binary" do
470 @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
471 @prot.read_binary.should == "this is a test string"
472 end
473
474 describe JsonProtocolFactory do
475 it "should create a JsonProtocol" do
476 JsonProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(JsonProtocol)
477 end
478 end
479end