Skip to content
Browse files

Extract AMQ::URI from AMQ::Settings

  • Loading branch information...
1 parent 56a330b commit 43a3c3c779df69fc91f832a777bdab3ddf2cfe01 Michael Klishin committed
Showing with 134 additions and 111 deletions.
  1. +2 −20 lib/amq/settings.rb
  2. +37 −0 lib/amq/uri.rb
  3. +0 −91 spec/amq/settings_spec.rb
  4. +95 −0 spec/amq/uri_parsing_spec.rb
View
22 lib/amq/settings.rb
@@ -1,12 +1,10 @@
# encoding: utf-8
require "amq/protocol/client"
-require "uri"
+require "amq/uri"
module AMQ
module Settings
- # @private
- AMQP_PORTS = {"amqp" => 5672, "amqps" => 5671}.freeze
# @private
AMQPS = "amqps".freeze
@@ -110,23 +108,7 @@ def self.configure(settings = nil)
#
# @api public
def self.parse_amqp_url(connection_string)
- uri = URI.parse(connection_string)
- raise ArgumentError.new("Connection URI must use amqp or amqps schema (example: amqp://bus.megacorp.internal:5766), learn more at http://bit.ly/ks8MXK") unless %w{amqp amqps}.include?(uri.scheme)
-
- opts = {}
-
- opts[:scheme] = uri.scheme
- opts[:user] = URI.unescape(uri.user) if uri.user
- opts[:pass] = URI.unescape(uri.password) if uri.password
- opts[:host] = uri.host if uri.host
- opts[:port] = uri.port || AMQP_PORTS[uri.scheme]
- opts[:ssl] = uri.scheme.to_s.downcase =~ /amqps/i
- if uri.path =~ %r{^/(.*)}
- raise ArgumentError.new("#{uri} has multiple-segment path; please percent-encode any slashes in the vhost name (e.g. /production => %2Fproduction). Learn more at http://bit.ly/amqp-gem-and-connection-uris") if $1.index('/')
- opts[:vhost] = URI.unescape($1)
- end
-
- opts
+ AMQ::URI.parse(connection_string)
end
end
end
View
37 lib/amq/uri.rb
@@ -0,0 +1,37 @@
+# encoding: utf-8
+
+require "amq/settings"
+
+require "cgi"
+require "uri"
+
+module AMQ
+ class URI
+ # @private
+ AMQP_PORTS = {"amqp" => 5672, "amqps" => 5671}.freeze
+
+
+ def self.parse(connection_string)
+ uri = ::URI.parse(connection_string)
+ raise ArgumentError.new("Connection URI must use amqp or amqps schema (example: amqp://bus.megacorp.internal:5766), learn more at http://bit.ly/ks8MXK") unless %w{amqp amqps}.include?(uri.scheme)
+
+ opts = {}
+
+ opts[:scheme] = uri.scheme
+ opts[:user] = ::URI.unescape(uri.user) if uri.user
+ opts[:pass] = ::URI.unescape(uri.password) if uri.password
+ opts[:host] = uri.host if uri.host
+ opts[:port] = uri.port || AMQP_PORTS[uri.scheme]
+ opts[:ssl] = uri.scheme.to_s.downcase =~ /amqps/i
+ if uri.path =~ %r{^/(.*)}
+ raise ArgumentError.new("#{uri} has multiple-segment path; please percent-encode any slashes in the vhost name (e.g. /production => %2Fproduction). Learn more at http://bit.ly/amqp-gem-and-connection-uris") if $1.index('/')
+ opts[:vhost] = ::URI.unescape($1)
+ end
+
+ opts
+ end
+ def self.parse_amqp_url(s)
+ parse(s)
+ end
+ end
+end
View
91 spec/amq/settings_spec.rb
@@ -22,95 +22,4 @@
settings[:host].should eql("tagadab")
end
end
-
- describe ".parse_amqp_url(connection_string)" do
- context "when schema is not one of [amqp, amqps]" do
- it "raises ArgumentError" do
- expect {
- described_class.parse_amqp_url("http://dev.rabbitmq.com")
- }.to raise_error(ArgumentError, /amqp or amqps schema/)
- end
- end
-
-
- it "handles amqp:// URIs w/o path part" do
- val = described_class.parse_amqp_url("amqp://dev.rabbitmq.com")
-
- val[:vhost].should be_nil # in this case, default / will be used
- val[:host].should == "dev.rabbitmq.com"
- val[:port].should == 5672
- val[:scheme].should == "amqp"
- val[:ssl].should be_false
- end
-
- it "handles amqps:// URIs w/o path part" do
- val = described_class.parse_amqp_url("amqps://dev.rabbitmq.com")
-
- val[:vhost].should be_nil
- val[:host].should == "dev.rabbitmq.com"
- val[:port].should == 5671
- val[:scheme].should == "amqps"
- val[:ssl].should be_true
- end
-
-
- context "when URI ends in a slash" do
- it "parses vhost as an empty string" do
- val = described_class.parse_amqp_url("amqp://dev.rabbitmq.com/")
-
- val[:host].should == "dev.rabbitmq.com"
- val[:port].should == 5672
- val[:scheme].should == "amqp"
- val[:ssl].should be_false
- val[:vhost].should == ""
- end
- end
-
-
- context "when URI ends in /%2Fvault" do
- it "parses vhost as /vault" do
- val = described_class.parse_amqp_url("amqp://dev.rabbitmq.com/%2Fvault")
-
- val[:host].should == "dev.rabbitmq.com"
- val[:port].should == 5672
- val[:scheme].should == "amqp"
- val[:ssl].should be_false
- val[:vhost].should == "/vault"
- end
- end
-
-
- context "when URI is amqp://dev.rabbitmq.com/a.path.without.slashes" do
- it "parses vhost as a.path.without.slashes" do
- val = described_class.parse_amqp_url("amqp://dev.rabbitmq.com/a.path.without.slashes")
-
- val[:host].should == "dev.rabbitmq.com"
- val[:port].should == 5672
- val[:scheme].should == "amqp"
- val[:ssl].should be_false
- val[:vhost].should == "a.path.without.slashes"
- end
- end
-
- context "when URI is amqp://dev.rabbitmq.com/a/path/with/slashes" do
- it "raises an ArgumentError" do
- lambda { described_class.parse_amqp_url("amqp://dev.rabbitmq.com/a/path/with/slashes") }.should raise_error(ArgumentError)
- end
- end
-
-
- context "when URI has username:password, for instance, amqp://hedgehog:t0ps3kr3t@hub.megacorp.internal" do
- it "parses them out" do
- val = described_class.parse_amqp_url("amqp://hedgehog:t0ps3kr3t@hub.megacorp.internal")
-
- val[:host].should == "hub.megacorp.internal"
- val[:port].should == 5672
- val[:scheme].should == "amqp"
- val[:ssl].should be_false
- val[:user].should == "hedgehog"
- val[:pass].should == "t0ps3kr3t"
- val[:vhost].should be_nil # in this case, default / will be used
- end
- end
- end
end
View
95 spec/amq/uri_parsing_spec.rb
@@ -0,0 +1,95 @@
+# encoding: utf-8
+
+require "spec_helper"
+require "amq/uri"
+
+describe AMQ::URI, ".parse" do
+ context "when schema is not one of [amqp, amqps]" do
+ it "raises ArgumentError" do
+ expect {
+ described_class.parse_amqp_url("http://dev.rabbitmq.com")
+ }.to raise_error(ArgumentError, /amqp or amqps schema/)
+ end
+ end
+
+
+ it "handles amqp:// URIs w/o path part" do
+ val = described_class.parse_amqp_url("amqp://dev.rabbitmq.com")
+
+ val[:vhost].should be_nil # in this case, default / will be used
+ val[:host].should == "dev.rabbitmq.com"
+ val[:port].should == 5672
+ val[:scheme].should == "amqp"
+ val[:ssl].should be_false
+ end
+
+ it "handles amqps:// URIs w/o path part" do
+ val = described_class.parse_amqp_url("amqps://dev.rabbitmq.com")
+
+ val[:vhost].should be_nil
+ val[:host].should == "dev.rabbitmq.com"
+ val[:port].should == 5671
+ val[:scheme].should == "amqps"
+ val[:ssl].should be_true
+ end
+
+
+ context "when URI ends in a slash" do
+ it "parses vhost as an empty string" do
+ val = described_class.parse_amqp_url("amqp://dev.rabbitmq.com/")
+
+ val[:host].should == "dev.rabbitmq.com"
+ val[:port].should == 5672
+ val[:scheme].should == "amqp"
+ val[:ssl].should be_false
+ val[:vhost].should == ""
+ end
+ end
+
+
+ context "when URI ends in /%2Fvault" do
+ it "parses vhost as /vault" do
+ val = described_class.parse_amqp_url("amqp://dev.rabbitmq.com/%2Fvault")
+
+ val[:host].should == "dev.rabbitmq.com"
+ val[:port].should == 5672
+ val[:scheme].should == "amqp"
+ val[:ssl].should be_false
+ val[:vhost].should == "/vault"
+ end
+ end
+
+
+ context "when URI is amqp://dev.rabbitmq.com/a.path.without.slashes" do
+ it "parses vhost as a.path.without.slashes" do
+ val = described_class.parse_amqp_url("amqp://dev.rabbitmq.com/a.path.without.slashes")
+
+ val[:host].should == "dev.rabbitmq.com"
+ val[:port].should == 5672
+ val[:scheme].should == "amqp"
+ val[:ssl].should be_false
+ val[:vhost].should == "a.path.without.slashes"
+ end
+ end
+
+ context "when URI is amqp://dev.rabbitmq.com/a/path/with/slashes" do
+ it "raises an ArgumentError" do
+ lambda { described_class.parse_amqp_url("amqp://dev.rabbitmq.com/a/path/with/slashes") }.should raise_error(ArgumentError)
+ end
+ end
+
+
+ context "when URI has username:password, for instance, amqp://hedgehog:t0ps3kr3t@hub.megacorp.internal" do
+ it "parses them out" do
+ val = described_class.parse_amqp_url("amqp://hedgehog:t0ps3kr3t@hub.megacorp.internal")
+
+ val[:host].should == "hub.megacorp.internal"
+ val[:port].should == 5672
+ val[:scheme].should == "amqp"
+ val[:ssl].should be_false
+ val[:user].should == "hedgehog"
+ val[:pass].should == "t0ps3kr3t"
+ val[:vhost].should be_nil # in this case, default / will be used
+ end
+ end
+end

0 comments on commit 43a3c3c

Please sign in to comment.
Something went wrong with that request. Please try again.