Skip to content

Commit

Permalink
Improved url parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
denpamusic committed Aug 12, 2017
1 parent 3f5b9af commit c54c61c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Denpa/Bitcoin/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,12 @@ protected function defaultConfig(array $config = [])
protected function parseUrl($config)
{
if (is_string($config)) {
$parts = parse_url($config);
if (!$parts) {
$allowed = ['scheme', 'host', 'port', 'user', 'pass'];

$parts = (array) parse_url($config);
$parts = array_intersect_key($parts, array_flip($allowed));

if (!$parts || empty($parts)) {
throw new ClientException('Invalid url');
}

Expand Down
20 changes: 18 additions & 2 deletions tests/Denpa/Bitcoin/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function setUp()
}

/**
* Test url expander.
* Test url parser.
*
* @param string $url
* @param string $scheme
Expand All @@ -67,7 +67,7 @@ public function setUp()
*
* @dataProvider urlProvider
*/
public function testUrlExpander($url, $scheme, $host, $port, $user, $pass)
public function testUrlParser($url, $scheme, $host, $port, $user, $pass)
{
$bitcoind = new Client($url);

Expand Down Expand Up @@ -101,6 +101,22 @@ public function urlProvider()
];
}

/**
* Test url parser with invalid url.
*
* @return array
*/
public function testUrlParserWithInvalidUrl()
{
try {
$bitcoind = new Client('cookies!');

$this->expectException(ClientException::class);
} catch (ClientException $e) {
$this->assertEquals('Invalid url', $e->getMessage());
}
}

/**
* Test client getter and setter.
*
Expand Down

0 comments on commit c54c61c

Please sign in to comment.