blob: b259bb0f152bab3755e504190ea15525e29acdb0 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +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
Jake Farrella87810f2012-09-28 01:59:04 +000020require 'spec_helper'
Kevin Clarkdc399732008-06-18 01:06:15 +000021
Jake Farrella87810f2012-09-28 01:59:04 +000022describe 'BaseProtocol' do
Kevin Clarkdc399732008-06-18 01:06:15 +000023 before(:each) do
James E. King III27247072018-03-22 20:50:23 -040024 @trans = double("MockTransport")
Jake Farrella87810f2012-09-28 01:59:04 +000025 @prot = Thrift::BaseProtocol.new(@trans)
Kevin Clarkdc399732008-06-18 01:06:15 +000026 end
27
Jake Farrella87810f2012-09-28 01:59:04 +000028 describe Thrift::BaseProtocol do
Kevin Clarkdc399732008-06-18 01:06:15 +000029 # most of the methods are stubs, so we can ignore them
Kevin Clark9db1c2e2008-06-18 01:06:42 +000030
James E. King III9aaf2952018-03-20 15:06:08 -040031 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -040032 expect(@trans).to receive(:to_s).once.and_return("trans")
33 expect(@prot.to_s).to eq("trans")
James E. King III9aaf2952018-03-20 15:06:08 -040034 end
35
Kevin Clark9db1c2e2008-06-18 01:06:42 +000036 it "should make trans accessible" do
James E. King III27247072018-03-22 20:50:23 -040037 expect(@prot.trans).to eql(@trans)
Kevin Clark9db1c2e2008-06-18 01:06:42 +000038 end
39
Roger Meier19dbbef2012-12-27 01:24:20 +010040 it 'should write out a field nicely (deprecated write_field signature)' do
James E. King III27247072018-03-22 20:50:23 -040041 expect(@prot).to receive(:write_field_begin).with('field', 'type', 'fid').ordered
42 expect(@prot).to receive(:write_type).with({:name => 'field', :type => 'type'}, 'value').ordered
43 expect(@prot).to receive(:write_field_end).ordered
Kevin Clarkdc399732008-06-18 01:06:15 +000044 @prot.write_field('field', 'type', 'fid', 'value')
45 end
46
Roger Meier19dbbef2012-12-27 01:24:20 +010047 it 'should write out a field nicely' do
James E. King III27247072018-03-22 20:50:23 -040048 expect(@prot).to receive(:write_field_begin).with('field', 'type', 'fid').ordered
49 expect(@prot).to receive(:write_type).with({:name => 'field', :type => 'type', :binary => false}, 'value').ordered
50 expect(@prot).to receive(:write_field_end).ordered
Roger Meier19dbbef2012-12-27 01:24:20 +010051 @prot.write_field({:name => 'field', :type => 'type', :binary => false}, 'fid', 'value')
52 end
53
54 it 'should write out the different types (deprecated write_type signature)' do
James E. King III27247072018-03-22 20:50:23 -040055 expect(@prot).to receive(:write_bool).with('bool').ordered
56 expect(@prot).to receive(:write_byte).with('byte').ordered
57 expect(@prot).to receive(:write_double).with('double').ordered
58 expect(@prot).to receive(:write_i16).with('i16').ordered
59 expect(@prot).to receive(:write_i32).with('i32').ordered
60 expect(@prot).to receive(:write_i64).with('i64').ordered
61 expect(@prot).to receive(:write_string).with('string').ordered
62 struct = double('Struct')
63 expect(struct).to receive(:write).with(@prot).ordered
Jake Farrella87810f2012-09-28 01:59:04 +000064 @prot.write_type(Thrift::Types::BOOL, 'bool')
65 @prot.write_type(Thrift::Types::BYTE, 'byte')
66 @prot.write_type(Thrift::Types::DOUBLE, 'double')
67 @prot.write_type(Thrift::Types::I16, 'i16')
68 @prot.write_type(Thrift::Types::I32, 'i32')
69 @prot.write_type(Thrift::Types::I64, 'i64')
70 @prot.write_type(Thrift::Types::STRING, 'string')
71 @prot.write_type(Thrift::Types::STRUCT, struct)
Kevin Clarkdc399732008-06-18 01:06:15 +000072 # all other types are not implemented
Jake Farrella87810f2012-09-28 01:59:04 +000073 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP, Thrift::Types::SET, Thrift::Types::LIST].each do |type|
Roger Meier19dbbef2012-12-27 01:24:20 +010074 expect { @prot.write_type(type, type.to_s) }.to raise_error(NotImplementedError)
Kevin Clarkdc399732008-06-18 01:06:15 +000075 end
76 end
77
Roger Meier19dbbef2012-12-27 01:24:20 +010078 it 'should write out the different types' do
James E. King III27247072018-03-22 20:50:23 -040079 expect(@prot).to receive(:write_bool).with('bool').ordered
80 expect(@prot).to receive(:write_byte).with('byte').ordered
81 expect(@prot).to receive(:write_double).with('double').ordered
82 expect(@prot).to receive(:write_i16).with('i16').ordered
83 expect(@prot).to receive(:write_i32).with('i32').ordered
84 expect(@prot).to receive(:write_i64).with('i64').ordered
85 expect(@prot).to receive(:write_string).with('string').ordered
86 expect(@prot).to receive(:write_binary).with('binary').ordered
87 struct = double('Struct')
88 expect(struct).to receive(:write).with(@prot).ordered
Roger Meier19dbbef2012-12-27 01:24:20 +010089 @prot.write_type({:type => Thrift::Types::BOOL}, 'bool')
90 @prot.write_type({:type => Thrift::Types::BYTE}, 'byte')
91 @prot.write_type({:type => Thrift::Types::DOUBLE}, 'double')
92 @prot.write_type({:type => Thrift::Types::I16}, 'i16')
93 @prot.write_type({:type => Thrift::Types::I32}, 'i32')
94 @prot.write_type({:type => Thrift::Types::I64}, 'i64')
95 @prot.write_type({:type => Thrift::Types::STRING}, 'string')
96 @prot.write_type({:type => Thrift::Types::STRING, :binary => true}, 'binary')
97 @prot.write_type({:type => Thrift::Types::STRUCT}, struct)
98 # all other types are not implemented
99 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP, Thrift::Types::SET, Thrift::Types::LIST].each do |type|
100 expect { @prot.write_type({:type => type}, type.to_s) }.to raise_error(NotImplementedError)
101 end
102 end
103
104 it 'should read the different types (deprecated read_type signature)' do
James E. King III27247072018-03-22 20:50:23 -0400105 expect(@prot).to receive(:read_bool).ordered
106 expect(@prot).to receive(:read_byte).ordered
107 expect(@prot).to receive(:read_i16).ordered
108 expect(@prot).to receive(:read_i32).ordered
109 expect(@prot).to receive(:read_i64).ordered
110 expect(@prot).to receive(:read_double).ordered
111 expect(@prot).to receive(:read_string).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000112 @prot.read_type(Thrift::Types::BOOL)
113 @prot.read_type(Thrift::Types::BYTE)
114 @prot.read_type(Thrift::Types::I16)
115 @prot.read_type(Thrift::Types::I32)
116 @prot.read_type(Thrift::Types::I64)
117 @prot.read_type(Thrift::Types::DOUBLE)
118 @prot.read_type(Thrift::Types::STRING)
Kevin Clarkdc399732008-06-18 01:06:15 +0000119 # all other types are not implemented
Roger Meier19dbbef2012-12-27 01:24:20 +0100120 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP,
121 Thrift::Types::SET, Thrift::Types::LIST, Thrift::Types::STRUCT].each do |type|
122 expect { @prot.read_type(type) }.to raise_error(NotImplementedError)
123 end
124 end
125
126 it 'should read the different types' do
James E. King III27247072018-03-22 20:50:23 -0400127 expect(@prot).to receive(:read_bool).ordered
128 expect(@prot).to receive(:read_byte).ordered
129 expect(@prot).to receive(:read_i16).ordered
130 expect(@prot).to receive(:read_i32).ordered
131 expect(@prot).to receive(:read_i64).ordered
132 expect(@prot).to receive(:read_double).ordered
133 expect(@prot).to receive(:read_string).ordered
134 expect(@prot).to receive(:read_binary).ordered
Roger Meier19dbbef2012-12-27 01:24:20 +0100135 @prot.read_type({:type => Thrift::Types::BOOL})
136 @prot.read_type({:type => Thrift::Types::BYTE})
137 @prot.read_type({:type => Thrift::Types::I16})
138 @prot.read_type({:type => Thrift::Types::I32})
139 @prot.read_type({:type => Thrift::Types::I64})
140 @prot.read_type({:type => Thrift::Types::DOUBLE})
141 @prot.read_type({:type => Thrift::Types::STRING})
142 @prot.read_type({:type => Thrift::Types::STRING, :binary => true})
143 # all other types are not implemented
144 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP,
145 Thrift::Types::SET, Thrift::Types::LIST, Thrift::Types::STRUCT].each do |type|
146 expect { @prot.read_type({:type => type}) }.to raise_error(NotImplementedError)
Kevin Clarkdc399732008-06-18 01:06:15 +0000147 end
148 end
149
150 it "should skip the basic types" do
James E. King III27247072018-03-22 20:50:23 -0400151 expect(@prot).to receive(:read_bool).ordered
152 expect(@prot).to receive(:read_byte).ordered
153 expect(@prot).to receive(:read_i16).ordered
154 expect(@prot).to receive(:read_i32).ordered
155 expect(@prot).to receive(:read_i64).ordered
156 expect(@prot).to receive(:read_double).ordered
157 expect(@prot).to receive(:read_string).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000158 @prot.skip(Thrift::Types::BOOL)
159 @prot.skip(Thrift::Types::BYTE)
160 @prot.skip(Thrift::Types::I16)
161 @prot.skip(Thrift::Types::I32)
162 @prot.skip(Thrift::Types::I64)
163 @prot.skip(Thrift::Types::DOUBLE)
164 @prot.skip(Thrift::Types::STRING)
Kevin Clarkdc399732008-06-18 01:06:15 +0000165 end
166
167 it "should skip structs" do
168 real_skip = @prot.method(:skip)
James E. King III27247072018-03-22 20:50:23 -0400169 expect(@prot).to receive(:read_struct_begin).ordered
170 expect(@prot).to receive(:read_field_begin).exactly(4).times.and_return(
Jake Farrella87810f2012-09-28 01:59:04 +0000171 ['field 1', Thrift::Types::STRING, 1],
172 ['field 2', Thrift::Types::I32, 2],
173 ['field 3', Thrift::Types::MAP, 3],
174 [nil, Thrift::Types::STOP, 0]
Kevin Clarkdc399732008-06-18 01:06:15 +0000175 )
James E. King III27247072018-03-22 20:50:23 -0400176 expect(@prot).to receive(:read_field_end).exactly(3).times
177 expect(@prot).to receive(:read_string).exactly(3).times
178 expect(@prot).to receive(:read_i32).ordered
179 expect(@prot).to receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRING, 1])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000180 # @prot.should_receive(:read_string).exactly(2).times
James E. King III27247072018-03-22 20:50:23 -0400181 expect(@prot).to receive(:read_map_end).ordered
182 expect(@prot).to receive(:read_struct_end).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000183 real_skip.call(Thrift::Types::STRUCT)
Kevin Clarkdc399732008-06-18 01:06:15 +0000184 end
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000185
186 it "should skip maps" do
187 real_skip = @prot.method(:skip)
James E. King III27247072018-03-22 20:50:23 -0400188 expect(@prot).to receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRUCT, 1])
189 expect(@prot).to receive(:read_string).ordered
190 expect(@prot).to receive(:read_struct_begin).ordered.and_return(["some_struct"])
191 expect(@prot).to receive(:read_field_begin).ordered.and_return([nil, Thrift::Types::STOP, nil]);
192 expect(@prot).to receive(:read_struct_end).ordered
193 expect(@prot).to receive(:read_map_end).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000194 real_skip.call(Thrift::Types::MAP)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000195 end
196
197 it "should skip sets" do
198 real_skip = @prot.method(:skip)
James E. King III27247072018-03-22 20:50:23 -0400199 expect(@prot).to receive(:read_set_begin).ordered.and_return([Thrift::Types::I64, 9])
200 expect(@prot).to receive(:read_i64).ordered.exactly(9).times
201 expect(@prot).to receive(:read_set_end)
Jake Farrella87810f2012-09-28 01:59:04 +0000202 real_skip.call(Thrift::Types::SET)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000203 end
204
205 it "should skip lists" do
206 real_skip = @prot.method(:skip)
James E. King III27247072018-03-22 20:50:23 -0400207 expect(@prot).to receive(:read_list_begin).ordered.and_return([Thrift::Types::DOUBLE, 11])
208 expect(@prot).to receive(:read_double).ordered.exactly(11).times
209 expect(@prot).to receive(:read_list_end)
Jake Farrella87810f2012-09-28 01:59:04 +0000210 real_skip.call(Thrift::Types::LIST)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000211 end
212 end
213
Jake Farrella87810f2012-09-28 01:59:04 +0000214 describe Thrift::BaseProtocolFactory do
Bryan Duxburye8ae5d32009-03-24 05:23:52 +0000215 it "should raise NotImplementedError" do
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000216 # returning nil since Protocol is just an abstract class
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400217 expect { Thrift::BaseProtocolFactory.new.get_protocol(double("MockTransport")) }.to raise_error(NotImplementedError)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000218 end
James E. King III9aaf2952018-03-20 15:06:08 -0400219
220 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -0400221 expect(Thrift::BaseProtocolFactory.new.to_s).to eq("base")
James E. King III9aaf2952018-03-20 15:06:08 -0400222 end
Kevin Clarkdc399732008-06-18 01:06:15 +0000223 end
224end