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;
|
2022-10-23 13:39:06 -05:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
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;
|
2022-10-23 13:39:06 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-11-16 03:19:25 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
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
|
|
|
|
{
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & ClientInterface $httpClient;
|
2019-11-16 03:19:25 -06:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2019-11-16 03:19:25 -06:00
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient = $this->createMock(ClientInterface::class);
|
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
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->once())->method('request')->willThrowException($this->clientException());
|
2019-11-16 03:19:25 -06:00
|
|
|
$this->expectException(InvalidUrlException::class);
|
|
|
|
|
2022-09-17 08:54:43 -05:00
|
|
|
$this->urlValidator()->validateUrl('http://foobar.com/12345/hello?foo=bar', true);
|
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';
|
|
|
|
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->once())->method('request')->with(
|
2019-11-16 03:19:25 -06:00
|
|
|
RequestMethodInterface::METHOD_GET,
|
|
|
|
$expectedUrl,
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->callback(function (array $options) {
|
2021-02-18 14:27:46 -06:00
|
|
|
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
|
|
|
|
2022-09-17 08:54:43 -05:00
|
|
|
$this->urlValidator()->validateUrl($expectedUrl, true);
|
2019-11-16 03:19:25 -06:00
|
|
|
}
|
2020-03-22 10:58:28 -05:00
|
|
|
|
2022-02-01 12:12:53 -06:00
|
|
|
/** @test */
|
|
|
|
public function noCheckIsPerformedWhenUrlValidationIsDisabled(): void
|
2020-03-22 10:58:28 -05:00
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->never())->method('request');
|
2022-09-17 08:54:43 -05:00
|
|
|
$this->urlValidator()->validateUrl('', false);
|
2020-03-22 10:58:28 -05:00
|
|
|
}
|
2020-09-23 12:19:17 -05:00
|
|
|
|
2022-02-01 12:12:53 -06:00
|
|
|
/** @test */
|
|
|
|
public function validateUrlWithTitleReturnsNullWhenRequestFailsAndValidationIsDisabled(): void
|
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->once())->method('request')->willThrowException($this->clientException());
|
2021-02-03 04:53:08 -06:00
|
|
|
|
2022-09-17 08:54:43 -05:00
|
|
|
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
|
2021-02-03 04:53:08 -06:00
|
|
|
|
|
|
|
self::assertNull($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsDisabled(): void
|
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->never())->method('request');
|
2021-02-03 04:53:08 -06:00
|
|
|
|
2022-09-17 08:54:43 -05:00
|
|
|
$result = $this->urlValidator()->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
|
2021-02-03 04:53:08 -06:00
|
|
|
|
|
|
|
self::assertNull($result);
|
|
|
|
}
|
|
|
|
|
2022-02-01 12:44:14 -06:00
|
|
|
/** @test */
|
|
|
|
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsDisabledAndValidationIsEnabled(): void
|
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->once())->method('request')->with(
|
|
|
|
RequestMethodInterface::METHOD_HEAD,
|
|
|
|
$this->anything(),
|
|
|
|
$this->anything(),
|
|
|
|
)->willReturn($this->respWithTitle());
|
2022-02-01 12:44:14 -06:00
|
|
|
|
2022-09-17 08:54:43 -05:00
|
|
|
$result = $this->urlValidator()->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
2022-02-01 12:44:14 -06:00
|
|
|
|
|
|
|
self::assertNull($result);
|
|
|
|
}
|
|
|
|
|
2021-02-03 04:53:08 -06:00
|
|
|
/** @test */
|
|
|
|
public function validateUrlWithTitleResolvesTitleWhenAutoResolutionIsEnabled(): void
|
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->once())->method('request')->with(
|
|
|
|
RequestMethodInterface::METHOD_GET,
|
|
|
|
$this->anything(),
|
|
|
|
$this->anything(),
|
|
|
|
)->willReturn($this->respWithTitle());
|
2021-02-03 04:53:08 -06:00
|
|
|
|
2022-09-17 08:54:43 -05:00
|
|
|
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
2021-02-03 04:53:08 -06:00
|
|
|
|
2022-05-22 01:29:26 -05:00
|
|
|
self::assertEquals('Resolved "title"', $result);
|
2021-02-03 04:53:08 -06:00
|
|
|
}
|
|
|
|
|
2022-05-01 04:48:20 -05:00
|
|
|
/** @test */
|
|
|
|
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsEnabledAndReturnedContentTypeIsInvalid(): void
|
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->once())->method('request')->with(
|
|
|
|
RequestMethodInterface::METHOD_GET,
|
|
|
|
$this->anything(),
|
|
|
|
$this->anything(),
|
|
|
|
)->willReturn(new Response('php://memory', 200, ['Content-Type' => 'application/octet-stream']));
|
2022-05-01 04:48:20 -05:00
|
|
|
|
2022-09-17 08:54:43 -05:00
|
|
|
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
2022-05-01 04:48:20 -05:00
|
|
|
|
|
|
|
self::assertNull($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsEnabledAndBodyDoesNotContainTitle(): void
|
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
$this->httpClient->expects($this->once())->method('request')->with(
|
|
|
|
RequestMethodInterface::METHOD_GET,
|
|
|
|
$this->anything(),
|
|
|
|
$this->anything(),
|
|
|
|
)->willReturn(
|
2022-05-01 04:48:20 -05:00
|
|
|
new Response($this->createStreamWithContent('<body>No title</body>'), 200, ['Content-Type' => 'text/html']),
|
|
|
|
);
|
|
|
|
|
2022-09-17 08:54:43 -05:00
|
|
|
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
2022-05-01 04:48:20 -05:00
|
|
|
|
|
|
|
self::assertNull($result);
|
|
|
|
}
|
|
|
|
|
2021-02-03 04:53:08 -06:00
|
|
|
private function respWithTitle(): Response
|
2022-05-01 04:48:20 -05:00
|
|
|
{
|
2022-05-22 01:29:26 -05:00
|
|
|
$body = $this->createStreamWithContent('<title data-foo="bar"> Resolved "title" </title>');
|
2022-05-01 04:48:20 -05:00
|
|
|
return new Response($body, 200, ['Content-Type' => 'TEXT/html; charset=utf-8']);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createStreamWithContent(string $content): Stream
|
2021-02-03 04:53:08 -06:00
|
|
|
{
|
|
|
|
$body = new Stream('php://temp', 'wr');
|
2022-05-01 04:48:20 -05:00
|
|
|
$body->write($content);
|
|
|
|
$body->rewind();
|
2021-02-03 04:53:08 -06:00
|
|
|
|
2022-05-01 04:48:20 -05:00
|
|
|
return $body;
|
2021-02-03 04:53:08 -06:00
|
|
|
}
|
2022-09-17 08:54:43 -05:00
|
|
|
|
2022-10-23 13:39:06 -05:00
|
|
|
private function clientException(): ClientException
|
2022-09-17 08:54:43 -05:00
|
|
|
{
|
2022-10-23 13:39:06 -05:00
|
|
|
return new ClientException(
|
|
|
|
'',
|
|
|
|
new Request(RequestMethodInterface::METHOD_GET, ''),
|
|
|
|
new Response(),
|
2022-09-17 08:54:43 -05:00
|
|
|
);
|
|
|
|
}
|
2022-10-23 13:39:06 -05:00
|
|
|
|
|
|
|
public function urlValidator(bool $autoResolveTitles = false): UrlValidator
|
|
|
|
{
|
|
|
|
return new UrlValidator($this->httpClient, new UrlShortenerOptions(autoResolveTitles: $autoResolveTitles));
|
|
|
|
}
|
2019-11-16 03:19:25 -06:00
|
|
|
}
|