|
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 PHPUnit\Framework\TestCase; |
|
11
|
|
|
use Prophecy\Argument; |
|
12
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Util\UrlValidator; |
|
15
|
|
|
use Zend\Diactoros\Request; |
|
16
|
|
|
|
|
17
|
|
|
class UrlValidatorTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var UrlValidator */ |
|
20
|
|
|
private $urlValidator; |
|
21
|
|
|
/** @var ObjectProphecy */ |
|
22
|
|
|
private $httpClient; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class); |
|
27
|
|
|
$this->urlValidator = new UrlValidator($this->httpClient->reveal()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** @test */ |
|
31
|
|
|
public function exceptionIsThrownWhenUrlIsInvalid(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willThrow( |
|
34
|
|
|
new ClientException('', $this->prophesize(Request::class)->reveal()) |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
$this->expectException(InvalidUrlException::class); |
|
38
|
|
|
$request->shouldBeCalledOnce(); |
|
39
|
|
|
|
|
40
|
|
|
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
* @dataProvider provideUrls |
|
46
|
|
|
*/ |
|
47
|
|
|
public function expectedUrlIsCalledInOrderToVerifyProvidedUrl(string $providedUrl, string $expectedUrl): void |
|
48
|
|
|
{ |
|
49
|
|
|
$request = $this->httpClient->request( |
|
50
|
|
|
RequestMethodInterface::METHOD_GET, |
|
51
|
|
|
$expectedUrl, |
|
52
|
|
|
Argument::cetera() |
|
53
|
|
|
)->will(function () { |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
$this->urlValidator->validateUrl($providedUrl); |
|
57
|
|
|
|
|
58
|
|
|
$request->shouldHaveBeenCalledOnce(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function provideUrls(): iterable |
|
62
|
|
|
{ |
|
63
|
|
|
yield 'regular domain' => ['http://foobar.com', 'http://foobar.com']; |
|
64
|
|
|
yield 'IDN' => ['https://cédric.laubacher.io/', 'https://xn--cdric-bsa.laubacher.io/']; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|