Skip to content

Commit

Permalink
[Bugfix] Throw invalid uris when no scheme is parsed
Browse files Browse the repository at this point in the history
A valid uri must have a scheme. We do not treat uris without a scheme as valid.

Closes #3
  • Loading branch information
mnavarrocarter committed Jul 8, 2021
1 parent 18926f2 commit 1838b32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public static function parse(string $uri): Uri

$scheme = Str\toLower($parts['scheme'] ?? '');

if ('' === $scheme) {
throw new InvalidUri('Uri must have a scheme');
}

return new self(
$scheme,
$parts['user'] ?? '',
Expand Down
22 changes: 20 additions & 2 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class UriTest extends TestCase
{
/**
* @dataProvider getUris
* @dataProvider getValidUris
*
* @throws InvalidUri
*/
Expand All @@ -36,7 +36,18 @@ public function testItParsesUris(string $uri, string $parsed): void
self::assertSame($parsed, Uri::parse($uri)->toStr());
}

public function getUris(): array
/**
* @dataProvider getInvalidUris
*
* @throws InvalidUri
*/
public function testInvalidUris(string $uri): void
{
$this->expectException(InvalidUri::class);
Uri::parse($uri);
}

public function getValidUris(): array
{
return [
['https://example.com', 'https://example.com'],
Expand All @@ -46,4 +57,11 @@ public function getUris(): array
['mailto:John.Doe@example.com', 'mailto:John.Doe@example.com'],
];
}

public function getInvalidUris(): array
{
return [
['://example.com/protocol-relative-url'],
];
}
}

0 comments on commit 1838b32

Please sign in to comment.