Fix handling of username when using "redis://".

The username is now correctly retrieved from the userinfo fragment of
the URI when using the "redis" scheme and a "username:password" pair is
present. Values retrieved from the userinfo fragment always override the
ones specified in `username` and `password` if those fields are present
in the query string.
This commit is contained in:
Daniele Alessandri
2020-09-01 11:35:35 +02:00
parent f225c55a01
commit 5eccc65d42
3 changed files with 53 additions and 18 deletions
+9
View File
@@ -1,3 +1,12 @@
v1.1.5 (2020-09-xx)
================================================================================
- __FIX__: the username is now correctly retrieved from the userinfo fragment of
the URI when using the "redis" scheme and a "username:password" pair is present.
Values retrieved from the userinfo fragment always override the ones specified
in `username` and `password` if those fields are present in the query string.
v1.1.4 (2020-08-31)
================================================================================
+10 -1
View File
@@ -109,8 +109,17 @@ class Parameters implements ParametersInterface
}
if (stripos($uri, 'redis') === 0) {
if (isset($parsed['user'])) {
if (strlen($parsed['user'])) {
$parsed['username'] = $parsed['user'];
}
unset($parsed['user']);
}
if (isset($parsed['pass'])) {
$parsed['password'] = $parsed['pass'];
if (strlen($parsed['pass'])) {
$parsed['password'] = $parsed['pass'];
}
unset($parsed['pass']);
}
+34 -17
View File
@@ -156,7 +156,7 @@ class ParametersTest extends PredisTestCase
*/
public function testParsingURIWithRedisScheme()
{
$uri = 'redis://:secret@10.10.10.10:6400/5?timeout=0.5&persistent=1';
$uri = 'redis://predis:secret@10.10.10.10:6400/5?timeout=0.5&persistent=1';
$expected = array(
'scheme' => 'redis',
@@ -164,20 +164,48 @@ class ParametersTest extends PredisTestCase
'port' => 6400,
'timeout' => '0.5',
'persistent' => '1',
'username' => 'predis',
'password' => 'secret',
'database' => '5',
);
$parameters = Parameters::parse($uri);
// TODO: parse_url() in PHP >= 5.6 returns an empty "user" entry in the
// dictionary when no username has been provided in the URI string. This
// actually makes sense, but let's keep the test ugly & simple for now.
unset($parameters['user']);
$this->assertSame($expected, $parameters);
}
/**
* @group disconnected
*/
public function testRedisSchemeOverridesUsernameAndPasswordInQueryString()
{
$parameters = Parameters::parse('redis://predis:secret@10.10.10.10/5?username=ignored&password=ignored');
$this->assertSame('predis', $parameters['username']);
$this->assertSame('secret', $parameters['password']);
}
/**
* @group disconnected
*/
public function testRedisSchemeDoesNotOverridesUsernameAndPasswordInQueryStringOnEmptyAuthFragment()
{
$parameters = Parameters::parse('redis://:@10.10.10.10/5?username=predis&password=secret');
$this->assertSame('predis', $parameters['username']);
$this->assertSame('secret', $parameters['password']);
}
/**
* @group disconnected
*/
public function testRedisSchemeOverridesDatabaseInQueryString()
{
$parameters = Parameters::parse('redis://10.10.10.10/5?database=10');
$this->assertSame('5', $parameters['database']);
}
/**
* @group disconnected
*/
@@ -197,17 +225,6 @@ class ParametersTest extends PredisTestCase
$this->assertSame($expected, $parameters);
}
/**
* @group disconnected
*/
public function testRedisSchemeOverridesPasswordAndDatabaseInQueryString()
{
$parameters = Parameters::parse('redis://:secret@10.10.10.10/5?password=ignored&database=4');
$this->assertSame('secret', $parameters['password']);
$this->assertSame('5', $parameters['database']);
}
/**
* @group disconnected
*/