newConnection(); $this->wsHandler->onOpen($connection); $connection->assertSentEvent('websocket.connection_established'); } public function test_connection_established_contains_socket_id() { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $event = collect($connection->sentData)->firstWhere('event', 'websocket.connection_established'); $data = json_decode($event['data'], true); $this->assertNotNull($data['socket_id']); $this->assertStringContainsString('.', $data['socket_id']); } public function test_connection_established_contains_activity_timeout() { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $event = collect($connection->sentData)->firstWhere('event', 'websocket.connection_established'); $data = json_decode($event['data'], true); $this->assertEquals(30, $data['activity_timeout']); } public function test_multiple_connections_get_unique_socket_ids() { $conn1 = $this->newConnection(); $conn2 = $this->newConnection(); $conn3 = $this->newConnection(); $this->wsHandler->onOpen($conn1); $this->wsHandler->onOpen($conn2); $this->wsHandler->onOpen($conn3); $socket1 = json_decode((collect($conn1->sentData)->firstWhere('event', 'websocket.connection_established')['data'] ?? '{}'), true)['socket_id'] ?? null; $socket2 = json_decode((collect($conn2->sentData)->firstWhere('event', 'websocket.connection_established')['data'] ?? '{}'), true)['socket_id'] ?? null; $socket3 = json_decode((collect($conn3->sentData)->firstWhere('event', 'websocket.connection_established')['data'] ?? '{}'), true)['socket_id'] ?? null; $this->assertNotNull($socket1); $this->assertNotNull($socket2); $this->assertNotNull($socket3); $this->assertNotEquals($socket1, $socket2); $this->assertNotEquals($socket2, $socket3); $this->assertNotEquals($socket1, $socket3); } // ========================================================================= // Public channel subscribe/unsubscribe // ========================================================================= public function test_subscribe_to_public_channel() { $connection = $this->newActiveConnection(['test-channel']); $connection->assertSentEvent('websocket_internal.subscription_succeeded'); $channel = $this->channelManager->find('1234', 'test-channel'); $this->assertTrue($channel->hasConnection($connection)); } public function test_subscribe_to_multiple_public_channels() { $connection = $this->newActiveConnection(['channel-a', 'channel-b', 'channel-c']); $channelA = $this->channelManager->find('1234', 'channel-a'); $channelB = $this->channelManager->find('1234', 'channel-b'); $channelC = $this->channelManager->find('1234', 'channel-c'); $this->assertTrue($channelA->hasConnection($connection)); $this->assertTrue($channelB->hasConnection($connection)); $this->assertTrue($channelC->hasConnection($connection)); } public function test_unsubscribe_from_public_channel() { $connection = $this->newActiveConnection(['test-channel']); $channel = $this->channelManager->find('1234', 'test-channel'); $this->assertTrue($channel->hasConnection($connection)); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'data' => ['channel' => 'test-channel'], ])); $this->assertFalse($channel->hasConnection($connection)); } public function test_unsubscribe_does_not_affect_other_channels() { $connection = $this->newActiveConnection(['channel-a', 'channel-b']); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'data' => ['channel' => 'channel-a'], ])); $channelA = $this->channelManager->find('1234', 'channel-a'); $channelB = $this->channelManager->find('1234', 'channel-b'); $this->assertFalse($channelA->hasConnection($connection)); $this->assertTrue($channelB->hasConnection($connection)); } // ========================================================================= // Private channel subscribe/unsubscribe // ========================================================================= public function test_subscribe_to_private_channel_with_valid_signature() { $connection = $this->newPrivateConnection('private-test'); $connection->assertSentEvent('websocket_internal.subscription_succeeded'); $channel = $this->channelManager->find('1234', 'private-test'); $this->assertTrue($channel->hasConnection($connection)); } public function test_subscribe_to_private_channel_with_invalid_signature_is_rejected() { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $message = new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => [ 'auth' => 'invalid-signature', 'channel' => 'private-test', ], ]); $this->wsHandler->onMessage($connection, $message); // One frame: refused with an error, not on the channel, not tracked. $this->assertSubscriptionRefused($connection, 'private-test'); $this->assertNotContains('private-test', WebsocketService::getActiveChannels()); } public function test_subscribe_to_presence_channel_with_invalid_signature_is_rejected() { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => [ 'auth' => 'TestKey:invalid-signature', 'channel' => 'presence-test', 'channel_data' => json_encode(['user_id' => 1, 'user_info' => ['name' => 'Rick']]), ], ])); $this->assertSubscriptionRefused($connection, 'presence-test'); $this->assertNotContains('presence-test', WebsocketService::getActiveChannels()); } /** * @dataProvider authorizedChannels */ public function test_message_after_rejected_subscribe_does_not_resubscribe(string $channelName) { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['auth' => 'TestKey:invalid-signature', 'channel' => $channelName], ])); $connection->resetEvents(); // An unrelated message on the channel used to re-add the socket // through shouldRejectMessage()'s auto-resubscribe. $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'app.someAction', 'data' => ['test' => true], 'channel' => $channelName, ])); $error = collect($connection->sentData)->firstWhere('event', 'app.someAction:error'); $this->assertNotNull($error); $this->assertEquals('Subscription not established', $error['data']['message']); $channel = $this->channelManager->find('1234', $channelName); $this->assertFalse($channel->hasConnection($connection)); $this->assertArrayNotHasKey($connection->socketId, $this->handlerChannelConnections()[$channelName] ?? []); // And it does not receive what the channel broadcasts. $channel->broadcast('1234', (object) ['event' => 'secret.event', 'channel' => $channelName, 'data' => []], false); $connection->assertNotSentEvent('secret.event'); } public static function authorizedChannels(): array { return [ 'private' => ['private-test'], 'private-encrypted' => ['private-encrypted-test'], 'presence' => ['presence-test'], ]; } /** * @dataProvider authorizedChannels */ public function test_unsubscribe_as_first_frame_does_not_join_the_channel(string $channelName) { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); // The first non-ping frame runs the full auth step, which used to // saveConnection() the socket onto the frame's channel. $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'channel' => $channelName, ])); $this->assertNotOnChannel($connection, $channelName); $this->assertUnsignedSubscribeIsRefused($connection, $channelName); } /** * @dataProvider authorizedChannels */ public function test_unsubscribe_after_refused_subscribe_does_not_join_the_channel(string $channelName) { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); // The refused frame ends before the auth step, so the unsubscribe // after it is still the connection's first auth. $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['auth' => 'TestKey:invalid-signature', 'channel' => $channelName], ])); $this->assertSubscriptionRefused($connection, $channelName); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'data' => ['channel' => $channelName], ])); $this->assertNotOnChannel($connection, $channelName); $this->assertUnsignedSubscribeIsRefused($connection, $channelName); } /** * @dataProvider authorizedChannels */ public function test_unsubscribe_after_auth_reset_does_not_join_the_channel(string $channelName) { $connection = $this->newActiveConnection(['public-channel']); // C:RESET from a child (logout) clears authLoaded: the next frame // runs the full auth step again. unset($connection->authLoaded); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'channel' => $channelName, ])); $this->assertNotOnChannel($connection, $channelName); $this->assertTrue($this->channelManager->find('1234', 'public-channel')->hasConnection($connection)); } /** * @dataProvider authorizedChannels */ public function test_auth_override_that_saves_the_connection_does_not_join_the_channel(string $channelName) { // Consumer handlers override authenticateConnection() and call // $channel->saveConnection() themselves, some without a null check. $this->wsHandler = new class($this->channelManager) extends Handler { protected function authenticateConnection( ConnectionInterface $connection, PrivateChannel|Channel|PresenceChannel|null $channel, $message = [] ) { $channel->saveConnection($connection); parent::authenticateConnection($connection, $channel, $message); } }; $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'channel' => $channelName, ])); $response = collect($connection->sentData)->firstWhere('event', 'websocket.unsubscribe:response'); $this->assertEquals('Success', $response['data']['message'] ?? null); $this->assertNotOnChannel($connection, $channelName); $this->assertUnsignedSubscribeIsRefused($connection, $channelName); } public function test_unsubscribe_on_the_first_auth_frame_leaves_a_public_channel() { $connection = $this->newActiveConnection(['public-channel']); unset($connection->authLoaded); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'channel' => 'public-channel', ])); // The auth step used to put the socket straight back. $this->assertNotOnChannel($connection, 'public-channel'); } /** * @dataProvider authorizedChannels */ public function test_unsigned_subscribe_of_a_socket_saved_on_the_channel_is_refused(string $channelName) { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); // A hook (e.g. a consumer's authenticateConnection override) saved // the socket on the channel without a subscribe. $this->channelManager->findOrCreate('1234', $channelName)->saveConnection($connection); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['channel' => $channelName], ])); $this->assertSubscriptionRefused($connection, $channelName); } public function test_signed_subscribe_of_a_socket_saved_on_the_channel_is_tracked() { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $this->channelManager->findOrCreate('1234', 'private-test')->saveConnection($connection); $this->wsHandler->onMessage($connection, new Mocks\SignedMessage([ 'event' => 'websocket.subscribe', 'data' => ['channel' => 'private-test'], ], $connection, 'private-test')); $response = collect($connection->sentData)->firstWhere('event', 'websocket.subscribe:response'); $this->assertEquals('Success', $response['data']['message'] ?? null); $this->assertTrue($this->channelManager->find('1234', 'private-test')->hasConnection($connection)); $this->assertArrayHasKey($connection->socketId, $this->handlerChannelConnections()['private-test']); } public function test_repeated_unsigned_subscribe_of_a_member_stays_a_no_op() { $connection = $this->newPrivateConnection('private-test'); $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['channel' => 'private-test'], ])); $response = collect($connection->sentData)->firstWhere('event', 'websocket.subscribe:response'); $this->assertEquals('Success', $response['data']['message'] ?? null); $connection->assertNotSentEvent('websocket.subscribe:error'); $this->assertTrue($this->channelManager->find('1234', 'private-test')->hasConnection($connection)); } /** * The socket is not on the channel, not tracked by the handler, not listed * in the cache, and does not receive what the channel broadcasts. */ private function assertNotOnChannel(Mocks\Connection $connection, string $channelName): void { $channel = $this->channelManager->find('1234', $channelName); $this->assertFalse((bool) $channel?->hasConnection($connection), 'Socket must not be on the channel'); $this->assertArrayNotHasKey($connection->socketId, $this->handlerChannelConnections()[$channelName] ?? []); $this->flushHandlerCacheBuffer(); $this->assertNotContains($connection->socketId, WebsocketService::getChannelConnections($channelName)); $connection->resetEvents(); $channel?->broadcast('1234', (object) ['event' => 'secret.event', 'channel' => $channelName, 'data' => []], false); $connection->assertNotSentEvent('secret.event'); } /** * A subscribe without a signature is refused and leaves the socket off the * channel (it used to be laundered into a tracked subscription). */ private function assertUnsignedSubscribeIsRefused(Mocks\Connection $connection, string $channelName): void { $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['channel' => $channelName], ])); $this->assertSubscriptionRefused($connection, $channelName); $this->assertNotOnChannel($connection, $channelName); } public function test_valid_subscribe_after_rejected_signature_succeeds() { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['auth' => 'TestKey:invalid-signature', 'channel' => 'private-test'], ])); $this->assertSubscriptionRefused($connection, 'private-test'); $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\SignedMessage([ 'event' => 'websocket.subscribe', 'data' => ['channel' => 'private-test'], ], $connection, 'private-test')); $connection->assertSentEvent('websocket_internal.subscription_succeeded'); $response = collect($connection->sentData)->firstWhere('event', 'websocket.subscribe:response'); $this->assertEquals('Success', $response['data']['message'] ?? null); $this->assertTrue($this->channelManager->find('1234', 'private-test')->hasConnection($connection)); $this->flushHandlerCacheBuffer(); $this->assertContains($connection->socketId, WebsocketService::getChannelConnections('private-test')); } public function test_rejected_resubscribe_drops_the_earlier_tracking() { $connection = $this->newPrivateConnection('private-test'); $channel = $this->channelManager->find('1234', 'private-test'); $this->assertTrue($channel->hasConnection($connection)); // The socket drops off the channel (e.g. removeObsoleteConnections) // while the handler still tracks it. $channel->unsubscribe($connection); $this->assertArrayHasKey($connection->socketId, $this->handlerChannelConnections()['private-test']); $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['auth' => 'TestKey:invalid-signature', 'channel' => 'private-test'], ])); $this->assertSubscriptionRefused($connection, 'private-test'); // With the tracking gone, the next message cannot re-admit the socket. $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'app.someAction', 'data' => [], 'channel' => 'private-test', ])); $this->assertFalse($channel->hasConnection($connection)); $connection->assertSentEvent('app.someAction:error'); } // ========================================================================= // Presence channel subscribe/unsubscribe // ========================================================================= public function test_subscribe_to_presence_channel() { $connection = $this->newPresenceConnection('presence-chat', [ 'user_id' => 1, 'user_info' => ['name' => 'Rick'], ]); $connection->assertSentEvent('websocket_internal.subscription_succeeded'); $channel = $this->channelManager->find('1234', 'presence-chat'); $this->assertTrue($channel->hasConnection($connection)); } public function test_presence_channel_member_added_on_second_connection() { $rick = $this->newPresenceConnection('presence-chat', [ 'user_id' => 1, 'user_info' => ['name' => 'Rick'], ]); $rick->resetEvents(); $morty = $this->newPresenceConnection('presence-chat', [ 'user_id' => 2, 'user_info' => ['name' => 'Morty'], ]); // Rick should receive a member_added event $rick->assertSentEvent('websocket_internal.member_added'); } public function test_presence_channel_shows_member_count() { $rick = $this->newPresenceConnection('presence-chat', [ 'user_id' => 1, 'user_info' => ['name' => 'Rick'], ]); $morty = $this->newPresenceConnection('presence-chat', [ 'user_id' => 2, 'user_info' => ['name' => 'Morty'], ]); $channel = $this->channelManager->find('1234', 'presence-chat'); $this->assertTrue($channel->hasConnection($rick)); $this->assertTrue($channel->hasConnection($morty)); } // ========================================================================= // Ping/pong heartbeat // ========================================================================= public function test_ping_receives_pong() { $connection = $this->newActiveConnection(['websocket']); $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.ping', 'data' => new \stdClass(), ])); $connection->assertSentEvent('websocket.pong'); } public function test_backward_compat_colon_ping_receives_pong() { $connection = $this->newActiveConnection(['websocket']); $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'pusher:ping', 'data' => new \stdClass(), ])); $connection->assertSentEvent('websocket.pong'); } public function test_ping_does_not_require_channel_subscription() { $connection = $this->newConnection(); $this->wsHandler->onOpen($connection); $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.ping', 'data' => new \stdClass(), ])); $connection->assertSentEvent('websocket.pong'); } // ========================================================================= // Protocol :response suffix // ========================================================================= public function test_subscribe_gets_response_suffix() { $connection = $this->newActiveConnection(['websocket']); $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['channel' => 'websocket'], ])); $response = collect($connection->sentData)->firstWhere('event', 'websocket.subscribe:response'); $this->assertNotNull($response); $this->assertEquals('Success', $response['data']['message']); } public function test_unsubscribe_gets_response_suffix() { $connection = $this->newActiveConnection(['websocket']); $connection->resetEvents(); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'data' => ['channel' => 'websocket'], ])); $response = collect($connection->sentData)->firstWhere('event', 'websocket.unsubscribe:response'); $this->assertNotNull($response); } // ========================================================================= // Message rejection without subscription // ========================================================================= public function test_message_to_unsubscribed_channel_gets_error() { $connection = $this->newActiveConnection(['websocket']); // Unsubscribe first $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'data' => ['channel' => 'websocket'], ])); $connection->resetEvents(); // Try to send a message to the channel $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'app.someAction', 'data' => ['test' => true], 'channel' => 'websocket', ])); $error = collect($connection->sentData)->firstWhere('event', 'app.someAction:error'); $this->assertNotNull($error); $this->assertEquals('Subscription not established', $error['data']['message']); } // ========================================================================= // Connection close and error handling // ========================================================================= public function test_on_close_removes_connection_from_channels() { $connection = $this->newActiveConnection(['websocket']); $channel = $this->channelManager->find('1234', 'websocket'); $this->assertTrue($channel->hasConnection($connection)); $this->wsHandler->onClose($connection); $this->assertFalse($channel->hasConnection($connection)); } public function test_on_error_does_not_close_other_connections() { $conn1 = $this->newActiveConnection(['websocket']); $conn2 = $this->newActiveConnection(['websocket']); $this->wsHandler->onError($conn1, new \Exception('Test error')); // Generic exceptions are ignored; only ExceptionsWebSocketException is emitted. $conn1->assertNotSentEvent('websocket.error'); $conn2->assertNotSentEvent('websocket.error'); $channel = $this->channelManager->find('1234', 'websocket'); $this->assertTrue($channel->hasConnection($conn2)); } // ========================================================================= // Broadcasting between connections (via Channel class) // ========================================================================= public function test_broadcast_to_public_channel_reaches_all_connections() { $conn1 = $this->newActiveConnection(['news']); $conn2 = $this->newActiveConnection(['news']); $conn3 = $this->newActiveConnection(['news']); $conn1->resetEvents(); $conn2->resetEvents(); $conn3->resetEvents(); $channel = $this->channelManager->find('1234', 'news'); $channel->broadcast('1234', (object) [ 'event' => 'news.update', 'data' => ['title' => 'Breaking News'], 'channel' => 'news', ]); $conn1->assertSentEvent('news.update'); $conn2->assertSentEvent('news.update'); $conn3->assertSentEvent('news.update'); } public function test_broadcast_except_excludes_specified_socket() { $conn1 = $this->newActiveConnection(['chat']); $conn2 = $this->newActiveConnection(['chat']); $established = collect($conn1->sentData)->firstWhere('event', 'websocket.connection_established'); $excludeSocketId = json_decode($established['data'] ?? '{}', true)['socket_id'] ?? ''; $conn1->resetEvents(); $conn2->resetEvents(); $channel = $this->channelManager->find('1234', 'chat'); $channel->broadcastToEveryoneExcept( (object) ['event' => 'chat.message', 'data' => ['text' => 'Hello']], $excludeSocketId, '1234' ); // conn1 excluded, conn2 receives $conn1->assertNotSentEvent('chat.message'); $conn2->assertSentEvent('chat.message'); } // ========================================================================= // Connection count tracking // ========================================================================= public function test_channel_connection_count_tracks_subscribe_and_unsubscribe() { $conn1 = $this->newActiveConnection(['counter-channel']); $conn2 = $this->newActiveConnection(['counter-channel']); $channel = $this->channelManager->find('1234', 'counter-channel'); $this->assertTrue($channel->hasConnection($conn1)); $this->assertTrue($channel->hasConnection($conn2)); // Unsubscribe one $this->wsHandler->onMessage($conn1, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'data' => ['channel' => 'counter-channel'], ])); $this->assertFalse($channel->hasConnection($conn1)); $this->assertTrue($channel->hasConnection($conn2)); } // ========================================================================= // Message without app context // ========================================================================= public function test_message_without_on_open_is_ignored() { $connection = new Mocks\Connection(); $connection->httpRequest = new \GuzzleHttp\Psr7\Request('GET', '/?appKey=TestKey'); $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.ping', 'data' => new \stdClass(), ])); $this->assertEmpty($connection->sentData); } // ========================================================================= // Re-subscription after unsubscribe // ========================================================================= public function test_resubscribe_after_unsubscribe_works() { $connection = $this->newActiveConnection(['websocket']); $channel = $this->channelManager->find('1234', 'websocket'); // Unsubscribe $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.unsubscribe', 'data' => ['channel' => 'websocket'], ])); $this->assertFalse($channel->hasConnection($connection)); // Re-subscribe $this->wsHandler->onMessage($connection, new Mocks\Message([ 'event' => 'websocket.subscribe', 'data' => ['channel' => 'websocket'], ])); $this->assertTrue($channel->hasConnection($connection)); } }