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 PHPUnit\Framework\TestCase; |
12
|
|
|
use Prophecy\Argument; |
13
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
14
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; |
15
|
|
|
use Shlinkio\Shlink\Core\Util\UrlValidator; |
16
|
|
|
use Zend\Diactoros\Response; |
17
|
|
|
|
18
|
|
|
class UrlValidatorTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** @var UrlValidator */ |
21
|
|
|
private $urlValidator; |
22
|
|
|
/** @var ObjectProphecy */ |
23
|
|
|
private $httpClient; |
24
|
|
|
|
25
|
|
|
public function setUp(): void |
26
|
|
|
{ |
27
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class); |
28
|
|
|
$this->urlValidator = new UrlValidator($this->httpClient->reveal()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @test */ |
32
|
|
|
public function exceptionIsThrownWhenUrlIsInvalid(): void |
33
|
|
|
{ |
34
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class); |
35
|
|
|
|
36
|
|
|
$request->shouldBeCalledOnce(); |
37
|
|
|
$this->expectException(InvalidUrlException::class); |
38
|
|
|
|
39
|
|
|
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** @test */ |
43
|
|
|
public function expectedUrlIsCalledWhenTryingToVerify(): void |
44
|
|
|
{ |
45
|
|
|
$expectedUrl = 'http://foobar.com'; |
46
|
|
|
|
47
|
|
|
$request = $this->httpClient->request( |
48
|
|
|
RequestMethodInterface::METHOD_GET, |
49
|
|
|
$expectedUrl, |
50
|
|
|
[RequestOptions::ALLOW_REDIRECTS => ['max' => 15]] |
51
|
|
|
)->willReturn(new Response()); |
52
|
|
|
|
53
|
|
|
$this->urlValidator->validateUrl($expectedUrl); |
54
|
|
|
|
55
|
|
|
$request->shouldHaveBeenCalledOnce(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|