From b3845eb53d95d18df9f2b3c15b67b06362cf7a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Jab=C5=82o=C5=84ski?= Date: Fri, 29 Mar 2024 18:25:50 +0100 Subject: [PATCH 1/3] Fix missing data property for pusher internal events with empty data --- src/Protocols/Pusher/EventHandler.php | 2 +- tests/Unit/Protocols/Pusher/EventTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Protocols/Pusher/EventHandler.php b/src/Protocols/Pusher/EventHandler.php index 594b8ba5..89d1b09f 100644 --- a/src/Protocols/Pusher/EventHandler.php +++ b/src/Protocols/Pusher/EventHandler.php @@ -150,7 +150,7 @@ public function formatPayload(string $event, array $data = [], ?string $channel return json_encode( array_filter([ 'event' => $prefix.$event, - 'data' => empty($data) ? null : json_encode($data), + 'data' => empty($data) ? '{}' : json_encode($data), 'channel' => $channel, ]) ); diff --git a/tests/Unit/Protocols/Pusher/EventTest.php b/tests/Unit/Protocols/Pusher/EventTest.php index fe629c6e..bd8f0a34 100644 --- a/tests/Unit/Protocols/Pusher/EventTest.php +++ b/tests/Unit/Protocols/Pusher/EventTest.php @@ -33,6 +33,7 @@ $this->connection->assertReceived([ 'event' => 'pusher_internal:subscription_succeeded', + 'data' => '{}', 'channel' => 'test-channel', ]); }); From 520163c71821ff1d6a239f4e57b4c15b075f0b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Jab=C5=82o=C5=84ski?= Date: Fri, 29 Mar 2024 19:10:01 +0100 Subject: [PATCH 2/3] Provide dedicated implementation of internal payload formatter --- src/Protocols/Pusher/EventHandler.php | 14 ++++++++++---- .../Protocols/Pusher/Reverb/ServerTest.php | 16 ++++++++-------- tests/Unit/Protocols/Pusher/EventTest.php | 5 ++--- tests/Unit/Protocols/Pusher/ServerTest.php | 5 +++++ 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Protocols/Pusher/EventHandler.php b/src/Protocols/Pusher/EventHandler.php index 89d1b09f..79424a6b 100644 --- a/src/Protocols/Pusher/EventHandler.php +++ b/src/Protocols/Pusher/EventHandler.php @@ -145,12 +145,12 @@ public function sendInternally(Connection $connection, string $event, array $dat /** * Format the payload for the given event. */ - public function formatPayload(string $event, array $data = [], ?string $channel = null, string $prefix = 'pusher:'): string|false + public function formatPayload(string $event, array $data = [], ?string $channel = null): string|false { return json_encode( array_filter([ - 'event' => $prefix.$event, - 'data' => empty($data) ? '{}' : json_encode($data), + 'event' => 'pusher:'.$event, + 'data' => empty($data) ? null : json_encode($data), 'channel' => $channel, ]) ); @@ -161,6 +161,12 @@ public function formatPayload(string $event, array $data = [], ?string $channel */ public function formatInternalPayload(string $event, array $data = [], $channel = null): string|false { - return static::formatPayload($event, $data, $channel, 'pusher_internal:'); + return json_encode( + array_filter([ + 'event' => 'pusher_internal:'.$event, + 'data' => empty($data) ? '{}' : json_encode($data), + 'channel' => $channel, + ]) + ); } } diff --git a/tests/Feature/Protocols/Pusher/Reverb/ServerTest.php b/tests/Feature/Protocols/Pusher/Reverb/ServerTest.php index f9f480f6..f449e635 100644 --- a/tests/Feature/Protocols/Pusher/Reverb/ServerTest.php +++ b/tests/Feature/Protocols/Pusher/Reverb/ServerTest.php @@ -21,13 +21,13 @@ $response = subscribe('test-channel'); expect(channels()->find('test-channel')->connections())->toHaveCount(1); - expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"test-channel"}'); + expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"test-channel"}'); }); it('can subscribe to a private channel', function () { $response = subscribe('private-test-channel'); - expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"private-test-channel"}'); + expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"private-test-channel"}'); }); it('can subscribe to a presence channel', function () { @@ -41,13 +41,13 @@ it('can subscribe to a cache channel', function () { $response = subscribe('cache-test-channel'); - expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"cache-test-channel"}'); + expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"cache-test-channel"}'); }); it('can subscribe to a private cache channel', function () { $response = subscribe('private-cache-test-channel'); - expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"private-cache-test-channel"}'); + expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"private-cache-test-channel"}'); }); it('can subscribe to a presence cache channel', function () { @@ -141,21 +141,21 @@ $connection->assertReceived('{"event":"App\\\\Events\\\\TestEvent","data":"{\"foo\":\"bar\"}","channel":"presence-cache-test-channel"}'); }); -it('can receive a cach missed message when joining a cache channel with an empty cache', function () { +it('can receive a cache missed message when joining a cache channel with an empty cache', function () { $connection = connect(); subscribe('cache-test-channel', connection: $connection); $connection->assertReceived('{"event":"pusher:cache_miss","channel":"cache-test-channel"}'); }); -it('can receive a cach missed message when joining a private cache channel with an empty cache', function () { +it('can receive a cache missed message when joining a private cache channel with an empty cache', function () { $connection = connect(); subscribe('private-cache-test-channel', connection: $connection); $connection->assertReceived('{"event":"pusher:cache_miss","channel":"private-cache-test-channel"}'); }); -it('can receive a cach missed message when joining a presence cache channel with an empty cache', function () { +it('can receive a cache missed message when joining a presence cache channel with an empty cache', function () { $connection = connect(); subscribe('presence-cache-test-channel', connection: $connection); @@ -455,7 +455,7 @@ ], ], $connection); - expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"my-channel"}'); + expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"my-channel"}'); }); it('buffers large requests correctly', function () { diff --git a/tests/Unit/Protocols/Pusher/EventTest.php b/tests/Unit/Protocols/Pusher/EventTest.php index bd8f0a34..4d86d7d0 100644 --- a/tests/Unit/Protocols/Pusher/EventTest.php +++ b/tests/Unit/Protocols/Pusher/EventTest.php @@ -64,11 +64,10 @@ 'foo', ['bar' => 'baz'], 'test-channel', - 'reverb:' ); expect($payload)->toBe(json_encode([ - 'event' => 'reverb:foo', + 'event' => 'pusher:foo', 'data' => json_encode(['bar' => 'baz']), 'channel' => 'test-channel', ])); @@ -85,7 +84,6 @@ 'foo', ['bar' => 'baz'], 'test-channel', - 'reverb:' ); expect($payload)->toBe(json_encode([ @@ -98,5 +96,6 @@ expect($payload)->toBe(json_encode([ 'event' => 'pusher_internal:foo', + 'data' => '{}', ])); }); diff --git a/tests/Unit/Protocols/Pusher/ServerTest.php b/tests/Unit/Protocols/Pusher/ServerTest.php index 059de801..83f2eb3e 100644 --- a/tests/Unit/Protocols/Pusher/ServerTest.php +++ b/tests/Unit/Protocols/Pusher/ServerTest.php @@ -56,6 +56,7 @@ $connection->assertReceived([ 'event' => 'pusher_internal:subscription_succeeded', + 'data' => '{}', 'channel' => 'test-channel', ]); }); @@ -108,6 +109,7 @@ $connection->assertReceived([ 'event' => 'pusher_internal:subscription_succeeded', + 'data' => '{}', 'channel' => 'test-channel', ]); }); @@ -125,6 +127,7 @@ $connection->assertReceived([ 'event' => 'pusher_internal:subscription_succeeded', + 'data' => '{}', 'channel' => 'private-test-channel', ]); }); @@ -165,6 +168,7 @@ $connection->assertReceived([ 'event' => 'pusher_internal:subscription_succeeded', + 'data' => '{}', 'channel' => 'cache-test-channel', ]); $connection->assertReceived([ @@ -199,6 +203,7 @@ $connection->assertReceived([ 'event' => 'pusher_internal:subscription_succeeded', + 'data' => '{}', 'channel' => 'cache-test-channel', ]); $connection->assertReceived(['foo' => 'bar']); From a7a7e5c45e5e0128dbb8d027bc621326ef0bab99 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 4 Apr 2024 12:37:17 +0100 Subject: [PATCH 3/3] wip --- src/Protocols/Pusher/EventHandler.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Protocols/Pusher/EventHandler.php b/src/Protocols/Pusher/EventHandler.php index 79424a6b..5360fa04 100644 --- a/src/Protocols/Pusher/EventHandler.php +++ b/src/Protocols/Pusher/EventHandler.php @@ -145,11 +145,11 @@ public function sendInternally(Connection $connection, string $event, array $dat /** * Format the payload for the given event. */ - public function formatPayload(string $event, array $data = [], ?string $channel = null): string|false + public function formatPayload(string $event, array $data = [], ?string $channel = null, string $prefix = 'pusher:'): string|false { return json_encode( array_filter([ - 'event' => 'pusher:'.$event, + 'event' => $prefix.$event, 'data' => empty($data) ? null : json_encode($data), 'channel' => $channel, ]) @@ -164,7 +164,7 @@ public function formatInternalPayload(string $event, array $data = [], $channel return json_encode( array_filter([ 'event' => 'pusher_internal:'.$event, - 'data' => empty($data) ? '{}' : json_encode($data), + 'data' => json_encode((object) $data), 'channel' => $channel, ]) );