Permalink
Browse files

Add ipv6 address support

  • Loading branch information...
1 parent 595a1f4 commit d4f05f982bb6b3fdf62ba22b10d26e9a8dc7705d @dplummer dplummer committed Jan 16, 2014
Showing with 43 additions and 1 deletion.
  1. +6 −1 lib/dalli/server.rb
  2. +37 −0 test/test_server.rb
View
@@ -32,7 +32,7 @@ class Server
}
def initialize(attribs, options = {})
- (@hostname, @port, @weight) = attribs.split(':')
+ (@hostname, @port, @weight) = parse_hostname(attribs)
@port ||= 11211
@port = Integer(@port)
@weight ||= 1
@@ -688,5 +688,10 @@ def sasl_authentication
# (step, msg) = sasl.receive('challenge', content)
# raise Dalli::NetworkError, "Authentication failed" if sasl.failed? || step != 'response'
end
+
+ def parse_hostname(str)
+ res = str.match(/\A(\[([\h:]+)\]|[^:]+)(:(\d+))?(:(\d+))?\z/)
+ return res[2] || res[1], res[4], res[6]
+ end
end
end
View
@@ -27,6 +27,43 @@
it 'handles ipv4 addresses' do
s = Dalli::Server.new('127.0.0.1')
assert_equal '127.0.0.1', s.hostname
+ assert_equal 11211, s.port
+ assert_equal 1, s.weight
+ end
+
+ it 'handles ipv6 addresses' do
+ s = Dalli::Server.new('[::1]')
+ assert_equal '::1', s.hostname
+ assert_equal 11211, s.port
+ assert_equal 1, s.weight
+ end
+
+ it 'handles ipv6 addresses with port' do
+ s = Dalli::Server.new('[::1]:11212')
+ assert_equal '::1', s.hostname
+ assert_equal 11212, s.port
+ assert_equal 1, s.weight
+ end
+
+ it 'handles ipv6 addresses with port and weight' do
+ s = Dalli::Server.new('[::1]:11212:2')
+ assert_equal '::1', s.hostname
+ assert_equal 11212, s.port
+ assert_equal 2, s.weight
+ end
+
+ it 'handles a FQDN' do
+ s = Dalli::Server.new('my.fqdn.com')
+ assert_equal 'my.fqdn.com', s.hostname
+ assert_equal 11211, s.port
+ assert_equal 1, s.weight
+ end
+
+ it 'handles a FQDN with port and weight' do
+ s = Dalli::Server.new('my.fqdn.com:11212:2')
+ assert_equal 'my.fqdn.com', s.hostname
+ assert_equal 11212, s.port
+ assert_equal 2, s.weight
end
end
end

0 comments on commit d4f05f9

Please sign in to comment.