mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -06:00
Added tests for UrlValidator
This commit is contained in:
parent
264b8c2a9e
commit
19c1b29f59
@ -16,7 +16,7 @@ use function idn_to_ascii;
|
|||||||
use const IDNA_DEFAULT;
|
use const IDNA_DEFAULT;
|
||||||
use const INTL_IDNA_VARIANT_UTS46;
|
use const INTL_IDNA_VARIANT_UTS46;
|
||||||
|
|
||||||
class UrlValidator implements UrlValidatorInterface
|
class UrlValidator implements UrlValidatorInterface, RequestMethodInterface
|
||||||
{
|
{
|
||||||
private const MAX_REDIRECTS = 15;
|
private const MAX_REDIRECTS = 15;
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ class UrlValidator implements UrlValidatorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->httpClient->request(RequestMethodInterface::METHOD_GET, (string) $uri, [
|
$this->httpClient->request(self::METHOD_GET, (string) $uri, [
|
||||||
RequestOptions::ALLOW_REDIRECTS => ['max' => self::MAX_REDIRECTS],
|
RequestOptions::ALLOW_REDIRECTS => ['max' => self::MAX_REDIRECTS],
|
||||||
]);
|
]);
|
||||||
} catch (GuzzleException $e) {
|
} catch (GuzzleException $e) {
|
||||||
|
@ -9,21 +9,18 @@ use Doctrine\Common\Collections\ArrayCollection;
|
|||||||
use Doctrine\DBAL\Connection;
|
use Doctrine\DBAL\Connection;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\ORMException;
|
use Doctrine\ORM\ORMException;
|
||||||
use GuzzleHttp\ClientInterface;
|
|
||||||
use GuzzleHttp\Exception\ClientException;
|
|
||||||
use GuzzleHttp\Psr7\Request;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
|
||||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||||
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
||||||
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
||||||
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
||||||
|
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
|
||||||
use Zend\Diactoros\Uri;
|
use Zend\Diactoros\Uri;
|
||||||
|
|
||||||
use function array_map;
|
use function array_map;
|
||||||
@ -35,11 +32,11 @@ class UrlShortenerTest extends TestCase
|
|||||||
/** @var ObjectProphecy */
|
/** @var ObjectProphecy */
|
||||||
private $em;
|
private $em;
|
||||||
/** @var ObjectProphecy */
|
/** @var ObjectProphecy */
|
||||||
private $httpClient;
|
private $urlValidator;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->httpClient = $this->prophesize(ClientInterface::class);
|
$this->urlValidator = $this->prophesize(UrlValidatorInterface::class);
|
||||||
|
|
||||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
||||||
$conn = $this->prophesize(Connection::class);
|
$conn = $this->prophesize(Connection::class);
|
||||||
@ -63,7 +60,7 @@ class UrlShortenerTest extends TestCase
|
|||||||
private function setUrlShortener(bool $urlValidationEnabled): void
|
private function setUrlShortener(bool $urlValidationEnabled): void
|
||||||
{
|
{
|
||||||
$this->urlShortener = new UrlShortener(
|
$this->urlShortener = new UrlShortener(
|
||||||
$this->httpClient->reveal(),
|
$this->urlValidator->reveal(),
|
||||||
$this->em->reveal(),
|
$this->em->reveal(),
|
||||||
new UrlShortenerOptions(['validate_url' => $urlValidationEnabled])
|
new UrlShortenerOptions(['validate_url' => $urlValidationEnabled])
|
||||||
);
|
);
|
||||||
@ -127,20 +124,19 @@ class UrlShortenerTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function exceptionIsThrownWhenUrlDoesNotExist(): void
|
public function validatorIsCalledWhenUrlValidationIsEnabled(): void
|
||||||
{
|
{
|
||||||
$this->setUrlShortener(true);
|
$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(
|
$this->urlShortener->urlToShortCode(
|
||||||
new Uri('http://foobar.com/12345/hello?foo=bar'),
|
new Uri('http://foobar.com/12345/hello?foo=bar'),
|
||||||
[],
|
[],
|
||||||
ShortUrlMeta::createEmpty()
|
ShortUrlMeta::createEmpty()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$validateUrl->shouldHaveBeenCalledOnce();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
66
module/Core/test/Util/UrlValidatorTest.php
Normal file
66
module/Core/test/Util/UrlValidatorTest.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkioTest\Shlink\Core\Util;
|
||||||
|
|
||||||
|
use Fig\Http\Message\RequestMethodInterface;
|
||||||
|
use GuzzleHttp\ClientInterface;
|
||||||
|
use GuzzleHttp\Exception\ClientException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Prophecy\Argument;
|
||||||
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
|
use Shlinkio\Shlink\Core\Util\UrlValidator;
|
||||||
|
use Zend\Diactoros\Request;
|
||||||
|
|
||||||
|
class UrlValidatorTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @var UrlValidator */
|
||||||
|
private $urlValidator;
|
||||||
|
/** @var ObjectProphecy */
|
||||||
|
private $httpClient;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->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/'];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user