From 3ad4b31dea53ccc25ae871df87c66094cbf9e12a Mon Sep 17 00:00:00 2001 From: Michael Lennox Date: Tue, 16 Jan 2018 09:14:33 +0000 Subject: [PATCH] This commit restores URI parsing functionality In https://github.com/gocardless/hutch/pull/159 functionality was added to hutch that meant that it would respect that when using `amqps://` as the URI scheme it would use TLS and also that when not specifying a port in the URI scheme it would default to the standard protocol ports (5671 for TLS connections, 5672 for non TLS connections). This functionality appears to have been lost since and is restored by this commit it also adds a new test case to prevent another regression. --- lib/hutch/broker.rb | 7 ++++++- spec/hutch/broker_spec.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/hutch/broker.rb b/lib/hutch/broker.rb index 8904c134..db0f050d 100644 --- a/lib/hutch/broker.rb +++ b/lib/hutch/broker.rb @@ -292,13 +292,18 @@ def parse_uri u = URI.parse(@config[:uri]) + @config[:mq_tls] = u.scheme == 'amqps' @config[:mq_host] = u.host - @config[:mq_port] = u.port + @config[:mq_port] = u.port || default_mq_port @config[:mq_vhost] = u.path.sub(/^\//, "") @config[:mq_username] = u.user @config[:mq_password] = u.password end + def default_mq_port + @config[:mq_tls] ? AMQ::Protocol::TLS_PORT : AMQ::Protocol::DEFAULT_PORT + end + def sanitized_uri p = connection_params scheme = p[:tls] ? "amqps" : "amqp" diff --git a/spec/hutch/broker_spec.rb b/spec/hutch/broker_spec.rb index d76d1716..7f293ca3 100644 --- a/spec/hutch/broker_spec.rb +++ b/spec/hutch/broker_spec.rb @@ -92,6 +92,36 @@ connection.close end + + context 'when configured with a URI' do + context 'which specifies the port' do + before { config[:uri] = 'amqp://guest:guest@127.0.0.1:5672/' } + + it 'successfully connects' do + expect { broker.open_connection }.not_to raise_error + end + end + + context 'which does not specify port and uses the amqp scheme' do + before { config[:uri] = 'amqp://guest:guest@127.0.0.1/' } + + it 'successfully connects' do + expect { broker.open_connection }.not_to raise_error + end + end + + context 'which specifies the amqps scheme' do + before { config[:uri] = 'amqps://guest:guest@127.0.0.1/' } + + it 'utilises TLS' do + expect(Hutch::Adapter).to receive(:new).with( + hash_including(tls: true, port: 5671) + ).and_return(instance_double('Hutch::Adapter', start: nil)) + + broker.open_connection + end + end + end end describe '#open_connection!' do