Skip to content

Commit

Permalink
[dsn] allow specify special chars in user and password.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Nov 23, 2018
1 parent 4b2f800 commit ba711b0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ public static function parse(string $dsn): array
$schemeExtensions = array_values($schemeParts);

$user = parse_url($dsn, PHP_URL_USER) ?: null;
if (is_string($user)) {
$user = rawurldecode($user);
}

$password = parse_url($dsn, PHP_URL_PASS) ?: null;
if (is_string($password)) {
$password = rawurldecode($password);
}

$path = parse_url($dsn, PHP_URL_PATH) ?: null;
if ($path) {
Expand Down
30 changes: 30 additions & 0 deletions Tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,36 @@ public function testShouldParseExpectedNumberOfMultipleDsns()
$this->assertCount(3, $dsns);
}

public function testShouldParseDsnWithOnlyUser()
{
$dsn = Dsn::parseFirst('foo://user@host');

$this->assertSame('user', $dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertSame('foo', $dsn->getScheme());
$this->assertSame('host', $dsn->getHost());
}

public function testShouldUrlEncodeUser()
{
$dsn = Dsn::parseFirst('foo://us%3Aer@host');

$this->assertSame('us:er', $dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertSame('foo', $dsn->getScheme());
$this->assertSame('host', $dsn->getHost());
}

public function testShouldUrlEncodePassword()
{
$dsn = Dsn::parseFirst('foo://user:pass%3Aword@host');

$this->assertSame('user', $dsn->getUser());
$this->assertSame('pass:word', $dsn->getPassword());
$this->assertSame('foo', $dsn->getScheme());
$this->assertSame('host', $dsn->getHost());
}

public static function provideSchemes()
{
yield [':', '', '', []];
Expand Down

0 comments on commit ba711b0

Please sign in to comment.