|
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'); |
|
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
|
|
|
[RequestOptions::ALLOW_REDIRECTS => ['max' => 15]], |
|
52
|
|
|
)->willReturn(new Response()); |
|
53
|
|
|
|
|
54
|
|
|
$this->urlValidator->validateUrl($expectedUrl); |
|
55
|
|
|
|
|
56
|
|
|
$request->shouldHaveBeenCalledOnce(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @test */ |
|
60
|
|
|
public function noCheckIsPerformedWhenUrlValidationIsDisabled(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response()); |
|
63
|
|
|
$this->options->validateUrl = false; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$this->urlValidator->validateUrl(''); |
|
66
|
|
|
|
|
67
|
|
|
$request->shouldNotHaveBeenCalled(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|