require 'spec_helper' module Punchblock describe Connection do let(:connection) { Connection.new :username => 1, :password => 1 } subject { connection } it 'should require a username and password to be passed in the options' do expect { Connection.new :password => 1 }.to raise_error ArgumentError expect { Connection.new :username => 1 }.to raise_error ArgumentError end it 'should properly set the Blather logger' do Connection.new :wire_logger => :foo, :username => 1, :password => 1 Blather.logger.should be :foo end its(:event_queue) { should be_a Queue } it "looking up original command by command ID" do offer = Event::Offer.new offer.call_id = '9f00061' offer.to = 'sip:whatever@127.0.0.1' say = <<-MSG 12/01/2011 MSG Component::Tropo::Say say = RayoNode.import parse_stanza(say).root connection.event_queue = [] connection.expects(:write_to_stream).once.returns true iq = Blather::Stanza::Iq.new :set, '9f00061@call.rayo.net' connection.expects(:create_iq).returns iq write_thread = Thread.new do connection.write offer.call_id, say end result = import_stanza <<-MSG MSG sleep 0.5 # Block so there's enough time for the write thread to get to the point where it's waiting on an IQ connection.__send__ :handle_iq_result, result write_thread.join say.state_name.should == :executing connection.original_component_from_id('fgh4590').should == say example_complete = import_stanza <<-MSG MSG connection.__send__ :handle_presence, example_complete say.complete_event.resource.source.should == say say.component_id.should == 'fgh4590' end describe '#handle_presence' do let :offer_xml do <<-MSG
MSG end let(:example_offer) { import_stanza offer_xml } it { example_offer.should be_a Blather::Stanza::Presence } let :complete_xml do <<-MSG MSG end let(:example_complete) { import_stanza complete_xml } it { example_complete.should be_a Blather::Stanza::Presence } describe "event placed on the event queue" do before do connection.event_queue = [] end describe "from an offer" do before do connection.__send__ :handle_presence, example_offer end subject { connection.event_queue.first } it { should be_instance_of Event::Offer } its(:call_id) { should == '9f00061' } it "should populate the call map with the domain for the call ID" do callmap = connection.instance_variable_get(:'@callmap') callmap['9f00061'].should == 'call.rayo.net' end end describe "from a complete" do before do connection.__send__ :handle_presence, example_complete end subject { connection.event_queue.first } it { should be_instance_of Event::Complete } its(:call_id) { should == '9f00061' } its(:connection) { should == connection } end describe "from something that's not a real event" do let :irrelevant_xml do <<-MSG MSG end let(:example_irrelevant_event) { import_stanza irrelevant_xml } before do lambda { connection.__send__ :handle_presence, example_irrelevant_event }.should throw_symbol(:pass) end subject { connection.event_queue } it { should be_empty } end end end describe "#handle_error" do let(:call_id) { "f6d437f4-1e18-457b-99f8-b5d853f50347" } let(:component_id) { 'abc123' } let :error_xml do <<-MSG Could not find call [id=f6d437f4-1e18-457b-99f8-b5d853f50347] MSG end let(:example_error) { import_stanza error_xml } let(:cmd) { Component::Output.new } before(:all) do cmd.request! connection.instance_variable_get(:'@iq_id_to_command')['blather000e'] = cmd connection.__send__ :handle_error, example_error end subject { cmd.response } its(:call_id) { should == call_id } its(:component_id) { should == component_id } its(:name) { should == :item_not_found } its(:text) { should == 'Could not find call [id=f6d437f4-1e18-457b-99f8-b5d853f50347]' } end end # describe Connection end # Punchblock