blob: d31e81193e50dfa0ff0c373c834dc41510d7d7e0 [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
24 before(:each) do
Kevin Clark9db1c2e2008-06-18 01:06:42 +000025 @trans = mock("MockTransport")
Jake Farrella87810f2012-09-28 01:59:04 +000026 @prot = Thrift::BaseProtocol.new(@trans)
Kevin Clarkdc399732008-06-18 01:06:15 +000027 end
28
Jake Farrella87810f2012-09-28 01:59:04 +000029 describe Thrift::BaseProtocol do
Kevin Clarkdc399732008-06-18 01:06:15 +000030 # most of the methods are stubs, so we can ignore them
Kevin Clark9db1c2e2008-06-18 01:06:42 +000031
James E. King III9aaf2952018-03-20 15:06:08 -040032 it "should provide a reasonable to_s" do
33 @trans.should_receive(:to_s).once.and_return("trans")
34 @prot.to_s.should == "trans"
35 end
36
Kevin Clark9db1c2e2008-06-18 01:06:42 +000037 it "should make trans accessible" do
38 @prot.trans.should eql(@trans)
39 end
40
Roger Meier19dbbef2012-12-27 01:24:20 +010041 it 'should write out a field nicely (deprecated write_field signature)' do
Kevin Clarkdc399732008-06-18 01:06:15 +000042 @prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered
Roger Meier19dbbef2012-12-27 01:24:20 +010043 @prot.should_receive(:write_type).with({:name => 'field', :type => 'type'}, 'value').ordered
Kevin Clarkdc399732008-06-18 01:06:15 +000044 @prot.should_receive(:write_field_end).ordered
45 @prot.write_field('field', 'type', 'fid', 'value')
46 end
47
Roger Meier19dbbef2012-12-27 01:24:20 +010048 it 'should write out a field nicely' do
49 @prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered
50 @prot.should_receive(:write_type).with({:name => 'field', :type => 'type', :binary => false}, 'value').ordered
51 @prot.should_receive(:write_field_end).ordered
52 @prot.write_field({:name => 'field', :type => 'type', :binary => false}, 'fid', 'value')
53 end
54
55 it 'should write out the different types (deprecated write_type signature)' do
Kevin Clarkdc399732008-06-18 01:06:15 +000056 @prot.should_receive(:write_bool).with('bool').ordered
57 @prot.should_receive(:write_byte).with('byte').ordered
58 @prot.should_receive(:write_double).with('double').ordered
59 @prot.should_receive(:write_i16).with('i16').ordered
60 @prot.should_receive(:write_i32).with('i32').ordered
61 @prot.should_receive(:write_i64).with('i64').ordered
62 @prot.should_receive(:write_string).with('string').ordered
63 struct = mock('Struct')
64 struct.should_receive(:write).with(@prot).ordered
Jake Farrella87810f2012-09-28 01:59:04 +000065 @prot.write_type(Thrift::Types::BOOL, 'bool')
66 @prot.write_type(Thrift::Types::BYTE, 'byte')
67 @prot.write_type(Thrift::Types::DOUBLE, 'double')
68 @prot.write_type(Thrift::Types::I16, 'i16')
69 @prot.write_type(Thrift::Types::I32, 'i32')
70 @prot.write_type(Thrift::Types::I64, 'i64')
71 @prot.write_type(Thrift::Types::STRING, 'string')
72 @prot.write_type(Thrift::Types::STRUCT, struct)
Kevin Clarkdc399732008-06-18 01:06:15 +000073 # all other types are not implemented
Jake Farrella87810f2012-09-28 01:59:04 +000074 [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 +010075 expect { @prot.write_type(type, type.to_s) }.to raise_error(NotImplementedError)
Kevin Clarkdc399732008-06-18 01:06:15 +000076 end
77 end
78
Roger Meier19dbbef2012-12-27 01:24:20 +010079 it 'should write out the different types' do
80 @prot.should_receive(:write_bool).with('bool').ordered
81 @prot.should_receive(:write_byte).with('byte').ordered
82 @prot.should_receive(:write_double).with('double').ordered
83 @prot.should_receive(:write_i16).with('i16').ordered
84 @prot.should_receive(:write_i32).with('i32').ordered
85 @prot.should_receive(:write_i64).with('i64').ordered
86 @prot.should_receive(:write_string).with('string').ordered
87 @prot.should_receive(:write_binary).with('binary').ordered
88 struct = mock('Struct')
89 struct.should_receive(:write).with(@prot).ordered
90 @prot.write_type({:type => Thrift::Types::BOOL}, 'bool')
91 @prot.write_type({:type => Thrift::Types::BYTE}, 'byte')
92 @prot.write_type({:type => Thrift::Types::DOUBLE}, 'double')
93 @prot.write_type({:type => Thrift::Types::I16}, 'i16')
94 @prot.write_type({:type => Thrift::Types::I32}, 'i32')
95 @prot.write_type({:type => Thrift::Types::I64}, 'i64')
96 @prot.write_type({:type => Thrift::Types::STRING}, 'string')
97 @prot.write_type({:type => Thrift::Types::STRING, :binary => true}, 'binary')
98 @prot.write_type({:type => Thrift::Types::STRUCT}, struct)
99 # all other types are not implemented
100 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP, Thrift::Types::SET, Thrift::Types::LIST].each do |type|
101 expect { @prot.write_type({:type => type}, type.to_s) }.to raise_error(NotImplementedError)
102 end
103 end
104
105 it 'should read the different types (deprecated read_type signature)' do
Kevin Clarkdc399732008-06-18 01:06:15 +0000106 @prot.should_receive(:read_bool).ordered
107 @prot.should_receive(:read_byte).ordered
108 @prot.should_receive(:read_i16).ordered
109 @prot.should_receive(:read_i32).ordered
110 @prot.should_receive(:read_i64).ordered
111 @prot.should_receive(:read_double).ordered
112 @prot.should_receive(:read_string).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000113 @prot.read_type(Thrift::Types::BOOL)
114 @prot.read_type(Thrift::Types::BYTE)
115 @prot.read_type(Thrift::Types::I16)
116 @prot.read_type(Thrift::Types::I32)
117 @prot.read_type(Thrift::Types::I64)
118 @prot.read_type(Thrift::Types::DOUBLE)
119 @prot.read_type(Thrift::Types::STRING)
Kevin Clarkdc399732008-06-18 01:06:15 +0000120 # all other types are not implemented
Roger Meier19dbbef2012-12-27 01:24:20 +0100121 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP,
122 Thrift::Types::SET, Thrift::Types::LIST, Thrift::Types::STRUCT].each do |type|
123 expect { @prot.read_type(type) }.to raise_error(NotImplementedError)
124 end
125 end
126
127 it 'should read the different types' do
128 @prot.should_receive(:read_bool).ordered
129 @prot.should_receive(:read_byte).ordered
130 @prot.should_receive(:read_i16).ordered
131 @prot.should_receive(:read_i32).ordered
132 @prot.should_receive(:read_i64).ordered
133 @prot.should_receive(:read_double).ordered
134 @prot.should_receive(:read_string).ordered
135 @prot.should_receive(:read_binary).ordered
136 @prot.read_type({:type => Thrift::Types::BOOL})
137 @prot.read_type({:type => Thrift::Types::BYTE})
138 @prot.read_type({:type => Thrift::Types::I16})
139 @prot.read_type({:type => Thrift::Types::I32})
140 @prot.read_type({:type => Thrift::Types::I64})
141 @prot.read_type({:type => Thrift::Types::DOUBLE})
142 @prot.read_type({:type => Thrift::Types::STRING})
143 @prot.read_type({:type => Thrift::Types::STRING, :binary => true})
144 # all other types are not implemented
145 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP,
146 Thrift::Types::SET, Thrift::Types::LIST, Thrift::Types::STRUCT].each do |type|
147 expect { @prot.read_type({:type => type}) }.to raise_error(NotImplementedError)
Kevin Clarkdc399732008-06-18 01:06:15 +0000148 end
149 end
150
151 it "should skip the basic types" do
152 @prot.should_receive(:read_bool).ordered
153 @prot.should_receive(:read_byte).ordered
154 @prot.should_receive(:read_i16).ordered
155 @prot.should_receive(:read_i32).ordered
156 @prot.should_receive(:read_i64).ordered
157 @prot.should_receive(:read_double).ordered
158 @prot.should_receive(:read_string).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000159 @prot.skip(Thrift::Types::BOOL)
160 @prot.skip(Thrift::Types::BYTE)
161 @prot.skip(Thrift::Types::I16)
162 @prot.skip(Thrift::Types::I32)
163 @prot.skip(Thrift::Types::I64)
164 @prot.skip(Thrift::Types::DOUBLE)
165 @prot.skip(Thrift::Types::STRING)
166 @prot.skip(Thrift::Types::STOP) # should do absolutely nothing
Kevin Clarkdc399732008-06-18 01:06:15 +0000167 end
168
169 it "should skip structs" do
170 real_skip = @prot.method(:skip)
171 @prot.should_receive(:read_struct_begin).ordered
172 @prot.should_receive(:read_field_begin).exactly(4).times.and_return(
Jake Farrella87810f2012-09-28 01:59:04 +0000173 ['field 1', Thrift::Types::STRING, 1],
174 ['field 2', Thrift::Types::I32, 2],
175 ['field 3', Thrift::Types::MAP, 3],
176 [nil, Thrift::Types::STOP, 0]
Kevin Clarkdc399732008-06-18 01:06:15 +0000177 )
Kevin Clarkdc399732008-06-18 01:06:15 +0000178 @prot.should_receive(:read_field_end).exactly(3).times
Bryan Duxburyc0166282009-02-02 00:48:17 +0000179 @prot.should_receive(:read_string).exactly(3).times
180 @prot.should_receive(:read_i32).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000181 @prot.should_receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRING, 1])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000182 # @prot.should_receive(:read_string).exactly(2).times
183 @prot.should_receive(:read_map_end).ordered
Kevin Clarkdc399732008-06-18 01:06:15 +0000184 @prot.should_receive(:read_struct_end).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000185 real_skip.call(Thrift::Types::STRUCT)
Kevin Clarkdc399732008-06-18 01:06:15 +0000186 end
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000187
188 it "should skip maps" do
189 real_skip = @prot.method(:skip)
Jake Farrella87810f2012-09-28 01:59:04 +0000190 @prot.should_receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRUCT, 1])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000191 @prot.should_receive(:read_string).ordered
192 @prot.should_receive(:read_struct_begin).ordered.and_return(["some_struct"])
Jake Farrella87810f2012-09-28 01:59:04 +0000193 @prot.should_receive(:read_field_begin).ordered.and_return([nil, Thrift::Types::STOP, nil]);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000194 @prot.should_receive(:read_struct_end).ordered
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000195 @prot.should_receive(:read_map_end).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000196 real_skip.call(Thrift::Types::MAP)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000197 end
198
199 it "should skip sets" do
200 real_skip = @prot.method(:skip)
Jake Farrella87810f2012-09-28 01:59:04 +0000201 @prot.should_receive(:read_set_begin).ordered.and_return([Thrift::Types::I64, 9])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000202 @prot.should_receive(:read_i64).ordered.exactly(9).times
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000203 @prot.should_receive(:read_set_end)
Jake Farrella87810f2012-09-28 01:59:04 +0000204 real_skip.call(Thrift::Types::SET)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000205 end
206
207 it "should skip lists" do
208 real_skip = @prot.method(:skip)
Jake Farrella87810f2012-09-28 01:59:04 +0000209 @prot.should_receive(:read_list_begin).ordered.and_return([Thrift::Types::DOUBLE, 11])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000210 @prot.should_receive(:read_double).ordered.exactly(11).times
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000211 @prot.should_receive(:read_list_end)
Jake Farrella87810f2012-09-28 01:59:04 +0000212 real_skip.call(Thrift::Types::LIST)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000213 end
214 end
215
Jake Farrella87810f2012-09-28 01:59:04 +0000216 describe Thrift::BaseProtocolFactory do
Bryan Duxburye8ae5d32009-03-24 05:23:52 +0000217 it "should raise NotImplementedError" do
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000218 # returning nil since Protocol is just an abstract class
Jake Farrella87810f2012-09-28 01:59:04 +0000219 lambda {Thrift::BaseProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000220 end
James E. King III9aaf2952018-03-20 15:06:08 -0400221
222 it "should provide a reasonable to_s" do
223 Thrift::BaseProtocolFactory.new.to_s.should == "base"
224 end
Kevin Clarkdc399732008-06-18 01:06:15 +0000225 end
226end