blob: 065f5ce29eaef907ac74b5575028aebb7069b447 [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'
Jake Farrell89414582011-11-10 00:53:17 +000021require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared")
Kevin Clarkf18b6432008-06-18 01:07:10 +000022
Jake Farrella87810f2012-09-28 01:59:04 +000023describe 'BinaryProtocol' do
Kevin Clarkf18b6432008-06-18 01:07:10 +000024
Jake Farrella87810f2012-09-28 01:59:04 +000025 it_should_behave_like 'a binary protocol'
Kevin Clarkf18b6432008-06-18 01:07:10 +000026
Jake Farrella87810f2012-09-28 01:59:04 +000027 def protocol_class
28 Thrift::BinaryProtocol
29 end
30
31 describe Thrift::BinaryProtocol do
32
33 before(:each) do
34 @trans = Thrift::MemoryBufferTransport.new
35 @prot = protocol_class.new(@trans)
Kevin Clarkf18b6432008-06-18 01:07:10 +000036 end
Kevin Clark5ae03842008-06-18 01:07:37 +000037
38 it "should read a message header" do
Bryan Duxburyad0ad822011-06-28 18:46:03 +000039 @trans.write([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY].pack('N'))
40 @trans.write([42].pack('N'))
James E. King III27247072018-03-22 20:50:23 -040041 expect(@prot).to receive(:read_string).and_return('testMessage')
42 expect(@prot.read_message_begin).to eq(['testMessage', Thrift::MessageTypes::REPLY, 42])
Kevin Clark5ae03842008-06-18 01:07:37 +000043 end
44
Kevin Clarkc13c33b2008-06-18 01:12:48 +000045 it "should raise an exception if the message header has the wrong version" do
James E. King III27247072018-03-22 20:50:23 -040046 expect(@prot).to receive(:read_i32).and_return(-1)
47 expect { @prot.read_message_begin }.to raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e|
David Reiss72754e12008-07-25 21:06:03 +000048 e.type == Thrift::ProtocolException::BAD_VERSION
Kevin Clark5ae03842008-06-18 01:07:37 +000049 end
50 end
Kevin Clarkead33822009-02-04 22:43:59 +000051
52 it "should raise an exception if the message header does not exist and strict_read is enabled" do
James E. King III27247072018-03-22 20:50:23 -040053 expect(@prot).to receive(:read_i32).and_return(42)
54 expect(@prot).to receive(:strict_read).and_return(true)
55 expect { @prot.read_message_begin }.to raise_error(Thrift::ProtocolException, 'No version identifier, old protocol client?') do |e|
Kevin Clarkead33822009-02-04 22:43:59 +000056 e.type == Thrift::ProtocolException::BAD_VERSION
57 end
58 end
James E. King III9aaf2952018-03-20 15:06:08 -040059
60 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -040061 expect(@prot.to_s).to eq("binary(memory)")
James E. King III9aaf2952018-03-20 15:06:08 -040062 end
Kevin Clarkf18b6432008-06-18 01:07:10 +000063 end
Kevin Clarke977a632008-06-18 01:07:50 +000064
Jake Farrella87810f2012-09-28 01:59:04 +000065 describe Thrift::BinaryProtocolFactory do
Kevin Clarke977a632008-06-18 01:07:50 +000066 it "should create a BinaryProtocol" do
James E. King III27247072018-03-22 20:50:23 -040067 expect(Thrift::BinaryProtocolFactory.new.get_protocol(double("MockTransport"))).to be_instance_of(Thrift::BinaryProtocol)
Kevin Clarke977a632008-06-18 01:07:50 +000068 end
James E. King III9aaf2952018-03-20 15:06:08 -040069
70 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -040071 expect(Thrift::BinaryProtocolFactory.new.to_s).to eq("binary")
James E. King III9aaf2952018-03-20 15:06:08 -040072 end
Kevin Clarke977a632008-06-18 01:07:50 +000073 end
Kevin Clarkf18b6432008-06-18 01:07:10 +000074end