require 'spec_helper' describe Protobuf::Rpc::Middleware::ResponseEncoder do let(:app) { Proc.new { |env| env.response = response; env } } let(:env) { Protobuf::Rpc::Env.new( 'response_type' => Test::Resource, 'log_signature' => 'log_signature' ) } let(:encoded_response) { response_wrapper.encode } let(:response) { Test::Resource.new(:name => 'required') } let(:response_wrapper) { Protobuf::Socketrpc::Response.new(:response_proto => response) } subject { described_class.new(app) } describe "#call" do it "encodes the response" do stack_env = subject.call(env) stack_env.encoded_response.should eq encoded_response end it "calls the stack" do stack_env = subject.call(env) stack_env.response.should eq response end context "when response is responds to :to_hash" do let(:app) { proc { |env| env.response = hashable; env } } let(:hashable) { double('hashable', :to_hash => response.to_hash) } it "sets Env#response" do stack_env = subject.call(env) stack_env.response.should eq response end end context "when response is responds to :to_proto" do let(:app) { proc { |env| env.response = protoable; env } } let(:protoable) { double('protoable', :to_proto => response) } it "sets Env#response" do stack_env = subject.call(env) stack_env.response.should eq response end end context "when response is not a valid response type" do let(:app) { proc { |env| env.response = "I'm not a valid response"; env } } it "raises a bad response proto exception" do expect { subject.call(env) }.to raise_exception(Protobuf::Rpc::BadResponseProto) end end context "when response is a Protobuf error" do let(:app) { proc { |env| env.response = error; env } } let(:error) { Protobuf::Rpc::RpcError.new } let(:response_wrapper) { error.to_response } it "wraps and encodes the response" do stack_env = subject.call(env) stack_env.encoded_response.should eq encoded_response end end context "when encoding fails" do before { Protobuf::Socketrpc::Response.any_instance.stub(:encode).and_raise(RuntimeError) } it "raises a bad request data exception" do expect { subject.call(env) }.to raise_exception(Protobuf::Rpc::PbError) end end end end