From 19c1b29f5955cadfc7614253a33f0a536e3c6f3e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 16 Nov 2019 10:19:25 +0100 Subject: [PATCH] Added tests for UrlValidator --- module/Core/src/Util/UrlValidator.php | 4 +- module/Core/test/Service/UrlShortenerTest.php | 22 +++---- module/Core/test/Util/UrlValidatorTest.php | 66 +++++++++++++++++++ 3 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 module/Core/test/Util/UrlValidatorTest.php diff --git a/module/Core/src/Util/UrlValidator.php b/module/Core/src/Util/UrlValidator.php index b518a2ea..5a30d9d3 100644 --- a/module/Core/src/Util/UrlValidator.php +++ b/module/Core/src/Util/UrlValidator.php @@ -16,7 +16,7 @@ use function idn_to_ascii; use const IDNA_DEFAULT; use const INTL_IDNA_VARIANT_UTS46; -class UrlValidator implements UrlValidatorInterface +class UrlValidator implements UrlValidatorInterface, RequestMethodInterface { private const MAX_REDIRECTS = 15; @@ -43,7 +43,7 @@ class UrlValidator implements UrlValidatorInterface } try { - $this->httpClient->request(RequestMethodInterface::METHOD_GET, (string) $uri, [ + $this->httpClient->request(self::METHOD_GET, (string) $uri, [ RequestOptions::ALLOW_REDIRECTS => ['max' => self::MAX_REDIRECTS], ]); } catch (GuzzleException $e) { diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 78e61a6a..1995c921 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -9,21 +9,18 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMException; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\ClientException; -use GuzzleHttp\Psr7\Request; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Tag; -use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface; use Shlinkio\Shlink\Core\Service\UrlShortener; +use Shlinkio\Shlink\Core\Util\UrlValidatorInterface; use Zend\Diactoros\Uri; use function array_map; @@ -35,11 +32,11 @@ class UrlShortenerTest extends TestCase /** @var ObjectProphecy */ private $em; /** @var ObjectProphecy */ - private $httpClient; + private $urlValidator; public function setUp(): void { - $this->httpClient = $this->prophesize(ClientInterface::class); + $this->urlValidator = $this->prophesize(UrlValidatorInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $conn = $this->prophesize(Connection::class); @@ -63,7 +60,7 @@ class UrlShortenerTest extends TestCase private function setUrlShortener(bool $urlValidationEnabled): void { $this->urlShortener = new UrlShortener( - $this->httpClient->reveal(), + $this->urlValidator->reveal(), $this->em->reveal(), new UrlShortenerOptions(['validate_url' => $urlValidationEnabled]) ); @@ -127,20 +124,19 @@ class UrlShortenerTest extends TestCase } /** @test */ - public function exceptionIsThrownWhenUrlDoesNotExist(): void + public function validatorIsCalledWhenUrlValidationIsEnabled(): void { $this->setUrlShortener(true); + $validateUrl = $this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar')->will(function () { + }); - $this->httpClient->request(Argument::cetera())->willThrow( - new ClientException('', $this->prophesize(Request::class)->reveal()) - ); - - $this->expectException(InvalidUrlException::class); $this->urlShortener->urlToShortCode( new Uri('http://foobar.com/12345/hello?foo=bar'), [], ShortUrlMeta::createEmpty() ); + + $validateUrl->shouldHaveBeenCalledOnce(); } /** @test */ diff --git a/module/Core/test/Util/UrlValidatorTest.php b/module/Core/test/Util/UrlValidatorTest.php new file mode 100644 index 00000000..331ee52f --- /dev/null +++ b/module/Core/test/Util/UrlValidatorTest.php @@ -0,0 +1,66 @@ +httpClient = $this->prophesize(ClientInterface::class); + $this->urlValidator = new UrlValidator($this->httpClient->reveal()); + } + + /** @test */ + public function exceptionIsThrownWhenUrlIsInvalid(): void + { + $request = $this->httpClient->request(Argument::cetera())->willThrow( + new ClientException('', $this->prophesize(Request::class)->reveal()) + ); + + $this->expectException(InvalidUrlException::class); + $request->shouldBeCalledOnce(); + + $this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar'); + } + + /** + * @test + * @dataProvider provideUrls + */ + public function expectedUrlIsCalledInOrderToVerifyProvidedUrl(string $providedUrl, string $expectedUrl): void + { + $request = $this->httpClient->request( + RequestMethodInterface::METHOD_GET, + $expectedUrl, + Argument::cetera() + )->will(function () { + }); + + $this->urlValidator->validateUrl($providedUrl); + + $request->shouldHaveBeenCalledOnce(); + } + + public function provideUrls(): iterable + { + yield 'regular domain' => ['http://foobar.com', 'http://foobar.com']; + yield 'IDN' => ['https://cédric.laubacher.io/', 'https://xn--cdric-bsa.laubacher.io/']; + } +}