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;
|
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\Util\UrlValidator;
|
2019-11-16 05:38:45 -06:00
|
|
|
use Zend\Diactoros\Response;
|
|
|
|
|
2019-11-16 03:19:25 -06:00
|
|
|
class UrlValidatorTest extends TestCase
|
|
|
|
{
|
2019-12-29 15:48:40 -06:00
|
|
|
private UrlValidator $urlValidator;
|
|
|
|
private ObjectProphecy $httpClient;
|
2019-11-16 03:19:25 -06:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class);
|
|
|
|
$this->urlValidator = new UrlValidator($this->httpClient->reveal());
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar');
|
|
|
|
}
|
|
|
|
|
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,
|
2019-12-21 09:09:29 -06:00
|
|
|
[RequestOptions::ALLOW_REDIRECTS => ['max' => 15]]
|
2019-11-16 05:38:45 -06:00
|
|
|
)->willReturn(new Response());
|
2019-11-16 03:19:25 -06:00
|
|
|
|
2019-12-07 14:01:14 -06:00
|
|
|
$this->urlValidator->validateUrl($expectedUrl);
|
2019-11-16 03:19:25 -06:00
|
|
|
|
|
|
|
$request->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
}
|