diff --git a/src/Ratchet/Server/IoServer.php b/src/Ratchet/Server/IoServer.php index d54f20f5..8550dac2 100644 --- a/src/Ratchet/Server/IoServer.php +++ b/src/Ratchet/Server/IoServer.php @@ -97,7 +97,7 @@ public function handleConnect(SocketConnection $conn) { $conn->decor->resourceId = (int)$conn->stream; - $uri = $conn->getRemoteAddress(); + $uri = (string) $conn->getRemoteAddress(); $conn->decor->remoteAddress = trim( parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST), '[]' diff --git a/tests/unit/Server/IoServerTest.php b/tests/unit/Server/IoServerTest.php index c49c6da4..be42c922 100644 --- a/tests/unit/Server/IoServerTest.php +++ b/tests/unit/Server/IoServerTest.php @@ -63,6 +63,23 @@ public function testOnOpen() { //$this->assertTrue(is_int($this->app->last['onOpen'][0]->resourceId)); } + public function testHandleOpenWithoutRemoteAddressAssignsEmptyRemoteAddress() { + $this->app->expects($this->once())->method('onOpen')->with($this->isInstanceOf('Ratchet\\ConnectionInterface')); + + $conn = $this->getMockBuilder('React\\Socket\\ConnectionInterface')->getMock(); + $conn->expects($this->once())->method('getRemoteAddress')->willReturn(null); + + // assign dynamic property without raising notice on PHP 8.2+ + set_error_handler(function () { }, E_DEPRECATED); + $conn->stream = STDOUT; + restore_error_handler(); + + $this->server->handleConnect($conn); + + $this->assertSame('', $conn->decor->remoteAddress); + $this->assertSame((int) STDOUT, $conn->decor->resourceId); + } + /** * @requires extension sockets */