blob: ec50c4823d66b83c7c5282fa40bc7db942114e5c [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
32 it "should make trans accessible" do
33 @prot.trans.should eql(@trans)
34 end
35
Roger Meier19dbbef2012-12-27 01:24:20 +010036 it 'should write out a field nicely (deprecated write_field signature)' do
Kevin Clarkdc399732008-06-18 01:06:15 +000037 @prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered
Roger Meier19dbbef2012-12-27 01:24:20 +010038 @prot.should_receive(:write_type).with({:name => 'field', :type => 'type'}, 'value').ordered
Kevin Clarkdc399732008-06-18 01:06:15 +000039 @prot.should_receive(:write_field_end).ordered
40 @prot.write_field('field', 'type', 'fid', 'value')
41 end
42
Roger Meier19dbbef2012-12-27 01:24:20 +010043 it 'should write out a field nicely' do
44 @prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered
45 @prot.should_receive(:write_type).with({:name => 'field', :type => 'type', :binary => false}, 'value').ordered
46 @prot.should_receive(:write_field_end).ordered
47 @prot.write_field({:name => 'field', :type => 'type', :binary => false}, 'fid', 'value')
48 end
49
50 it 'should write out the different types (deprecated write_type signature)' do
Kevin Clarkdc399732008-06-18 01:06:15 +000051 @prot.should_receive(:write_bool).with('bool').ordered
52 @prot.should_receive(:write_byte).with('byte').ordered
53 @prot.should_receive(:write_double).with('double').ordered
54 @prot.should_receive(:write_i16).with('i16').ordered
55 @prot.should_receive(:write_i32).with('i32').ordered
56 @prot.should_receive(:write_i64).with('i64').ordered
57 @prot.should_receive(:write_string).with('string').ordered
58 struct = mock('Struct')
59 struct.should_receive(:write).with(@prot).ordered
Jake Farrella87810f2012-09-28 01:59:04 +000060 @prot.write_type(Thrift::Types::BOOL, 'bool')
61 @prot.write_type(Thrift::Types::BYTE, 'byte')
62 @prot.write_type(Thrift::Types::DOUBLE, 'double')
63 @prot.write_type(Thrift::Types::I16, 'i16')
64 @prot.write_type(Thrift::Types::I32, 'i32')
65 @prot.write_type(Thrift::Types::I64, 'i64')
66 @prot.write_type(Thrift::Types::STRING, 'string')
67 @prot.write_type(Thrift::Types::STRUCT, struct)
Kevin Clarkdc399732008-06-18 01:06:15 +000068 # all other types are not implemented
Jake Farrella87810f2012-09-28 01:59:04 +000069 [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 +010070 expect { @prot.write_type(type, type.to_s) }.to raise_error(NotImplementedError)
Kevin Clarkdc399732008-06-18 01:06:15 +000071 end
72 end
73
Roger Meier19dbbef2012-12-27 01:24:20 +010074 it 'should write out the different types' do
75 @prot.should_receive(:write_bool).with('bool').ordered
76 @prot.should_receive(:write_byte).with('byte').ordered
77 @prot.should_receive(:write_double).with('double').ordered
78 @prot.should_receive(:write_i16).with('i16').ordered
79 @prot.should_receive(:write_i32).with('i32').ordered
80 @prot.should_receive(:write_i64).with('i64').ordered
81 @prot.should_receive(:write_string).with('string').ordered
82 @prot.should_receive(:write_binary).with('binary').ordered
83 struct = mock('Struct')
84 struct.should_receive(:write).with(@prot).ordered
85 @prot.write_type({:type => Thrift::Types::BOOL}, 'bool')
86 @prot.write_type({:type => Thrift::Types::BYTE}, 'byte')
87 @prot.write_type({:type => Thrift::Types::DOUBLE}, 'double')
88 @prot.write_type({:type => Thrift::Types::I16}, 'i16')
89 @prot.write_type({:type => Thrift::Types::I32}, 'i32')
90 @prot.write_type({:type => Thrift::Types::I64}, 'i64')
91 @prot.write_type({:type => Thrift::Types::STRING}, 'string')
92 @prot.write_type({:type => Thrift::Types::STRING, :binary => true}, 'binary')
93 @prot.write_type({:type => Thrift::Types::STRUCT}, struct)
94 # all other types are not implemented
95 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP, Thrift::Types::SET, Thrift::Types::LIST].each do |type|
96 expect { @prot.write_type({:type => type}, type.to_s) }.to raise_error(NotImplementedError)
97 end
98 end
99
100 it 'should read the different types (deprecated read_type signature)' do
Kevin Clarkdc399732008-06-18 01:06:15 +0000101 @prot.should_receive(:read_bool).ordered
102 @prot.should_receive(:read_byte).ordered
103 @prot.should_receive(:read_i16).ordered
104 @prot.should_receive(:read_i32).ordered
105 @prot.should_receive(:read_i64).ordered
106 @prot.should_receive(:read_double).ordered
107 @prot.should_receive(:read_string).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000108 @prot.read_type(Thrift::Types::BOOL)
109 @prot.read_type(Thrift::Types::BYTE)
110 @prot.read_type(Thrift::Types::I16)
111 @prot.read_type(Thrift::Types::I32)
112 @prot.read_type(Thrift::Types::I64)
113 @prot.read_type(Thrift::Types::DOUBLE)
114 @prot.read_type(Thrift::Types::STRING)
Kevin Clarkdc399732008-06-18 01:06:15 +0000115 # all other types are not implemented
Roger Meier19dbbef2012-12-27 01:24:20 +0100116 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP,
117 Thrift::Types::SET, Thrift::Types::LIST, Thrift::Types::STRUCT].each do |type|
118 expect { @prot.read_type(type) }.to raise_error(NotImplementedError)
119 end
120 end
121
122 it 'should read the different types' do
123 @prot.should_receive(:read_bool).ordered
124 @prot.should_receive(:read_byte).ordered
125 @prot.should_receive(:read_i16).ordered
126 @prot.should_receive(:read_i32).ordered
127 @prot.should_receive(:read_i64).ordered
128 @prot.should_receive(:read_double).ordered
129 @prot.should_receive(:read_string).ordered
130 @prot.should_receive(:read_binary).ordered
131 @prot.read_type({:type => Thrift::Types::BOOL})
132 @prot.read_type({:type => Thrift::Types::BYTE})
133 @prot.read_type({:type => Thrift::Types::I16})
134 @prot.read_type({:type => Thrift::Types::I32})
135 @prot.read_type({:type => Thrift::Types::I64})
136 @prot.read_type({:type => Thrift::Types::DOUBLE})
137 @prot.read_type({:type => Thrift::Types::STRING})
138 @prot.read_type({:type => Thrift::Types::STRING, :binary => true})
139 # all other types are not implemented
140 [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP,
141 Thrift::Types::SET, Thrift::Types::LIST, Thrift::Types::STRUCT].each do |type|
142 expect { @prot.read_type({:type => type}) }.to raise_error(NotImplementedError)
Kevin Clarkdc399732008-06-18 01:06:15 +0000143 end
144 end
145
146 it "should skip the basic types" do
147 @prot.should_receive(:read_bool).ordered
148 @prot.should_receive(:read_byte).ordered
149 @prot.should_receive(:read_i16).ordered
150 @prot.should_receive(:read_i32).ordered
151 @prot.should_receive(:read_i64).ordered
152 @prot.should_receive(:read_double).ordered
153 @prot.should_receive(:read_string).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000154 @prot.skip(Thrift::Types::BOOL)
155 @prot.skip(Thrift::Types::BYTE)
156 @prot.skip(Thrift::Types::I16)
157 @prot.skip(Thrift::Types::I32)
158 @prot.skip(Thrift::Types::I64)
159 @prot.skip(Thrift::Types::DOUBLE)
160 @prot.skip(Thrift::Types::STRING)
161 @prot.skip(Thrift::Types::STOP) # should do absolutely nothing
Kevin Clarkdc399732008-06-18 01:06:15 +0000162 end
163
164 it "should skip structs" do
165 real_skip = @prot.method(:skip)
166 @prot.should_receive(:read_struct_begin).ordered
167 @prot.should_receive(:read_field_begin).exactly(4).times.and_return(
Jake Farrella87810f2012-09-28 01:59:04 +0000168 ['field 1', Thrift::Types::STRING, 1],
169 ['field 2', Thrift::Types::I32, 2],
170 ['field 3', Thrift::Types::MAP, 3],
171 [nil, Thrift::Types::STOP, 0]
Kevin Clarkdc399732008-06-18 01:06:15 +0000172 )
Kevin Clarkdc399732008-06-18 01:06:15 +0000173 @prot.should_receive(:read_field_end).exactly(3).times
Bryan Duxburyc0166282009-02-02 00:48:17 +0000174 @prot.should_receive(:read_string).exactly(3).times
175 @prot.should_receive(:read_i32).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000176 @prot.should_receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRING, 1])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000177 # @prot.should_receive(:read_string).exactly(2).times
178 @prot.should_receive(:read_map_end).ordered
Kevin Clarkdc399732008-06-18 01:06:15 +0000179 @prot.should_receive(:read_struct_end).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000180 real_skip.call(Thrift::Types::STRUCT)
Kevin Clarkdc399732008-06-18 01:06:15 +0000181 end
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000182
183 it "should skip maps" do
184 real_skip = @prot.method(:skip)
Jake Farrella87810f2012-09-28 01:59:04 +0000185 @prot.should_receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRUCT, 1])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000186 @prot.should_receive(:read_string).ordered
187 @prot.should_receive(:read_struct_begin).ordered.and_return(["some_struct"])
Jake Farrella87810f2012-09-28 01:59:04 +0000188 @prot.should_receive(:read_field_begin).ordered.and_return([nil, Thrift::Types::STOP, nil]);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000189 @prot.should_receive(:read_struct_end).ordered
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000190 @prot.should_receive(:read_map_end).ordered
Jake Farrella87810f2012-09-28 01:59:04 +0000191 real_skip.call(Thrift::Types::MAP)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000192 end
193
194 it "should skip sets" do
195 real_skip = @prot.method(:skip)
Jake Farrella87810f2012-09-28 01:59:04 +0000196 @prot.should_receive(:read_set_begin).ordered.and_return([Thrift::Types::I64, 9])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000197 @prot.should_receive(:read_i64).ordered.exactly(9).times
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000198 @prot.should_receive(:read_set_end)
Jake Farrella87810f2012-09-28 01:59:04 +0000199 real_skip.call(Thrift::Types::SET)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000200 end
201
202 it "should skip lists" do
203 real_skip = @prot.method(:skip)
Jake Farrella87810f2012-09-28 01:59:04 +0000204 @prot.should_receive(:read_list_begin).ordered.and_return([Thrift::Types::DOUBLE, 11])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000205 @prot.should_receive(:read_double).ordered.exactly(11).times
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000206 @prot.should_receive(:read_list_end)
Jake Farrella87810f2012-09-28 01:59:04 +0000207 real_skip.call(Thrift::Types::LIST)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000208 end
209 end
210
Jake Farrella87810f2012-09-28 01:59:04 +0000211 describe Thrift::BaseProtocolFactory do
Bryan Duxburye8ae5d32009-03-24 05:23:52 +0000212 it "should raise NotImplementedError" do
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000213 # returning nil since Protocol is just an abstract class
Jake Farrella87810f2012-09-28 01:59:04 +0000214 lambda {Thrift::BaseProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000215 end
Kevin Clarkdc399732008-06-18 01:06:15 +0000216 end
217end