David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | require 'thrift/protocol/binaryprotocolaccelerated' |
| 3 | require File.dirname(__FILE__) + '/binaryprotocol_spec_shared' |
| 4 | require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types' |
| 5 | |
| 6 | class ThriftBinaryProtocolAcceleratedSpec < Spec::ExampleGroup |
| 7 | include Thrift |
| 8 | |
| 9 | describe BinaryProtocolAccelerated do |
| 10 | # given that BinaryProtocolAccelerated only actually overrides read_message_begin |
| 11 | # this shared spec isn't going to do much, but it's still worth including |
| 12 | # for future-proofing in case we start overriding individual methods |
| 13 | it_should_behave_like 'a binary protocol' |
| 14 | |
| 15 | def protocol_class |
| 16 | BinaryProtocolAccelerated |
| 17 | end |
| 18 | |
| 19 | before(:each) do |
| 20 | @buffer = "" |
| 21 | @trans.stub!(:borrow).and_return { @buffer } |
| 22 | @trans.stub!(:consume!).and_return do |*args| |
| 23 | n = args.first || 0 |
| 24 | @buffer.slice!(0,n) |
| 25 | end |
| 26 | end |
| 27 | |
| 28 | it "should read a message header" do |
| 29 | @buffer = "\200\001\000\002\000\000\000\vtestMessage\000\000\000*" |
| 30 | # @prot.should_receive(:read_i32).and_return(protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY, 42) |
| 31 | # @prot.should_receive(:read_string).and_return('testMessage') |
| 32 | @prot.read_message_begin.should == ['testMessage', Thrift::MessageTypes::REPLY, 42] |
| 33 | end |
| 34 | |
| 35 | it "should raise an exception if the message header has the wrong version" do |
| 36 | @buffer = "\000\000\000\v" |
| 37 | # @prot.should_receive(:read_i32).and_return(42) |
| 38 | lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e| |
| 39 | e.type == Thrift::ProtocolException::BAD_VERSION |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | it "should encode a struct with all fields set identically to Thrift::BinaryProtocol" do |
| 44 | foo = SpecNamespace::Foo.new(:complex => {5 => {"foo" => 1.2}, 17 => {"bar" => 3.14159, "baz" => 5.8}}) |
| 45 | @prot.encode_binary(foo).should == "\r\000\005\b\r\000\000\000\002\000\000\000\005\v\004\000\000\000\001\000\ |
| 46 | \000\000\003foo?\363333333\000\000\000\021\v\004\000\000\000\002\000\000\000\003baz@\027333333\000\000\000\003bar@\ |
| 47 | \t!\371\360\e\206n\016\000\006\006\000\000\000\003\000\005\000\021\000\357\b\000\001\000\000\0005\v\000\002\000\000\ |
| 48 | \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\017\000\004\b\000\000\000\004\000\000\000\001\000\ |
| 49 | \000\000\002\000\000\000\002\000\000\000\003\000" |
| 50 | end |
| 51 | |
| 52 | it "should encode a struct with missing fields identically to Thrift::BinaryProtocol" do |
| 53 | foo = SpecNamespace::Foo.new(:simple => nil, :ints => nil) |
| 54 | @prot.encode_binary(foo).should == "\016\000\006\006\000\000\000\003\000\005\000\021\000\357\v\000\002\000\000\ |
| 55 | \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\000" |
| 56 | end |
| 57 | |
| 58 | it "should decode a struct with all fields set identically to Thrift::BinaryProtocol" do |
| 59 | foo = SpecNamespace::Foo.new(:complex => {5 => {"foo" => 1.2}, 17 => {"bar" => 3.14159, "baz" => 5.8}}) |
| 60 | trans = Thrift::MemoryBuffer.new("\r\000\005\b\r\000\000\000\002\000\000\000\005\v\004\000\000\000\001\000\ |
| 61 | \000\000\003foo?\363333333\000\000\000\021\v\004\000\000\000\002\000\000\000\003baz@\027333333\000\000\000\003bar@\ |
| 62 | \t!\371\360\e\206n\016\000\006\006\000\000\000\003\000\005\000\021\000\357\b\000\001\000\000\0005\v\000\002\000\000\ |
| 63 | \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\017\000\004\b\000\000\000\004\000\000\000\001\000\ |
| 64 | \000\000\002\000\000\000\002\000\000\000\003\000") |
| 65 | @prot.decode_binary(SpecNamespace::Foo.new, trans).should == foo |
| 66 | end |
| 67 | |
| 68 | it "should decode a struct with missing fields identically to Thrift::BinaryProtocol" do |
| 69 | trans = Thrift::MemoryBuffer.new("\016\000\006\006\000\000\000\003\000\005\000\021\000\357\v\000\002\000\000\ |
| 70 | \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\000") |
| 71 | @prot.decode_binary(SpecNamespace::Foo.new, trans).should == SpecNamespace::Foo.new |
| 72 | end |
David Reiss | 0c7d38c | 2008-07-25 21:06:06 +0000 | [diff] [blame] | 73 | |
| 74 | it "should encode a string with null bytes in it" do |
| 75 | foo = SpecNamespace::Hello.new(:greeting => "Hello\000World!") |
| 76 | @prot.encode_binary(foo).should == "\v\000\001\000\000\000\fHello\000World!\000" |
| 77 | end |
| 78 | |
| 79 | it "should decode a string with null bytes in it" do |
| 80 | trans = Thrift::MemoryBuffer.new("\v\000\001\000\000\000\fHello\000World!\000") |
| 81 | @prot.decode_binary(SpecNamespace::Hello.new, trans).should == SpecNamespace::Hello.new(:greeting => "Hello\000World!") |
| 82 | end |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 83 | end |
| 84 | |
| 85 | describe BinaryProtocolAcceleratedFactory do |
| 86 | it "should create a BinaryProtocolAccelerated" do |
| 87 | BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocolAccelerated) |
| 88 | end |
| 89 | end |
| 90 | end |