blob: 3a0d691bbcea2c6d3861a575eb2ebd82cd095b26 [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
Kevin Clarkf18b6432008-06-18 01:07:10 +000020require File.dirname(__FILE__) + '/spec_helper'
21require 'thrift/protocol/binaryprotocol'
David Reiss72754e12008-07-25 21:06:03 +000022require File.dirname(__FILE__) + '/binaryprotocol_spec_shared'
Kevin Clarkf18b6432008-06-18 01:07:10 +000023
Kevin Clark356f8612008-06-18 01:08:05 +000024class ThriftBinaryProtocolSpec < Spec::ExampleGroup
Kevin Clarkf18b6432008-06-18 01:07:10 +000025 include Thrift
26
27 describe BinaryProtocol do
David Reiss72754e12008-07-25 21:06:03 +000028 it_should_behave_like 'a binary protocol'
Kevin Clarkf18b6432008-06-18 01:07:10 +000029
David Reiss72754e12008-07-25 21:06:03 +000030 def protocol_class
31 BinaryProtocol
Kevin Clarkf18b6432008-06-18 01:07:10 +000032 end
Kevin Clark5ae03842008-06-18 01:07:37 +000033
34 it "should read a message header" do
Kevin Clarkead33822009-02-04 22:43:59 +000035 @trans.should_receive(:read_all).exactly(2).times.and_return(
36 [protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY].pack('N'),
37 [42].pack('N')
38 )
Kevin Clark5ae03842008-06-18 01:07:37 +000039 @prot.should_receive(:read_string).and_return('testMessage')
David Reiss72754e12008-07-25 21:06:03 +000040 @prot.read_message_begin.should == ['testMessage', Thrift::MessageTypes::REPLY, 42]
Kevin Clark5ae03842008-06-18 01:07:37 +000041 end
42
Kevin Clarkc13c33b2008-06-18 01:12:48 +000043 it "should raise an exception if the message header has the wrong version" do
Kevin Clarkead33822009-02-04 22:43:59 +000044 @prot.should_receive(:read_i32).and_return(-1)
David Reiss72754e12008-07-25 21:06:03 +000045 lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e|
46 e.type == Thrift::ProtocolException::BAD_VERSION
Kevin Clark5ae03842008-06-18 01:07:37 +000047 end
48 end
Kevin Clarkead33822009-02-04 22:43:59 +000049
50 it "should raise an exception if the message header does not exist and strict_read is enabled" do
51 @prot.should_receive(:read_i32).and_return(42)
52 @prot.should_receive(:strict_read).and_return(true)
53 lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'No version identifier, old protocol client?') do |e|
54 e.type == Thrift::ProtocolException::BAD_VERSION
55 end
56 end
Kevin Clarkf18b6432008-06-18 01:07:10 +000057 end
Kevin Clarke977a632008-06-18 01:07:50 +000058
59 describe BinaryProtocolFactory do
60 it "should create a BinaryProtocol" do
Kevin Clark90a2cbe2008-06-18 01:15:53 +000061 BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocol)
Kevin Clarke977a632008-06-18 01:07:50 +000062 end
63 end
Kevin Clarkf18b6432008-06-18 01:07:10 +000064end