spec/amq/protocol/frame_spec.rb in amq-protocol-0.0.1.pre vs spec/amq/protocol/frame_spec.rb in amq-protocol-0.5.0

- old
+ new

@@ -1,82 +1,97 @@ # encoding: binary -require_relative "../../spec_helper.rb" -require "stringio" +require File.expand_path('../../../spec_helper', __FILE__) -describe AMQ::Protocol::Frame do - describe ".encode" do - it "should raise ConnectionError if type isn't one of: [:method, :header, :body, :heartbeat]" do - -> { Frame.encode(nil, 0, "") }.should raise_error(ConnectionError, "Must be one of [:method, :header, :body, :heartbeat]") - end - it "should raise RuntimeError if channel isn't 0 or an integer in range 1..65535" do - -> { Frame.encode(:method, -1, "") }.should raise_error(RuntimeError, "Channel has to be 0 or an integer in range 1..65535") - -> { Frame.encode(:method, 65536, "") }.should raise_error(RuntimeError, "Channel has to be 0 or an integer in range 1..65535") - -> { Frame.encode(:method, 65535, "") }.should_not raise_error(RuntimeError, "Channel has to be 0 or an integer in range 1..65535") - -> { Frame.encode(:method, 0, "") }.should_not raise_error(RuntimeError, "Channel has to be 0 or an integer in range 1..65535") - -> { Frame.encode(:method, 1, "") }.should_not raise_error(RuntimeError, "Channel has to be 0 or an integer in range 1..65535") - end +module AMQ + module Protocol + describe Frame do + describe ".encode" do + it "should raise FrameTypeError if type isn't one of: [:method, :header, :body, :heartbeat]" do + lambda { Frame.encode(nil, "", 0) }.should raise_error(FrameTypeError) + end - it "should raise RuntimeError if payload is nil" do - -> { Frame.encode(:method, 0, nil) }.should raise_error(RuntimeError, "Payload can't be nil") - end + it "should raise FrameTypeError if type isn't valid (when type is a symbol)" do + expect { Frame.encode(:xyz, "test", 12) }.to raise_error(FrameTypeError) + end - it "should encode type" do - Frame.encode(:body, 0, "").unpack("c").first.should eql(3) - end + it "should raise FrameTypeError if type isn't valid (when type is a number)" do + expect { Frame.encode(16, "test", 12) }.to raise_error(FrameTypeError) + end - it "should encode channel" do - Frame.encode(:body, 12, "").unpack("cn").last.should eql(12) - end + it "should raise RuntimeError if channel isn't 0 or an integer in range 1..65535" do + lambda { Frame.encode(:method, "", -1) }.should raise_error(RuntimeError, /^Channel has to be 0 or an integer in range 1\.\.65535/) + lambda { Frame.encode(:method, "", 65536) }.should raise_error(RuntimeError, /^Channel has to be 0 or an integer in range 1\.\.65535/) + lambda { Frame.encode(:method, "", 65535) }.should_not raise_error(RuntimeError, /^Channel has to be 0 or an integer in range 1\.\.65535/) + lambda { Frame.encode(:method, "", 0) }.should_not raise_error(RuntimeError, /^Channel has to be 0 or an integer in range 1\.\.65535/) + lambda { Frame.encode(:method, "", 1) }.should_not raise_error(RuntimeError, /^Channel has to be 0 or an integer in range 1\.\.65535/) + end - it "should encode size" do - Frame.encode(:body, 12, "test").unpack("cnN").last.should eql(4) - end + it "should raise RuntimeError if payload is nil" do + lambda { Frame.encode(:method, nil, 0) }.should raise_error(RuntimeError, "Payload can't be nil") + end - it "should include payload" do - Frame.encode(:body, 12, "test")[7..-2].should eql("test") - end + it "should encode type" do + Frame.encode(:body, "", 0).unpack("c").first.should eql(3) + end - it "should include final octet" do - Frame.encode(:body, 12, "test")[-1].should eql("\xCE") - end - end + it "should encode channel" do + Frame.encode(:body, "", 12).unpack("cn").last.should eql(12) + end - describe ".new" do - before(:each) do - @data = Frame.encode(:body, 5, "test") - @readable = StringIO.new(@data) - end + it "should encode size" do + Frame.encode(:body, "test", 12).unpack("cnN").last.should eql(4) + end - it "should decode type" do - Frame.decode(@readable).type.should eql(:body) - end + it "should include payload" do + Frame.encode(:body, "test", 12)[7..-2].should eql("test") + end - it "should decode size" do - Frame.decode(@readable).size.should eql(4) - end + it "should include final octet" do + Frame.encode(:body, "test", 12).should =~ /\xCE$/ + end - it "should decode channel" do - Frame.decode(@readable).channel.should eql(5) - end + it "should encode unicode strings" do + expect { Frame.encode(:body, "à bientôt!", 12) }.to_not raise_error + end + end - it "should decode payload" do - Frame.decode(@readable).payload.should eql("test") - end + describe ".new" do + it "should raise FrameTypeError if the type is not one of the accepted" do + expect { Frame.new(10) }.to raise_error(FrameTypeError) + end + end - it "should raise RuntimeError if the size is bigger than the actual size" do - pending - invalid_data = @data.dup - invalid_data[3..6] = [5].pack("N") - readable = StringIO.new(invalid_data) - -> { Frame.decode(readable) }.should raise_error(RuntimeError, "Frame doesn't end with \xCE as it must, which means the size is miscalculated.") - end + describe '#decode_header' do + it 'raises FrameTypeError if the decoded type is not one of the accepted' do + expect { Frame.decode_header("\n\x00\x01\x00\x00\x00\x05") }.to raise_error(FrameTypeError) + end - it "should raise RuntimeError if the size is smaller than the actual size" do - invalid_data = @data.dup - invalid_data[3..6] = [3].pack("N") - readable = StringIO.new(invalid_data) - -> { Frame.decode(readable) }.should raise_error(RuntimeError, "Frame doesn't end with \xCE as it must, which means the size is miscalculated.") + it 'raises EmptyResponseError if the header is nil' do + expect { Frame.decode_header(nil) }.to raise_error(EmptyResponseError) + end + end + + describe HeaderFrame do + subject { HeaderFrame.new("\x00<\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x98\x00\x18application/octet-stream\x02\x00", nil) } + + it "should decode body_size from payload" do + subject.body_size.should == 10 + end + + it "should decode klass_id from payload" do + subject.klass_id.should == 60 + end + + it "should decode weight from payload" do + subject.weight.should == 0 + end + + it "should decode properties from payload" do + subject.properties[:delivery_mode].should == 2 + subject.properties[:priority].should == 0 + end + end end end -end +end \ No newline at end of file