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;
|
2019-12-21 09:09:29 -06:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\Diactoros\Response;
|
2021-02-03 04:53:08 -06:00
|
|
|
use Laminas\Diactoros\Stream;
|
2021-02-18 14:27:46 -06:00
|
|
|
use PHPUnit\Framework\Assert;
|
2019-11-16 03:19:25 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2019-11-16 03:19:25 -06:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
2020-03-22 10:58:28 -05:00
|
|
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
2019-11-16 03:19:25 -06:00
|
|
|
use Shlinkio\Shlink\Core\Util\UrlValidator;
|
2019-11-16 05:38:45 -06:00
|
|
|
|
2019-11-16 03:19:25 -06:00
|
|
|
class UrlValidatorTest extends TestCase
|
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-29 15:48:40 -06:00
|
|
|
private UrlValidator $urlValidator;
|
|
|
|
private ObjectProphecy $httpClient;
|
2020-03-22 10:58:28 -05:00
|
|
|
private UrlShortenerOptions $options;
|
2019-11-16 03:19:25 -06:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class);
|
2020-03-22 10:58:28 -05:00
|
|
|
$this->options = new UrlShortenerOptions(['validate_url' => true]);
|
|
|
|
$this->urlValidator = new UrlValidator($this->httpClient->reveal(), $this->options);
|
2019-11-16 03:19:25 -06:00
|
|
|
}
|
|
|
|
|
2019-12-21 09:09:29 -06:00
|
|
|
/** @test */
|
|
|
|
public function exceptionIsThrownWhenUrlIsInvalid(): void
|
2019-11-16 03:19:25 -06:00
|
|
|
{
|
2019-12-21 09:09:29 -06:00
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class);
|
2019-11-16 03:19:25 -06:00
|
|
|
|
2019-12-21 09:09:29 -06:00
|
|
|
$request->shouldBeCalledOnce();
|
2019-11-16 03:19:25 -06:00
|
|
|
$this->expectException(InvalidUrlException::class);
|
|
|
|
|
2020-09-23 12:19:17 -05:00
|
|
|
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar', null);
|
2019-11-16 03:19:25 -06:00
|
|
|
}
|
|
|
|
|
2019-12-07 14:01:14 -06:00
|
|
|
/** @test */
|
|
|
|
public function expectedUrlIsCalledWhenTryingToVerify(): void
|
2019-11-16 03:19:25 -06:00
|
|
|
{
|
2019-12-07 14:01:14 -06:00
|
|
|
$expectedUrl = 'http://foobar.com';
|
|
|
|
|
2019-11-16 03:19:25 -06:00
|
|
|
$request = $this->httpClient->request(
|
|
|
|
RequestMethodInterface::METHOD_GET,
|
|
|
|
$expectedUrl,
|
2021-02-18 14:27:46 -06:00
|
|
|
Argument::that(function (array $options) {
|
|
|
|
Assert::assertArrayHasKey(RequestOptions::ALLOW_REDIRECTS, $options);
|
|
|
|
Assert::assertEquals(['max' => 15], $options[RequestOptions::ALLOW_REDIRECTS]);
|
|
|
|
Assert::assertArrayHasKey(RequestOptions::IDN_CONVERSION, $options);
|
|
|
|
Assert::assertTrue($options[RequestOptions::IDN_CONVERSION]);
|
|
|
|
Assert::assertArrayHasKey(RequestOptions::HEADERS, $options);
|
|
|
|
Assert::assertArrayHasKey('User-Agent', $options[RequestOptions::HEADERS]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}),
|
2019-11-16 05:38:45 -06:00
|
|
|
)->willReturn(new Response());
|
2019-11-16 03:19:25 -06:00
|
|
|
|
2020-09-23 12:19:17 -05:00
|
|
|
$this->urlValidator->validateUrl($expectedUrl, null);
|
2019-11-16 03:19:25 -06:00
|
|
|
|
|
|
|
$request->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2020-03-22 10:58:28 -05:00
|
|
|
|
2020-09-23 12:19:17 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDisabledCombinations
|
|
|
|
*/
|
|
|
|
public function noCheckIsPerformedWhenUrlValidationIsDisabled(?bool $doValidate, bool $validateUrl): void
|
2020-03-22 10:58:28 -05:00
|
|
|
{
|
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response());
|
2020-09-23 12:19:17 -05:00
|
|
|
$this->options->validateUrl = $validateUrl;
|
2020-03-22 10:58:28 -05:00
|
|
|
|
2020-09-23 12:19:17 -05:00
|
|
|
$this->urlValidator->validateUrl('', $doValidate);
|
2020-03-22 10:58:28 -05:00
|
|
|
|
|
|
|
$request->shouldNotHaveBeenCalled();
|
|
|
|
}
|
2020-09-23 12:19:17 -05:00
|
|
|
|
2021-02-03 04:53:08 -06:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDisabledCombinations
|
|
|
|
*/
|
|
|
|
public function validateUrlWithTitleReturnsNullWhenRequestFailsAndValidationIsDisabled(
|
|
|
|
?bool $doValidate,
|
|
|
|
bool $validateUrl
|
|
|
|
): void {
|
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class);
|
|
|
|
$this->options->validateUrl = $validateUrl;
|
2021-02-05 11:22:54 -06:00
|
|
|
$this->options->autoResolveTitles = true;
|
2021-02-03 04:53:08 -06:00
|
|
|
|
|
|
|
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', $doValidate);
|
|
|
|
|
|
|
|
self::assertNull($result);
|
|
|
|
$request->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
2020-09-23 12:19:17 -05:00
|
|
|
public function provideDisabledCombinations(): iterable
|
|
|
|
{
|
|
|
|
yield 'config is disabled and no runtime option is provided' => [null, false];
|
|
|
|
yield 'config is enabled but runtime option is disabled' => [false, true];
|
|
|
|
yield 'both config and runtime option are disabled' => [false, false];
|
|
|
|
}
|
2021-02-03 04:53:08 -06:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsDisabled(): void
|
|
|
|
{
|
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willReturn($this->respWithTitle());
|
|
|
|
$this->options->autoResolveTitles = false;
|
|
|
|
|
2021-02-05 11:22:54 -06:00
|
|
|
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
|
2021-02-03 04:53:08 -06:00
|
|
|
|
|
|
|
self::assertNull($result);
|
2021-02-05 11:22:54 -06:00
|
|
|
$request->shouldNotHaveBeenCalled();
|
2021-02-03 04:53:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function validateUrlWithTitleResolvesTitleWhenAutoResolutionIsEnabled(): void
|
|
|
|
{
|
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willReturn($this->respWithTitle());
|
|
|
|
$this->options->autoResolveTitles = true;
|
|
|
|
|
|
|
|
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
|
|
|
|
|
|
|
self::assertEquals('Resolved title', $result);
|
|
|
|
$request->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function respWithTitle(): Response
|
|
|
|
{
|
|
|
|
$body = new Stream('php://temp', 'wr');
|
|
|
|
$body->write('<title> Resolved title</title>');
|
|
|
|
|
|
|
|
return new Response($body);
|
|
|
|
}
|
2019-11-16 03:19:25 -06:00
|
|
|
}
|