|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpClient\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpClient\Exception\InvalidArgumentException; |
| 16 | +use Symfony\Component\HttpClient\Exception\TransportException; |
| 17 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 18 | +use Symfony\Component\HttpClient\NoPrivateNetworkHttpClient; |
| 19 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 20 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 21 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 22 | + |
| 23 | +class NoPrivateNetworkHttpClientTest extends TestCase |
| 24 | +{ |
| 25 | + public function getBlacklistData(): array |
| 26 | + { |
| 27 | + return [ |
| 28 | + // private |
| 29 | + ['0.0.0.1', null, true], |
| 30 | + ['169.254.0.1', null, true], |
| 31 | + ['127.0.0.1', null, true], |
| 32 | + ['240.0.0.1', null, true], |
| 33 | + ['10.0.0.1', null, true], |
| 34 | + ['172.16.0.1', null, true], |
| 35 | + ['192.168.0.1', null, true], |
| 36 | + ['::1', null, true], |
| 37 | + ['::ffff:0:1', null, true], |
| 38 | + ['fe80::1', null, true], |
| 39 | + ['fc00::1', null, true], |
| 40 | + ['fd00::1', null, true], |
| 41 | + ['10.0.0.1', '10.0.0.0/24', true], |
| 42 | + ['10.0.0.1', '10.0.0.1', true], |
| 43 | + ['fc00::1', 'fc00::1/120', true], |
| 44 | + ['fc00::1', 'fc00::1', true], |
| 45 | + |
| 46 | + ['172.16.0.1', ['10.0.0.0/8', '192.168.0.0/16'], false], |
| 47 | + ['fc00::1', ['fe80::/10', '::ffff:0:0/96'], false], |
| 48 | + |
| 49 | + // public |
| 50 | + ['104.26.14.6', null, false], |
| 51 | + ['104.26.14.6', '104.26.14.0/24', true], |
| 52 | + ['2606:4700:20::681a:e06', null, false], |
| 53 | + ['2606:4700:20::681a:e06', '2606:4700:20::/43', true], |
| 54 | + |
| 55 | + // no ipv4/ipv6 at all |
| 56 | + ['2606:4700:20::681a:e06', '::/0', true], |
| 57 | + ['104.26.14.6', '0.0.0.0/0', true], |
| 58 | + |
| 59 | + // weird scenarios (e.g.: when trying to match ipv4 address on ipv6 subnet) |
| 60 | + ['10.0.0.1', 'fc00::/7', false], |
| 61 | + ['fc00::1', '10.0.0.0/8', false], |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66
F438
| + * @dataProvider getBlacklistData |
| 67 | + */ |
| 68 | + public function testBlacklist(string $ipAddr, $subnets, bool $mustThrow) |
| 69 | + { |
| 70 | + $content = 'foo'; |
| 71 | + $url = sprintf('http://%s/', 0 < substr_count($ipAddr, ':') ? sprintf('[%s]', $ipAddr) : $ipAddr); |
| 72 | + |
| 73 | + if ($mustThrow) { |
| 74 | + $this->expectException(TransportException::class); |
| 75 | + $this->expectExceptionMessage(sprintf('IP "%s" is blacklisted for "%s".', $ipAddr, $url)); |
| 76 | + } |
| 77 | + |
| 78 | + $previousHttpClient = $this->getHttpClientMock($url, $ipAddr, $content); |
| 79 | + $client = new NoPrivateNetworkHttpClient($previousHttpClient, $subnets); |
| 80 | + $response = $client->request('GET', $url); |
| 81 | + |
| 82 | + if (!$mustThrow) { |
| 83 | + $this->assertEquals($content, $response->getContent()); |
| 84 | + $this->assertEquals(200, $response->getStatusCode()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public function testCustomOnProgressCallback() |
| 89 | + { |
| 90 | + $ipAddr = '104.26.14.6'; |
| 91 | + $url = sprintf('http://%s/', $ipAddr); |
| 92 | + $content = 'foo'; |
| 93 | + |
| 94 | + $executionCount = 0; |
| 95 | + $customCallback = function (int $dlNow, int $dlSize, array $info) use (&$executionCount): void { |
| 96 | + ++$executionCount; |
| 97 | + }; |
| 98 | + |
| 99 | + $previousHttpClient = $this->getHttpClientMock($url, $ipAddr, $content); |
| 100 | + $client = new NoPrivateNetworkHttpClient($previousHttpClient); |
| 101 | + $response = $client->request('GET', $url, ['on_progress' => $customCallback]); |
| 102 | + |
| 103 | + $this->assertEquals(1, $executionCount); |
| 104 | + $this->assertEquals($content, $response->getContent()); |
| 105 | + $this->assertEquals(200, $response->getStatusCode()); |
| 106 | + } |
| 107 | + |
| 108 | + public function testNonCallableOnProgressCallback() |
| 109 | + { |
| 110 | + $ipAddr = '104.26.14.6'; |
| 111 | + $url = sprintf('http://%s/', $ipAddr); |
| 112 | + $content = 'bar'; |
| 113 | + $customCallback = sprintf('cb_%s', microtime(true)); |
| 114 | + |
| 115 | + $this->expectException(InvalidArgumentException::class); |
| 116 | + $this->expectExceptionMessage('Option "on_progress" must be callable, string given.'); |
| 117 | + |
| 118 | + $client = new NoPrivateNetworkHttpClient(new MockHttpClient()); |
| 119 | + $client->request('GET', $url, ['on_progress' => $customCallback]); |
| 120 | + } |
| 121 | + |
| 122 | + public function testConstructor() |
| 123 | + { |
| 124 | + $this->expectException(\TypeError::class); |
| 125 | + $this->expectExceptionMessage('Argument 2 passed to Symfony\Component\HttpClient\NoPrivateNetworkHttpClient::__construct() must be of the type array, string or null. integer given.'); |
| 126 | + |
| 127 | + new NoPrivateNetworkHttpClient(new MockHttpClient(), 3); |
| 128 | + } |
| 129 | + |
| 130 | + private function getHttpClientMock(string $url, string $ipAddr, string $content) |
| 131 | + { |
| 132 | + $previousHttpClient = $this |
| 133 | + ->getMockBuilder(HttpClientInterface::class) |
| 134 | + ->getMock(); |
| 135 | + |
| 136 | + $previousHttpClient |
| 137 | + ->expects($this->once()) |
| 138 | + ->method('request') |
| 139 | + ->with( |
| 140 | + 'GET', |
| 141 | + $url, |
| 142 | + $this->callback(function ($options) { |
| 143 | + $this->assertArrayHasKey('on_progress', $options); |
| 144 | + $onProgress = $options['on_progress']; |
| 145 | + $this->assertIsCallable($onProgress); |
| 146 | + |
| 147 | + return true; |
| 148 | + }) |
| 149 | + ) |
| 150 | + ->willReturnCallback(function ($method, $url, $options) use ($ipAddr, $content): ResponseInterface { |
| 151 | + $info = [ |
| 152 | + 'primary_ip' => $ipAddr, |
| 153 | + 'url' => $url, |
| 154 | + ]; |
| 155 | + |
| 156 | + $onProgress = $options['on_progress']; |
| 157 | + $onProgress(0, 0, $info); |
| 158 | + |
| 159 | + return MockResponse::fromRequest($method, $url, [], new MockResponse($content)); |
| 160 | + }); |
| 161 | + |
| 162 | + return $previousHttpClient; |
| 163 | + } |
| 164 | +} |
0 commit comments