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' |
Jake Farrell | 8941458 | 2011-11-10 00:53:17 +0000 | [diff] [blame] | 21 | require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared") |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 22 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 23 | describe 'BinaryProtocol' do |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 24 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 25 | it_should_behave_like 'a binary protocol' |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 26 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 27 | 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 Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 36 | end |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 37 | |
| 38 | it "should read a message header" do |
Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 39 | @trans.write([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY].pack('N')) |
| 40 | @trans.write([42].pack('N')) |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 41 | @prot.should_receive(:read_string).and_return('testMessage') |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 42 | @prot.read_message_begin.should == ['testMessage', Thrift::MessageTypes::REPLY, 42] |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 43 | end |
| 44 | |
Kevin Clark | c13c33b | 2008-06-18 01:12:48 +0000 | [diff] [blame] | 45 | it "should raise an exception if the message header has the wrong version" do |
Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 46 | @prot.should_receive(:read_i32).and_return(-1) |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 47 | lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e| |
| 48 | e.type == Thrift::ProtocolException::BAD_VERSION |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 49 | end |
| 50 | end |
Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 51 | |
| 52 | it "should raise an exception if the message header does not exist and strict_read is enabled" do |
| 53 | @prot.should_receive(:read_i32).and_return(42) |
| 54 | @prot.should_receive(:strict_read).and_return(true) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 55 | lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'No version identifier, old protocol client?') do |e| |
Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 56 | e.type == Thrift::ProtocolException::BAD_VERSION |
| 57 | end |
| 58 | end |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 59 | |
| 60 | it "should provide a reasonable to_s" do |
| 61 | @prot.to_s.should == "binary(memory)" |
| 62 | end |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 63 | end |
Kevin Clark | e977a63 | 2008-06-18 01:07:50 +0000 | [diff] [blame] | 64 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 65 | describe Thrift::BinaryProtocolFactory do |
Kevin Clark | e977a63 | 2008-06-18 01:07:50 +0000 | [diff] [blame] | 66 | it "should create a BinaryProtocol" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 67 | Thrift::BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::BinaryProtocol) |
Kevin Clark | e977a63 | 2008-06-18 01:07:50 +0000 | [diff] [blame] | 68 | end |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 69 | |
| 70 | it "should provide a reasonable to_s" do |
| 71 | Thrift::BinaryProtocolFactory.new.to_s.should == "binary" |
| 72 | end |
Kevin Clark | e977a63 | 2008-06-18 01:07:50 +0000 | [diff] [blame] | 73 | end |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 74 | end |