Browse Source

pass redis password in the URL

pull/120/head
Miguel Grinberg 8 years ago
parent
commit
af81132610
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 18
      socketio/asyncio_redis_manager.py
  2. 23
      tests/test_asyncio_redis_manager.py

18
socketio/asyncio_redis_manager.py

@ -13,19 +13,14 @@ def _parse_redis_url(url):
p = urlparse(url) p = urlparse(url)
if p.scheme != 'redis': if p.scheme != 'redis':
raise ValueError('Invalid redis url') raise ValueError('Invalid redis url')
if ':' in p.netloc: host = p.hostname or 'localhost'
host, port = p.netloc.split(':') port = p.port or 6379
port = int(port) password = p.password
else:
host = p.netloc or 'localhost'
port = 6379
if p.path: if p.path:
db = int(p.path[1:]) db = int(p.path[1:])
else: else:
db = 0 db = 0
if not host: return host, port, password, db
raise ValueError('Invalid redis hostname')
return host, port, db
class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
@ -53,15 +48,14 @@ class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
name = 'aioredis' name = 'aioredis'
def __init__(self, url='redis://localhost:6379/0', channel='socketio', def __init__(self, url='redis://localhost:6379/0', channel='socketio',
write_only=False, password=None): write_only=False):
if aioredis is None: if aioredis is None:
raise RuntimeError('Redis package is not installed ' raise RuntimeError('Redis package is not installed '
'(Run "pip install aioredis" in your ' '(Run "pip install aioredis" in your '
'virtualenv).') 'virtualenv).')
self.host, self.port, self.db = _parse_redis_url(url) self.host, self.port, self.password, self.db = _parse_redis_url(url)
self.pub = None self.pub = None
self.sub = None self.sub = None
self.password = password
super().__init__(channel=channel, write_only=write_only) super().__init__(channel=channel, write_only=write_only)
async def _publish(self, data): async def _publish(self, data):

23
tests/test_asyncio_redis_manager.py

@ -9,26 +9,37 @@ if sys.version_info >= (3, 5):
class TestAsyncRedisManager(unittest.TestCase): class TestAsyncRedisManager(unittest.TestCase):
def test_default_url(self): def test_default_url(self):
self.assertEqual(asyncio_redis_manager._parse_redis_url('redis://'), self.assertEqual(asyncio_redis_manager._parse_redis_url('redis://'),
('localhost', 6379, 0)) ('localhost', 6379, None, 0))
def test_only_host_url(self): def test_only_host_url(self):
self.assertEqual( self.assertEqual(
asyncio_redis_manager._parse_redis_url('redis://redis.host'), asyncio_redis_manager._parse_redis_url('redis://redis.host'),
('redis.host', 6379, 0)) ('redis.host', 6379, None, 0))
def test_no_db_url(self): def test_no_db_url(self):
self.assertEqual( self.assertEqual(
asyncio_redis_manager._parse_redis_url('redis://redis.host:123/1'), asyncio_redis_manager._parse_redis_url('redis://redis.host:123/1'),
('redis.host', 123, 1)) ('redis.host', 123, None, 1))
def test_no_port_url(self): def test_no_port_url(self):
self.assertEqual( self.assertEqual(
asyncio_redis_manager._parse_redis_url('redis://redis.host/1'), asyncio_redis_manager._parse_redis_url('redis://redis.host/1'),
('redis.host', 6379, 1)) ('redis.host', 6379, None, 1))
def test_password(self):
self.assertEqual(
asyncio_redis_manager._parse_redis_url('redis://:[email protected]/1'),
('redis.host', 6379, 'pw', 1))
def test_no_host_url(self): def test_no_host_url(self):
self.assertRaises(ValueError, asyncio_redis_manager._parse_redis_url, self.assertEqual(
'redis://:123/1') asyncio_redis_manager._parse_redis_url('redis://:123/1'),
('localhost', 123, None, 1))
def test_no_host_password_url(self):
self.assertEqual(
asyncio_redis_manager._parse_redis_url('redis://:pw@:123/1'),
('localhost', 123, 'pw', 1))
def test_bad_port_url(self): def test_bad_port_url(self):
self.assertRaises(ValueError, asyncio_redis_manager._parse_redis_url, self.assertRaises(ValueError, asyncio_redis_manager._parse_redis_url,

Loading…
Cancel
Save