diff --git a/src/Factory.php b/src/Factory.php index af4c199..e6d595e 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -98,7 +98,8 @@ private function parseUrl($target) } $parts = parse_url($target); - if ($parts === false || !isset($parts['host']) || $parts['scheme'] !== 'tcp') { + $validSchemes = array('redis', 'tcp'); + if ($parts === false || !isset($parts['host']) || !in_array($parts['scheme'], $validSchemes)) { throw new InvalidArgumentException('Given URL can not be parsed'); } @@ -110,15 +111,12 @@ private function parseUrl($target) $parts['host'] = '127.0.0.1'; } - $auth = null; - if (isset($parts['user'])) { - $auth = $parts['user']; - } if (isset($parts['pass'])) { - $auth .= ':' . $parts['pass']; - } - if ($auth !== null) { - $parts['auth'] = $auth; + // username:password@ (Redis doesn't support usernames) + $parts['auth'] = $parts['pass']; + } elseif (isset($parts['user'])) { + // password@ + $parts['auth'] = $parts['user']; } if (isset($parts['path']) && $parts['path'] !== '') { diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index a1c029c..a24cdd5 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -64,11 +64,20 @@ public function testWillWriteSelectCommandIfTargetContainsPath() public function testWillWriteAuthCommandIfTargetContainsUserInfo() { $stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock(); - $stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$11\r\nhello:world\r\n"); + $stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n"); $this->connector->expects($this->once())->method('create')->willReturn(Promise\resolve($stream)); $this->factory->createClient('tcp://hello:world@127.0.0.1'); } + + public function testWillWriteAuthCommandIfTargetContainsPasswordAsUser() + { + $stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock(); + $stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n"); + + $this->connector->expects($this->once())->method('create')->willReturn(Promise\resolve($stream)); + $this->factory->createClient('tcp://world@127.0.0.1'); + } public function testWillRejectIfConnectorRejects() {