Skip to content

Commit

Permalink
Added missing test to make sure publish payload doesn't have socket key
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 24, 2024
1 parent 3b271cf commit 43a6e89
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public static function isJwtValid($jwt, $timeFn, $key)
return $isSignatureValid && ! $isTokenExpired;
}

/**
* https://www.php.net/manual/en/function.base64-encode.php#127544
*/
public static function base64url_encode($str): string
{
return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
Expand Down
25 changes: 22 additions & 3 deletions tests/AblyBroadcasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,33 @@ public function testLaravelAblyAgentHeader()
$this->assertcontains( 'Ably-Agent: '.$expectedLaravelHeader, $ably->http->lastHeaders, 'Expected Laravel broadcaster header in HTTP request '.json_encode($ably->http->lastHeaders));
}

public function testPayloadShouldNotIncludeSocketKey()
public function testPublishPayloadShouldNotIncludeSocketKey()
{
$ably = (new AblyFactory())->make([
'key' => 'abcd:efgh',
'httpClass' => 'Ably\LaravelBroadcaster\Tests\HttpMock',
]);
$broadcaster = m::mock(AblyBroadcasterExposed::class, [$ably, []])->makePartial();

// self::assertArrayNotHasKey('socket', $message->data);
$socketIdObject = new \stdClass();
$socketIdObject->connectionKey = 'foo';
$socketIdObject->clientId = 'sacOO7';
$payload = [
'foo' => 'bar',
'socket' => Utils::base64url_encode(json_encode($socketIdObject))
];

$broadcaster->broadcast(["channel1", "channel2"], 'testEvent', $payload);

self::assertCount(2, $broadcaster->payloads);
foreach ($broadcaster->payloads as $payload) {
self::assertArrayNotHasKey('socket', $payload);
}
}

public function testBuildMessageBasedOnSocketIdObject()
{
$broadcaster = m::mock(AblyBroadcasterExposed::class, [$this->ably, []])->makePartial();

$payload = [
'foo' => 'bar',
'chat' => 'hello there'
Expand All @@ -354,8 +371,10 @@ public function testBuildMessageBasedOnSocketIdObject()

class AblyBroadcasterExposed extends AblyBroadcaster
{
public $payloads = [];
public function buildAblyMessage($event, $payload = [], $socketIdObject = null)
{
$this->payloads[] = $payload;
return parent::buildAblyMessage($event, $payload, $socketIdObject);
}
}
Expand Down

0 comments on commit 43a6e89

Please sign in to comment.