More tests
This commit is contained in:
parent
8f4dfc0e1e
commit
9c2ac54322
|
|
@ -1,10 +1,86 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace BeyondCode\LaravelWebsockets\Tests\HttpApi;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelsController;
|
||||
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
|
||||
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
||||
use BeyondCode\LaravelWebSockets\Tests\TestCase;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class FetchChannelTest
|
||||
class FetchChannelTest extends TestCase
|
||||
{
|
||||
|
||||
/** @test */
|
||||
public function invalid_signatures_can_not_access_the_api()
|
||||
{
|
||||
$this->expectException(HttpException::class);
|
||||
$this->expectExceptionMessage('Invalid auth signature provided.');
|
||||
|
||||
$connection = new Connection();
|
||||
|
||||
$auth_key = 'TestKey';
|
||||
$auth_timestamp = time();
|
||||
$auth_version = '1.0';
|
||||
|
||||
$queryParameters = http_build_query(compact('auth_key','auth_timestamp','auth_version'));
|
||||
|
||||
$signature =
|
||||
"GET\n/apps/1234/channels\n" .
|
||||
"auth_key={$auth_key}" .
|
||||
"&auth_timestamp={$auth_timestamp}" .
|
||||
"&auth_version={$auth_version}";
|
||||
|
||||
$auth_signature = hash_hmac('sha256', $signature, 'InvalidSecret');
|
||||
|
||||
$request = new Request('GET', "/apps/1234/channels?appId=1234&auth_signature={$auth_signature}&{$queryParameters}");
|
||||
|
||||
$controller = app(FetchChannelsController::class);
|
||||
|
||||
$controller->onOpen($connection, $request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_the_channel_information()
|
||||
{
|
||||
$this->joinPresenceChannel('presence-channel');
|
||||
$this->joinPresenceChannel('presence-channel');
|
||||
$this->joinPresenceChannel('presence-channel');
|
||||
|
||||
$connection = new Connection();
|
||||
|
||||
$auth_key = 'TestKey';
|
||||
$auth_timestamp = time();
|
||||
$auth_version = '1.0';
|
||||
|
||||
$queryParameters = http_build_query(compact('auth_key','auth_timestamp','auth_version'));
|
||||
|
||||
$signature =
|
||||
"GET\n/apps/1234/channels\n" .
|
||||
"auth_key={$auth_key}" .
|
||||
"&auth_timestamp={$auth_timestamp}" .
|
||||
"&auth_version={$auth_version}";
|
||||
|
||||
$auth_signature = hash_hmac('sha256', $signature, 'TestSecret');
|
||||
|
||||
$request = new Request('GET', "/apps/1234/channels?appId=1234&auth_signature={$auth_signature}&{$queryParameters}");
|
||||
|
||||
$controller = app(FetchChannelsController::class);
|
||||
|
||||
$controller->onOpen($connection, $request);
|
||||
|
||||
/** @var JsonResponse $response */
|
||||
$response = array_pop($connection->sentRawData);
|
||||
|
||||
$this->assertSame([
|
||||
'channels' => [
|
||||
'presence-channel' => [
|
||||
'user_count' => 3
|
||||
]
|
||||
]
|
||||
], json_decode($response->getContent(), true));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -13,11 +13,14 @@ class Connection implements ConnectionInterface
|
|||
|
||||
public $sentData = [];
|
||||
|
||||
public $sentRawData = [];
|
||||
|
||||
public $closed = false;
|
||||
|
||||
public function send($data)
|
||||
{
|
||||
$this->sentData[] = json_decode($data, true);
|
||||
$this->sentRawData[] = $data;
|
||||
}
|
||||
|
||||
public function close()
|
||||
|
|
|
|||
|
|
@ -76,6 +76,35 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
return $connection;
|
||||
}
|
||||
|
||||
protected function joinPresenceChannel($channel): Connection
|
||||
{
|
||||
$connection = $this->getWebSocketConnection();
|
||||
|
||||
$this->pusherServer->onOpen($connection);
|
||||
|
||||
$channelData = [
|
||||
'user_id' => 1,
|
||||
'user_info' => [
|
||||
'name' => 'Marcel'
|
||||
]
|
||||
];
|
||||
|
||||
$signature = "{$connection->socketId}:{$channel}:".json_encode($channelData);
|
||||
|
||||
$message = new Message(json_encode([
|
||||
'event' => 'pusher:subscribe',
|
||||
'data' => [
|
||||
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
|
||||
'channel' => $channel,
|
||||
'channel_data' => json_encode($channelData)
|
||||
],
|
||||
]));
|
||||
|
||||
$this->pusherServer->onMessage($connection, $message);
|
||||
|
||||
return $connection;
|
||||
}
|
||||
|
||||
protected function getChannel(ConnectionInterface $connection, string $channelName)
|
||||
{
|
||||
return $this->channelManager->findOrCreate($connection->app->id, $channelName);
|
||||
|
|
|
|||
Loading…
Reference in New Issue