|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Util; |
|
6
|
|
|
|
|
7
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
10
|
|
|
use GuzzleHttp\RequestOptions; |
|
11
|
|
|
use Laminas\Diactoros\Response; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use Prophecy\Argument; |
|
14
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
15
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; |
|
16
|
|
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; |
|
17
|
|
|
use Shlinkio\Shlink\Core\Util\UrlValidator; |
|
18
|
|
|
|
|
19
|
|
|
class UrlValidatorTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
private UrlValidator $urlValidator; |
|
22
|
|
|
private ObjectProphecy $httpClient; |
|
23
|
|
|
private UrlShortenerOptions $options; |
|
24
|
|
|
|
|
25
|
|
|
public function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class); |
|
28
|
|
|
$this->options = new UrlShortenerOptions(['validate_url' => true]); |
|
29
|
|
|
$this->urlValidator = new UrlValidator($this->httpClient->reveal(), $this->options); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** @test */ |
|
33
|
|
|
public function exceptionIsThrownWhenUrlIsInvalid(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class); |
|
36
|
|
|
|
|
37
|
|
|
$request->shouldBeCalledOnce(); |
|
38
|
|
|
$this->expectException(InvalidUrlException::class); |
|
39
|
|
|
|
|
40
|
|
|
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar', null); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** @test */ |
|
44
|
|
|
public function expectedUrlIsCalledWhenTryingToVerify(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$expectedUrl = 'http://foobar.com'; |
|
47
|
|
|
|
|
48
|
|
|
$request = $this->httpClient->request( |
|
49
|
|
|
RequestMethodInterface::METHOD_GET, |
|
50
|
|
|
$expectedUrl, |
|
51
|
|
|
[ |
|
52
|
|
|
RequestOptions::ALLOW_REDIRECTS => ['max' => 15], |
|
53
|
|
|
RequestOptions::IDN_CONVERSION => true, |
|
54
|
|
|
], |
|
55
|
|
|
)->willReturn(new Response()); |
|
56
|
|
|
|
|
57
|
|
|
$this->urlValidator->validateUrl($expectedUrl, null); |
|
58
|
|
|
|
|
59
|
|
|
$request->shouldHaveBeenCalledOnce(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
* @dataProvider provideDisabledCombinations |
|
65
|
|
|
*/ |
|
66
|
|
|
public function noCheckIsPerformedWhenUrlValidationIsDisabled(?bool $doValidate, bool $validateUrl): void |
|
67
|
|
|
{ |
|
68
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response()); |
|
69
|
|
|
$this->options->validateUrl = $validateUrl; |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$this->urlValidator->validateUrl('', $doValidate); |
|
72
|
|
|
|
|
73
|
|
|
$request->shouldNotHaveBeenCalled(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function provideDisabledCombinations(): iterable |
|
77
|
|
|
{ |
|
78
|
|
|
yield 'config is disabled and no runtime option is provided' => [null, false]; |
|
79
|
|
|
yield 'config is enabled but runtime option is disabled' => [false, true]; |
|
80
|
|
|
yield 'both config and runtime option are disabled' => [false, false]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|