require 'test_helper' module RubyPsigate class ConnectionTest < Test::Unit::TestCase def setup @ok = stub(:code => 200, :message => 'OK', :body => 'success') @internal_server_error = stub(:code => 500, :message => 'Internal Server Error', :body => 'failure') @endpoint = "https://dev.psigate.com:7989/Messenger/XMLMessenger" @connection = RubyPsigate::Connection.new(@endpoint) end context "uri" do should "be parsed from endpoint" do assert_equal URI.parse(@endpoint), @connection.endpoint end should "connection parser should also accept uri directly" do endpoint = URI.parse(@endpoint) connection = RubyPsigate::Connection.new(endpoint) assert_equal endpoint, connection.endpoint end should "raise uri error" do assert_raises URI::InvalidURIError do RubyPsigate::Connection.new("hello world") end end end context "making requests" do should "be successful when making a POST request" do Net::HTTP.any_instance.expects(:post).with('/Messenger/XMLMessenger', 'data').returns(@ok) response = @connection.post('data') assert_equal 'success', response end should "raise error when encounter internal server error" do Net::HTTP.any_instance.stubs(:post).returns(@internal_server_error) assert_raises(RubyPsigate::ResponseError) do @connection.post('') end end end context "timeouts" do should "have a read timeout" do assert_equal RubyPsigate::Connection::READ_TIMEOUT, @connection.read_timeout end should "change the read timeout" do @connection.read_timeout = 33 assert_equal @connection.read_timeout, 33 end should "have an open timeout" do assert_equal RubyPsigate::Connection::OPEN_TIMEOUT, @connection.open_timeout end should "change the open timeout" do @connection.open_timeout = 5 assert_equal @connection.open_timeout, 5 end should "raise error on Timeout::Error" do Net::HTTP.any_instance.expects(:post).raises(Timeout::Error) @connection.retry_safe = false assert_raises(RubyPsigate::ConnectionError) do @connection.post('') end end should "raise error on Timeout::Error" do Net::HTTP.any_instance.expects(:post).raises(Timeout::Error) @connection.retry_safe = false assert_raises(RubyPsigate::ConnectionError) do @connection.post('') end end should "raise error on Errno::ETIMEDOUT" do Net::HTTP.any_instance.expects(:post).raises(Errno::ETIMEDOUT) @connection.retry_safe = false assert_raises(RubyPsigate::ConnectionError) do @connection.post('') end end end context "peer verification" do should "have a default value" do assert_equal RubyPsigate::Connection::VERIFY_PEER, @connection.verify_peer end should "be overridden" do @connection.verify_peer = false assert_equal @connection.verify_peer, false end should "raise error if cacert.pem (verification file) is missing" do File.expects(:exists?).returns(false) assert_raises(RubyPsigate::CertVerificationFileMissingError) do @connection.post('') end end end context "failures" do should "raise error when met with EOFError" do Net::HTTP.any_instance.expects(:post).raises(EOFError) @connection.retry_safe = false assert_raises(RubyPsigate::ConnectionError) do @connection.post('') end end should "retry after recoverable exception" do Net::HTTP.any_instance.expects(:post).times(2).raises(Errno::ECONNREFUSED).then.returns(@ok) assert_nothing_raised do @connection.post('') end end should "raise error when retry limit is reached" do Net::HTTP.any_instance.expects(:post).times(RubyPsigate::Connection::MAX_RETRIES).raises(Errno::ECONNREFUSED) assert_raises(RubyPsigate::ConnectionError) do @connection.post('') end end should "raise error after a mixture of errors raised" do Net::HTTP.any_instance.expects(:post).times(3).raises(Errno::ECONNRESET). raises(Errno::ECONNREFUSED). raises(EOFError) assert_raises(RubyPsigate::ConnectionError) do @connection.post('') end end end end end