blob: 379ae69805b4ed354a7fc3974ebf25a376e12cff [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 Clark95833c52008-06-18 01:04:34 +000021
Jake Farrella87810f2012-09-28 01:59:04 +000022describe 'Exception' do
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000023
Jake Farrella87810f2012-09-28 01:59:04 +000024 describe Thrift::Exception do
Kevin Clark61387bf2008-06-18 01:04:48 +000025 it "should have an accessible message" do
Jake Farrella87810f2012-09-28 01:59:04 +000026 e = Thrift::Exception.new("test message")
James E. King III27247072018-03-22 20:50:23 -040027 expect(e.message).to eq("test message")
Kevin Clark61387bf2008-06-18 01:04:48 +000028 end
Kevin Clark95833c52008-06-18 01:04:34 +000029 end
Kevin Clark95833c52008-06-18 01:04:34 +000030
Jake Farrella87810f2012-09-28 01:59:04 +000031 describe Thrift::ApplicationException do
Kevin Clark61387bf2008-06-18 01:04:48 +000032 it "should inherit from Thrift::Exception" do
James E. King III27247072018-03-22 20:50:23 -040033 expect(Thrift::ApplicationException.superclass).to eq(Thrift::Exception)
Kevin Clark61387bf2008-06-18 01:04:48 +000034 end
Kevin Clark95833c52008-06-18 01:04:34 +000035
Kevin Clark61387bf2008-06-18 01:04:48 +000036 it "should have an accessible type and message" do
Jake Farrella87810f2012-09-28 01:59:04 +000037 e = Thrift::ApplicationException.new
James E. King III27247072018-03-22 20:50:23 -040038 expect(e.type).to eq(Thrift::ApplicationException::UNKNOWN)
39 expect(e.message).to be_nil
Jake Farrella87810f2012-09-28 01:59:04 +000040 e = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, "test message")
James E. King III27247072018-03-22 20:50:23 -040041 expect(e.type).to eq(Thrift::ApplicationException::UNKNOWN_METHOD)
42 expect(e.message).to eq("test message")
Kevin Clark61387bf2008-06-18 01:04:48 +000043 end
44
45 it "should read a struct off of a protocol" do
James E. King III27247072018-03-22 20:50:23 -040046 prot = double("MockProtocol")
47 expect(prot).to receive(:read_struct_begin).ordered
48 expect(prot).to receive(:read_field_begin).exactly(3).times.and_return(
Jake Farrella87810f2012-09-28 01:59:04 +000049 ["message", Thrift::Types::STRING, 1],
50 ["type", Thrift::Types::I32, 2],
51 [nil, Thrift::Types::STOP, 0]
Kevin Clark61387bf2008-06-18 01:04:48 +000052 )
James E. King III27247072018-03-22 20:50:23 -040053 expect(prot).to receive(:read_string).ordered.and_return "test message"
54 expect(prot).to receive(:read_i32).ordered.and_return Thrift::ApplicationException::BAD_SEQUENCE_ID
55 expect(prot).to receive(:read_field_end).exactly(2).times
56 expect(prot).to receive(:read_struct_end).ordered
Kevin Clark61387bf2008-06-18 01:04:48 +000057
Jake Farrella87810f2012-09-28 01:59:04 +000058 e = Thrift::ApplicationException.new
Kevin Clark61387bf2008-06-18 01:04:48 +000059 e.read(prot)
James E. King III27247072018-03-22 20:50:23 -040060 expect(e.message).to eq("test message")
61 expect(e.type).to eq(Thrift::ApplicationException::BAD_SEQUENCE_ID)
Kevin Clark61387bf2008-06-18 01:04:48 +000062 end
63
64 it "should skip bad fields when reading a struct" do
James E. King III27247072018-03-22 20:50:23 -040065 prot = double("MockProtocol")
66 expect(prot).to receive(:read_struct_begin).ordered
67 expect(prot).to receive(:read_field_begin).exactly(5).times.and_return(
Jake Farrella87810f2012-09-28 01:59:04 +000068 ["type", Thrift::Types::I32, 2],
69 ["type", Thrift::Types::STRING, 2],
70 ["message", Thrift::Types::MAP, 1],
71 ["message", Thrift::Types::STRING, 3],
72 [nil, Thrift::Types::STOP, 0]
Kevin Clark61387bf2008-06-18 01:04:48 +000073 )
James E. King III27247072018-03-22 20:50:23 -040074 expect(prot).to receive(:read_i32).and_return Thrift::ApplicationException::INVALID_MESSAGE_TYPE
75 expect(prot).to receive(:skip).with(Thrift::Types::STRING).twice
76 expect(prot).to receive(:skip).with(Thrift::Types::MAP)
77 expect(prot).to receive(:read_field_end).exactly(4).times
78 expect(prot).to receive(:read_struct_end).ordered
Kevin Clark61387bf2008-06-18 01:04:48 +000079
Jake Farrella87810f2012-09-28 01:59:04 +000080 e = Thrift::ApplicationException.new
Kevin Clark61387bf2008-06-18 01:04:48 +000081 e.read(prot)
James E. King III27247072018-03-22 20:50:23 -040082 expect(e.message).to be_nil
83 expect(e.type).to eq(Thrift::ApplicationException::INVALID_MESSAGE_TYPE)
Kevin Clark61387bf2008-06-18 01:04:48 +000084 end
85
86 it "should write a Thrift::ApplicationException struct to the oprot" do
James E. King III27247072018-03-22 20:50:23 -040087 prot = double("MockProtocol")
88 expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
89 expect(prot).to receive(:write_field_begin).with("message", Thrift::Types::STRING, 1).ordered
90 expect(prot).to receive(:write_string).with("test message").ordered
91 expect(prot).to receive(:write_field_begin).with("type", Thrift::Types::I32, 2).ordered
92 expect(prot).to receive(:write_i32).with(Thrift::ApplicationException::UNKNOWN_METHOD).ordered
93 expect(prot).to receive(:write_field_end).twice
94 expect(prot).to receive(:write_field_stop).ordered
95 expect(prot).to receive(:write_struct_end).ordered
Kevin Clark61387bf2008-06-18 01:04:48 +000096
Jake Farrella87810f2012-09-28 01:59:04 +000097 e = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, "test message")
Kevin Clark61387bf2008-06-18 01:04:48 +000098 e.write(prot)
99 end
100
101 it "should skip nil fields when writing to the oprot" do
James E. King III27247072018-03-22 20:50:23 -0400102 prot = double("MockProtocol")
103 expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
104 expect(prot).to receive(:write_field_begin).with("message", Thrift::Types::STRING, 1).ordered
105 expect(prot).to receive(:write_string).with("test message").ordered
106 expect(prot).to receive(:write_field_end).ordered
107 expect(prot).to receive(:write_field_stop).ordered
108 expect(prot).to receive(:write_struct_end).ordered
Kevin Clark61387bf2008-06-18 01:04:48 +0000109
Jake Farrella87810f2012-09-28 01:59:04 +0000110 e = Thrift::ApplicationException.new(nil, "test message")
Kevin Clark61387bf2008-06-18 01:04:48 +0000111 e.write(prot)
112
James E. King III27247072018-03-22 20:50:23 -0400113 prot = double("MockProtocol")
114 expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
115 expect(prot).to receive(:write_field_begin).with("type", Thrift::Types::I32, 2).ordered
116 expect(prot).to receive(:write_i32).with(Thrift::ApplicationException::BAD_SEQUENCE_ID).ordered
117 expect(prot).to receive(:write_field_end).ordered
118 expect(prot).to receive(:write_field_stop).ordered
119 expect(prot).to receive(:write_struct_end).ordered
Kevin Clark61387bf2008-06-18 01:04:48 +0000120
Jake Farrella87810f2012-09-28 01:59:04 +0000121 e = Thrift::ApplicationException.new(Thrift::ApplicationException::BAD_SEQUENCE_ID)
Kevin Clark61387bf2008-06-18 01:04:48 +0000122 e.write(prot)
123
James E. King III27247072018-03-22 20:50:23 -0400124 prot = double("MockProtocol")
125 expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
126 expect(prot).to receive(:write_field_stop).ordered
127 expect(prot).to receive(:write_struct_end).ordered
Kevin Clark61387bf2008-06-18 01:04:48 +0000128
Jake Farrella87810f2012-09-28 01:59:04 +0000129 e = Thrift::ApplicationException.new(nil)
Kevin Clark61387bf2008-06-18 01:04:48 +0000130 e.write(prot)
131 end
Kevin Clark95833c52008-06-18 01:04:34 +0000132 end
Kevin Clarkdfaada42008-06-18 01:06:01 +0000133
Jake Farrella87810f2012-09-28 01:59:04 +0000134 describe Thrift::ProtocolException do
Kevin Clarkdfaada42008-06-18 01:06:01 +0000135 it "should have an accessible type" do
Jake Farrella87810f2012-09-28 01:59:04 +0000136 prot = Thrift::ProtocolException.new(Thrift::ProtocolException::SIZE_LIMIT, "message")
James E. King III27247072018-03-22 20:50:23 -0400137 expect(prot.type).to eq(Thrift::ProtocolException::SIZE_LIMIT)
138 expect(prot.message).to eq("message")
Kevin Clarkdfaada42008-06-18 01:06:01 +0000139 end
140 end
Kevin Clark95833c52008-06-18 01:04:34 +0000141end