| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 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 Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 20 | require 'spec_helper' |
| Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 21 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 22 | describe 'Exception' do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 23 | describe Thrift::Exception do |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 24 | it "should have an accessible message" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 25 | e = Thrift::Exception.new("test message") |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 26 | expect(e.message).to eq("test message") |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 27 | end |
| Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 28 | end |
| Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 29 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 30 | describe Thrift::ApplicationException do |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 31 | it "should inherit from Thrift::Exception" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 32 | expect(Thrift::ApplicationException.superclass).to eq(Thrift::Exception) |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 33 | end |
| Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 34 | |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 35 | it "should have an accessible type and message" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 36 | e = Thrift::ApplicationException.new |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 37 | expect(e.type).to eq(Thrift::ApplicationException::UNKNOWN) |
| 38 | expect(e.message).to be_nil |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 39 | e = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, "test message") |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 40 | expect(e.type).to eq(Thrift::ApplicationException::UNKNOWN_METHOD) |
| 41 | expect(e.message).to eq("test message") |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 42 | end |
| 43 | |
| 44 | it "should read a struct off of a protocol" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 45 | prot = double("MockProtocol") |
| 46 | expect(prot).to receive(:read_struct_begin).ordered |
| 47 | expect(prot).to receive(:read_field_begin).exactly(3).times.and_return( |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 48 | ["message", Thrift::Types::STRING, 1], |
| 49 | ["type", Thrift::Types::I32, 2], |
| 50 | [nil, Thrift::Types::STOP, 0] |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 51 | ) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 52 | expect(prot).to receive(:read_string).ordered.and_return "test message" |
| 53 | expect(prot).to receive(:read_i32).ordered.and_return Thrift::ApplicationException::BAD_SEQUENCE_ID |
| 54 | expect(prot).to receive(:read_field_end).exactly(2).times |
| 55 | expect(prot).to receive(:read_struct_end).ordered |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 56 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 57 | e = Thrift::ApplicationException.new |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 58 | e.read(prot) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 59 | expect(e.message).to eq("test message") |
| 60 | expect(e.type).to eq(Thrift::ApplicationException::BAD_SEQUENCE_ID) |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 61 | end |
| 62 | |
| 63 | it "should skip bad fields when reading a struct" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 64 | prot = double("MockProtocol") |
| 65 | expect(prot).to receive(:read_struct_begin).ordered |
| 66 | expect(prot).to receive(:read_field_begin).exactly(5).times.and_return( |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 67 | ["type", Thrift::Types::I32, 2], |
| 68 | ["type", Thrift::Types::STRING, 2], |
| 69 | ["message", Thrift::Types::MAP, 1], |
| 70 | ["message", Thrift::Types::STRING, 3], |
| 71 | [nil, Thrift::Types::STOP, 0] |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 72 | ) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 73 | expect(prot).to receive(:read_i32).and_return Thrift::ApplicationException::INVALID_MESSAGE_TYPE |
| 74 | expect(prot).to receive(:skip).with(Thrift::Types::STRING).twice |
| 75 | expect(prot).to receive(:skip).with(Thrift::Types::MAP) |
| 76 | expect(prot).to receive(:read_field_end).exactly(4).times |
| 77 | expect(prot).to receive(:read_struct_end).ordered |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 78 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 79 | e = Thrift::ApplicationException.new |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 80 | e.read(prot) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 81 | expect(e.message).to be_nil |
| 82 | expect(e.type).to eq(Thrift::ApplicationException::INVALID_MESSAGE_TYPE) |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 83 | end |
| 84 | |
| 85 | it "should write a Thrift::ApplicationException struct to the oprot" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 86 | prot = double("MockProtocol") |
| 87 | expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered |
| 88 | expect(prot).to receive(:write_field_begin).with("message", Thrift::Types::STRING, 1).ordered |
| 89 | expect(prot).to receive(:write_string).with("test message").ordered |
| 90 | expect(prot).to receive(:write_field_begin).with("type", Thrift::Types::I32, 2).ordered |
| 91 | expect(prot).to receive(:write_i32).with(Thrift::ApplicationException::UNKNOWN_METHOD).ordered |
| 92 | expect(prot).to receive(:write_field_end).twice |
| 93 | expect(prot).to receive(:write_field_stop).ordered |
| 94 | expect(prot).to receive(:write_struct_end).ordered |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 95 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 96 | e = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, "test message") |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 97 | e.write(prot) |
| 98 | end |
| 99 | |
| 100 | it "should skip nil fields when writing to the oprot" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 101 | prot = double("MockProtocol") |
| 102 | expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered |
| 103 | expect(prot).to receive(:write_field_begin).with("message", Thrift::Types::STRING, 1).ordered |
| 104 | expect(prot).to receive(:write_string).with("test message").ordered |
| 105 | expect(prot).to receive(:write_field_end).ordered |
| 106 | expect(prot).to receive(:write_field_stop).ordered |
| 107 | expect(prot).to receive(:write_struct_end).ordered |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 108 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 109 | e = Thrift::ApplicationException.new(nil, "test message") |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 110 | e.write(prot) |
| 111 | |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 112 | prot = double("MockProtocol") |
| 113 | expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered |
| 114 | expect(prot).to receive(:write_field_begin).with("type", Thrift::Types::I32, 2).ordered |
| 115 | expect(prot).to receive(:write_i32).with(Thrift::ApplicationException::BAD_SEQUENCE_ID).ordered |
| 116 | expect(prot).to receive(:write_field_end).ordered |
| 117 | expect(prot).to receive(:write_field_stop).ordered |
| 118 | expect(prot).to receive(:write_struct_end).ordered |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 119 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 120 | e = Thrift::ApplicationException.new(Thrift::ApplicationException::BAD_SEQUENCE_ID) |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 121 | e.write(prot) |
| 122 | |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 123 | prot = double("MockProtocol") |
| 124 | expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered |
| 125 | expect(prot).to receive(:write_field_stop).ordered |
| 126 | expect(prot).to receive(:write_struct_end).ordered |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 127 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 128 | e = Thrift::ApplicationException.new(nil) |
| Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 129 | e.write(prot) |
| 130 | end |
| Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 131 | end |
| Kevin Clark | dfaada4 | 2008-06-18 01:06:01 +0000 | [diff] [blame] | 132 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 133 | describe Thrift::ProtocolException do |
| Kevin Clark | dfaada4 | 2008-06-18 01:06:01 +0000 | [diff] [blame] | 134 | it "should have an accessible type" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 135 | prot = Thrift::ProtocolException.new(Thrift::ProtocolException::SIZE_LIMIT, "message") |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 136 | expect(prot.type).to eq(Thrift::ProtocolException::SIZE_LIMIT) |
| 137 | expect(prot.message).to eq("message") |
| Kevin Clark | dfaada4 | 2008-06-18 01:06:01 +0000 | [diff] [blame] | 138 | end |
| 139 | end |
| Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 140 | end |