shlink/module/Core/test/Util/UrlValidatorTest.php

70 lines
2.1 KiB
PHP
Raw Normal View History

2019-11-16 03:19:25 -06:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Util;
use Fig\Http\Message\RequestMethodInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\RequestOptions;
2020-01-01 14:11:53 -06:00
use Laminas\Diactoros\Response;
2019-11-16 03:19:25 -06:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
2019-11-16 03:19:25 -06:00
use Shlinkio\Shlink\Core\Util\UrlValidator;
2019-11-16 03:19:25 -06:00
class UrlValidatorTest extends TestCase
{
private UrlValidator $urlValidator;
private ObjectProphecy $httpClient;
private UrlShortenerOptions $options;
2019-11-16 03:19:25 -06:00
public function setUp(): void
{
$this->httpClient = $this->prophesize(ClientInterface::class);
$this->options = new UrlShortenerOptions(['validate_url' => true]);
$this->urlValidator = new UrlValidator($this->httpClient->reveal(), $this->options);
2019-11-16 03:19:25 -06:00
}
/** @test */
public function exceptionIsThrownWhenUrlIsInvalid(): void
2019-11-16 03:19:25 -06:00
{
$request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class);
2019-11-16 03:19:25 -06:00
$request->shouldBeCalledOnce();
2019-11-16 03:19:25 -06:00
$this->expectException(InvalidUrlException::class);
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar');
}
/** @test */
public function expectedUrlIsCalledWhenTryingToVerify(): void
2019-11-16 03:19:25 -06:00
{
$expectedUrl = 'http://foobar.com';
2019-11-16 03:19:25 -06:00
$request = $this->httpClient->request(
RequestMethodInterface::METHOD_GET,
$expectedUrl,
2020-01-01 13:48:31 -06:00
[RequestOptions::ALLOW_REDIRECTS => ['max' => 15]],
)->willReturn(new Response());
2019-11-16 03:19:25 -06:00
$this->urlValidator->validateUrl($expectedUrl);
2019-11-16 03:19:25 -06:00
$request->shouldHaveBeenCalledOnce();
}
/** @test */
public function noCheckIsPerformedWhenUrlValidationIsDisabled(): void
{
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response());
$this->options->validateUrl = false;
$this->urlValidator->validateUrl('');
$request->shouldNotHaveBeenCalled();
}
2019-11-16 03:19:25 -06:00
}