blob: 945d059f2c3ae81f84a026a85432b28f0432f673 [file] [log] [blame]
Kevin Clark0ff9e8c2008-06-18 01:05:03 +00001require File.dirname(__FILE__) + '/spec_helper'
2
3class ThriftSpec < Spec::ExampleGroup
4 include Thrift
5
6 class ClientSpec
7 include Thrift::Client
8 end
9
10 before(:each) do
11 @prot = mock("MockProtocol")
12 @client = ClientSpec.new(@prot)
13 end
14
15 describe "Client" do
16 it "should re-use iprot for oprot if not otherwise specified" do
17 @client.instance_variable_get(:'@iprot').should eql(@prot)
18 @client.instance_variable_get(:'@oprot').should eql(@prot)
19 end
20
21 it "should send a test message" do
22 @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::CALL, 0)
23 mock_args = mock('#<TestMessage_args:mock>')
24 mock_args.should_receive(:foo=).with('foo')
25 mock_args.should_receive(:bar=).with(42)
26 mock_args.should_receive(:write).with(@prot)
27 @prot.should_receive(:write_message_end)
28 @prot.should_receive(:trans) do
29 mock('trans').tee do |trans|
30 trans.should_receive(:flush)
31 end
32 end
33 klass = stub("TestMessage_args", :new => mock_args)
34 @client.send_message('testMessage', klass, :foo => 'foo', :bar => 42)
35 end
36
37 it "should receive a test message" do
38 @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::CALL, 0]
39 @prot.should_receive(:read_message_end)
40 mock_klass = mock("#<MockClass:mock>")
41 mock_klass.should_receive(:read).with(@prot)
42 @client.receive_message(stub("MockClass", :new => mock_klass))
43 end
44
45 it "should handle received exceptions" do
46 @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::EXCEPTION, 0]
47 @prot.should_receive(:read_message_end)
48 ApplicationException.should_receive(:new).and_return do
49 StandardError.new.tee do |mock_exc|
50 mock_exc.should_receive(:read).with(@prot)
51 end
52 end
53 lambda { @client.receive_message(nil) }.should raise_error(StandardError)
54 end
55 end
56end