|
26 | 26 | use PHPUnit\Framework\TestCase; |
27 | 27 | use Psr\Container\ContainerInterface; |
28 | 28 | use Psr\Http\Message\RequestInterface; |
| 29 | +use ReflectionClass; |
29 | 30 | use Swoole\Http\Request as SwooleRequest; |
30 | 31 |
|
31 | 32 | /** |
@@ -155,6 +156,68 @@ public function testGetUriFromGlobals() |
155 | 156 | $this->assertSame(null, $uri->getPort()); |
156 | 157 | } |
157 | 158 |
|
| 159 | + /** |
| 160 | + * @group ParseHost |
| 161 | + */ |
| 162 | + public function testParseHost() |
| 163 | + { |
| 164 | + $hostStrIPv4 = '192.168.119.100:9501'; |
| 165 | + $hostStrIPv6 = '[fe80::a464:1aff:fe88:7b5a]:9502'; |
| 166 | + $objReflectClass = new ReflectionClass('Hyperf\HttpMessage\Server\Request'); |
| 167 | + $method = $objReflectClass->getMethod('parseHost'); |
| 168 | + $method->setAccessible(true); |
| 169 | + |
| 170 | + $resIPv4 = $method->invokeArgs(null, [$hostStrIPv4]); |
| 171 | + $this->assertSame('192.168.119.100', $resIPv4[0]); |
| 172 | + $this->assertSame(9501, $resIPv4[1]); |
| 173 | + |
| 174 | + $resIPv6 = $method->invokeArgs(null, [$hostStrIPv6]); |
| 175 | + $this->assertSame('[fe80::a464:1aff:fe88:7b5a]', $resIPv6[0]); |
| 176 | + $this->assertSame(9502, $resIPv6[1]); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @dataProvider getIPv6Examples |
| 181 | + * @param mixed $originHost |
| 182 | + * @param mixed $host |
| 183 | + * @param mixed $port |
| 184 | + */ |
| 185 | + public function testGetUriFromGlobalsForIPv6Host($originHost, $host, $port) |
| 186 | + { |
| 187 | + $swooleRequest = Mockery::mock(SwooleRequest::class); |
| 188 | + $data = ['name' => 'Hyperf']; |
| 189 | + $swooleRequest->shouldReceive('rawContent')->andReturn(Json::encode($data)); |
| 190 | + |
| 191 | + $swooleRequest->server = [ |
| 192 | + 'http_host' => $originHost, |
| 193 | + ]; |
| 194 | + $request = Request::loadFromSwooleRequest($swooleRequest); |
| 195 | + $uri = $request->getUri(); |
| 196 | + $this->assertSame($port, $uri->getPort()); |
| 197 | + $this->assertSame($host, $uri->getHost()); |
| 198 | + |
| 199 | + $swooleRequest->server = []; |
| 200 | + $swooleRequest->header = [ |
| 201 | + 'host' => $originHost, |
| 202 | + ]; |
| 203 | + $request = Request::loadFromSwooleRequest($swooleRequest); |
| 204 | + $uri = $request->getUri(); |
| 205 | + $this->assertSame($port, $uri->getPort()); |
| 206 | + $this->assertSame($host, $uri->getHost()); |
| 207 | + } |
| 208 | + |
| 209 | + public function getIPv6Examples(): array |
| 210 | + { |
| 211 | + return [ |
| 212 | + ['localhost:9501', 'localhost', 9501], |
| 213 | + ['localhost:', 'localhost', null], |
| 214 | + ['localhost', 'localhost', null], |
| 215 | + ['[2a00:f48:1008::212:183:10]', '[2a00:f48:1008::212:183:10]', null], |
| 216 | + ['[2a00:f48:1008::212:183:10]:9501', '[2a00:f48:1008::212:183:10]', 9501], |
| 217 | + ['[2a00:f48:1008::212:183:10]:', '[2a00:f48:1008::212:183:10]', null], |
| 218 | + ]; |
| 219 | + } |
| 220 | + |
158 | 221 | protected function getContainer() |
159 | 222 | { |
160 | 223 | $container = Mockery::mock(ContainerInterface::class); |
|
0 commit comments