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

176 lines
6.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;
2021-02-03 04:53:08 -06:00
use Laminas\Diactoros\Stream;
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;
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
{
2020-11-02 04:50:19 -06:00
use ProphecyTrait;
private ObjectProphecy $httpClient;
2019-11-16 03:19:25 -06:00
protected function setUp(): void
2019-11-16 03:19:25 -06:00
{
$this->httpClient = $this->prophesize(ClientInterface::class);
}
/** @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', true);
2019-11-16 03:19:25 -06:00
}
/** @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,
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;
}),
)->willReturn(new Response());
2019-11-16 03:19:25 -06:00
$this->urlValidator()->validateUrl($expectedUrl, true);
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->urlValidator()->validateUrl('', false);
$request->shouldNotHaveBeenCalled();
}
/** @test */
public function validateUrlWithTitleReturnsNullWhenRequestFailsAndValidationIsDisabled(): void
{
2021-02-03 04:53:08 -06:00
$request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class);
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
2021-02-03 04:53:08 -06:00
self::assertNull($result);
$request->shouldHaveBeenCalledOnce();
}
/** @test */
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsDisabled(): void
{
$request = $this->httpClient->request(Argument::cetera())->willReturn($this->respWithTitle());
$result = $this->urlValidator()->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
2021-02-03 04:53:08 -06:00
self::assertNull($result);
$request->shouldNotHaveBeenCalled();
2021-02-03 04:53:08 -06:00
}
/** @test */
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsDisabledAndValidationIsEnabled(): void
{
$request = $this->httpClient->request(RequestMethodInterface::METHOD_HEAD, Argument::cetera())->willReturn(
$this->respWithTitle(),
);
$result = $this->urlValidator()->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
self::assertNull($result);
$request->shouldHaveBeenCalledOnce();
}
2021-02-03 04:53:08 -06:00
/** @test */
public function validateUrlWithTitleResolvesTitleWhenAutoResolutionIsEnabled(): void
{
$request = $this->httpClient->request(RequestMethodInterface::METHOD_GET, Argument::cetera())->willReturn(
$this->respWithTitle(),
);
2021-02-03 04:53:08 -06:00
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
2021-02-03 04:53:08 -06:00
self::assertEquals('Resolved "title"', $result);
2021-02-03 04:53:08 -06:00
$request->shouldHaveBeenCalledOnce();
}
/** @test */
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsEnabledAndReturnedContentTypeIsInvalid(): void
{
$request = $this->httpClient->request(RequestMethodInterface::METHOD_GET, Argument::cetera())->willReturn(
new Response('php://memory', 200, ['Content-Type' => 'application/octet-stream']),
);
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
self::assertNull($result);
$request->shouldHaveBeenCalledOnce();
}
/** @test */
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsEnabledAndBodyDoesNotContainTitle(): void
{
$request = $this->httpClient->request(RequestMethodInterface::METHOD_GET, Argument::cetera())->willReturn(
new Response($this->createStreamWithContent('<body>No title</body>'), 200, ['Content-Type' => 'text/html']),
);
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
self::assertNull($result);
$request->shouldHaveBeenCalledOnce();
}
2021-02-03 04:53:08 -06:00
private function respWithTitle(): Response
{
$body = $this->createStreamWithContent('<title data-foo="bar"> Resolved &quot;title&quot; </title>');
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');
$body->write($content);
$body->rewind();
2021-02-03 04:53:08 -06:00
return $body;
2021-02-03 04:53:08 -06:00
}
public function urlValidator(bool $autoResolveTitles = false): UrlValidator
{
return new UrlValidator(
$this->httpClient->reveal(),
new UrlShortenerOptions(autoResolveTitles: $autoResolveTitles),
);
}
2019-11-16 03:19:25 -06:00
}