Skip to content

Commit c54c61c

Browse files
committed
Improved url parser.
1 parent 3f5b9af commit c54c61c

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Denpa/Bitcoin/Client.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,12 @@ protected function defaultConfig(array $config = [])
256256
protected function parseUrl($config)
257257
{
258258
if (is_string($config)) {
259-
$parts = parse_url($config);
260-
if (!$parts) {
259+
$allowed = ['scheme', 'host', 'port', 'user', 'pass'];
260+
261+
$parts = (array) parse_url($config);
262+
$parts = array_intersect_key($parts, array_flip($allowed));
263+
264+
if (!$parts || empty($parts)) {
261265
throw new ClientException('Invalid url');
262266
}
263267

tests/Denpa/Bitcoin/ClientTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function setUp()
5454
}
5555

5656
/**
57-
* Test url expander.
57+
* Test url parser.
5858
*
5959
* @param string $url
6060
* @param string $scheme
@@ -67,7 +67,7 @@ public function setUp()
6767
*
6868
* @dataProvider urlProvider
6969
*/
70-
public function testUrlExpander($url, $scheme, $host, $port, $user, $pass)
70+
public function testUrlParser($url, $scheme, $host, $port, $user, $pass)
7171
{
7272
$bitcoind = new Client($url);
7373

@@ -101,6 +101,22 @@ public function urlProvider()
101101
];
102102
}
103103

104+
/**
105+
* Test url parser with invalid url.
106+
*
107+
* @return array
108+
*/
109+
public function testUrlParserWithInvalidUrl()
110+
{
111+
try {
112+
$bitcoind = new Client('cookies!');
113+
114+
$this->expectException(ClientException::class);
115+
} catch (ClientException $e) {
116+
$this->assertEquals('Invalid url', $e->getMessage());
117+
}
118+
}
119+
104120
/**
105121
* Test client getter and setter.
106122
*

0 commit comments

Comments
 (0)