1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
# frozen_string_literal: true
require_relative '../helper'
describe Dalli::Protocol::ServerConfigParser do
describe 'parse' do
let(:port) { rand(9999..99_999) }
let(:weight) { rand(1..5) }
describe 'when the string is not an memcached URI' do
describe 'tcp' do
describe 'when the hostname is a domain name' do
let(:hostname) { "a#{SecureRandom.hex(5)}.b#{SecureRandom.hex(3)}.#{%w[net com edu].sample}" }
it 'parses a hostname by itself' do
assert_equal Dalli::Protocol::ServerConfigParser.parse(hostname), [hostname, 11_211, :tcp, 1, {}]
end
it 'parses hostname with a port' do
assert_equal Dalli::Protocol::ServerConfigParser.parse("#{hostname}:#{port}"),
[hostname, port, :tcp, 1, {}]
end
it 'parses hostname with a port and weight' do
assert_equal Dalli::Protocol::ServerConfigParser.parse("#{hostname}:#{port}:#{weight}"),
[hostname, port, :tcp, weight, {}]
end
end
describe 'when the hostname is an IPv4 address' do
let(:hostname) { '203.0.113.28' }
it 'parses a hostname by itself' do
assert_equal Dalli::Protocol::ServerConfigParser.parse(hostname), [hostname, 11_211, :tcp, 1, {}]
end
it 'parses hostname with a port' do
assert_equal Dalli::Protocol::ServerConfigParser.parse("#{hostname}:#{port}"),
[hostname, port, :tcp, 1, {}]
end
it 'parses hostname with a port and weight' do
assert_equal Dalli::Protocol::ServerConfigParser.parse("#{hostname}:#{port}:#{weight}"),
[hostname, port, :tcp, weight, {}]
end
end
describe 'when the hostname is an IPv6 address' do
let(:hostname) { ['2001:db8:ffff:ffff:ffff:ffff:ffff:ffff', '2001:db8::'].sample }
it 'parses a hostname by itself' do
assert_equal Dalli::Protocol::ServerConfigParser.parse("[#{hostname}]"),
[hostname, 11_211, :tcp, 1, {}]
end
it 'parses hostname with a port' do
assert_equal Dalli::Protocol::ServerConfigParser.parse("[#{hostname}]:#{port}"),
[hostname, port, :tcp, 1, {}]
end
it 'parses hostname with a port and weight' do
assert_equal Dalli::Protocol::ServerConfigParser.parse("[#{hostname}]:#{port}:#{weight}"),
[hostname, port, :tcp, weight, {}]
end
end
end
describe 'unix' do
let(:hostname) { "/tmp/#{SecureRandom.hex(5)}" }
it 'parses a socket by itself' do
assert_equal Dalli::Protocol::ServerConfigParser.parse(hostname), [hostname, nil, :unix, 1, {}]
end
it 'parses socket with a weight' do
assert_equal Dalli::Protocol::ServerConfigParser.parse("#{hostname}:#{weight}"),
[hostname, nil, :unix, weight, {}]
end
it 'produces an error with a port and weight' do
err = assert_raises Dalli::DalliError do
Dalli::Protocol::ServerConfigParser.parse("#{hostname}:#{port}:#{weight}")
end
assert_equal err.message, "Could not parse hostname #{hostname}:#{port}:#{weight}"
end
end
end
describe 'when the string is a memcached URI' do
let(:user) { SecureRandom.hex(5) }
let(:password) { SecureRandom.hex(5) }
let(:port) { rand(15_000..16_023) }
let(:hostname) { "a#{SecureRandom.hex(3)}.b#{SecureRandom.hex(3)}.com" }
describe 'when the URI is properly formed and includes all values' do
let(:uri) { "memcached://#{user}:#{password}@#{hostname}:#{port}" }
it 'parses correctly' do
assert_equal Dalli::Protocol::ServerConfigParser.parse(uri),
[hostname, port, :tcp, 1, { username: user, password: password }]
end
end
describe 'when the URI does not include a port' do
let(:uri) { "memcached://#{user}:#{password}@#{hostname}" }
it 'parses correctly' do
assert_equal Dalli::Protocol::ServerConfigParser.parse(uri),
[hostname, 11_211, :tcp, 1, { username: user, password: password }]
end
end
end
describe 'errors' do
describe 'when the string is empty' do
it 'produces an error' do
err = assert_raises Dalli::DalliError do
Dalli::Protocol::ServerConfigParser.parse('')
end
assert_equal('Could not parse hostname ', err.message)
end
end
describe 'when the string starts with a colon' do
it 'produces an error' do
err = assert_raises Dalli::DalliError do
Dalli::Protocol::ServerConfigParser.parse(':1:2')
end
assert_equal('Could not parse hostname :1:2', err.message)
end
end
describe 'when the string ends with a colon' do
it 'produces an error' do
err = assert_raises Dalli::DalliError do
Dalli::Protocol::ServerConfigParser.parse('abc.com:')
end
assert_equal('Could not parse hostname abc.com:', err.message)
end
end
end
end
end
|