Skip to content

Commit db3a927

Browse files
committed
Add tests for Redis AUTH command with username and password support
1 parent 16e17f6 commit db3a927

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/Io/FactoryStreamingClientTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,4 +708,55 @@ public function testCreateClientWillCancelTimerWhenConnectionRejects(): void
708708

709709
$deferred->reject(new \RuntimeException());
710710
}
711+
712+
public function testWillWriteAuthCommandIfTargetContainsUsernameAndPasswordQueryParameter(): void
713+
{
714+
$stream = $this->createMock(ConnectionInterface::class);
715+
$stream->expects($this->once())->method('write')->with("*3\r\n$4\r\nauth\r\n$5\r\nhello\r\n$5\r\nworld\r\n");
716+
717+
$loop = $this->createMock(LoopInterface::class);
718+
$loop->expects($this->once())->method('addTimer');
719+
assert($loop instanceof LoopInterface);
720+
Loop::set($loop);
721+
722+
$this->connector
723+
->expects($this->once())
724+
->method('connect')
725+
->with('example.com:6379')
726+
->willReturn(resolve($stream));
727+
728+
$this->factory->createClient(
729+
'redis://example.com?username=hello&password=world'
730+
);
731+
}
732+
733+
public function testWillResolveWhenAuthCommandReceivesOkResponseWithUsernameAndPassword(): void
734+
{
735+
$dataHandler = null;
736+
$stream = $this->createMock(ConnectionInterface::class);
737+
$stream->expects($this->once())->method('write')->with("*3\r\n$4\r\nauth\r\n$4\r\nuser\r\n$4\r\npass\r\n");
738+
$stream->expects($this->exactly(2))->method('on')->withConsecutive(
739+
['data', $this->callback(function ($cb) use (&$dataHandler) {
740+
$dataHandler = $cb;
741+
return true;
742+
})],
743+
['close', $this->anything()]
744+
);
745+
746+
$this->connector
747+
->expects($this->once())
748+
->method('connect')
749+
->willReturn(resolve($stream));
750+
751+
$promise = $this->factory->createClient(
752+
'redis://user:[email protected]'
753+
);
754+
755+
$this->assertTrue(is_callable($dataHandler));
756+
$dataHandler("+OK\r\n");
757+
758+
$promise->then($this->expectCallableOnceWith(
759+
$this->isInstanceOf(StreamingClient::class)
760+
));
761+
}
711762
}

0 commit comments

Comments
 (0)