2018-12-03 09:26:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Test;
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\API\FetchChannel;
|
2018-12-03 09:26:17 +00:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
|
|
|
|
|
|
class FetchChannelTest extends TestCase
|
|
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_invalid_signatures_can_not_access_the_api()
|
2018-12-03 09:26:17 +00:00
|
|
|
{
|
|
|
|
|
$this->expectException(HttpException::class);
|
|
|
|
|
$this->expectExceptionMessage('Invalid auth signature provided.');
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = new Mocks\Connection;
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channel/my-channel';
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'channelName' => 'my-channel',
|
|
|
|
|
];
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2021-12-17 15:42:26 +00:00
|
|
|
$queryString = self::build_auth_query_string(
|
2020-09-10 19:59:26 +00:00
|
|
|
'TestKey', 'InvalidSecret', 'GET', $requestPath
|
|
|
|
|
);
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$controller = app(FetchChannel::class);
|
2018-12-03 09:26:17 +00:00
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_it_returns_the_channel_information()
|
2018-12-03 09:26:17 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->newActiveConnection(['my-channel']);
|
|
|
|
|
$this->newActiveConnection(['my-channel']);
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = new Mocks\Connection;
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channel/my-channel';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'channelName' => 'my-channel',
|
|
|
|
|
];
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2021-12-17 15:42:26 +00:00
|
|
|
$queryString = self::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$controller = app(FetchChannel::class);
|
2018-12-03 09:26:17 +00:00
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->assertSame([
|
|
|
|
|
'occupied' => true,
|
2018-12-04 21:22:33 +00:00
|
|
|
'subscription_count' => 2,
|
2018-12-03 09:26:17 +00:00
|
|
|
], json_decode($response->getContent(), true));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_it_returns_presence_channel_information()
|
2019-04-05 19:30:41 +00:00
|
|
|
{
|
2020-09-17 11:44:52 +00:00
|
|
|
$this->newPresenceConnection('presence-channel', ['user_id' => 1]);
|
|
|
|
|
$this->newPresenceConnection('presence-channel', ['user_id' => 2]);
|
2019-04-05 19:30:41 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = new Mocks\Connection;
|
2019-04-05 19:30:41 +00:00
|
|
|
|
|
|
|
|
$requestPath = '/apps/1234/channel/my-channel';
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2019-04-05 19:30:41 +00:00
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'channelName' => 'presence-channel',
|
|
|
|
|
];
|
|
|
|
|
|
2021-12-17 15:42:26 +00:00
|
|
|
$queryString = self::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
2019-04-05 19:30:41 +00:00
|
|
|
|
|
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$controller = app(FetchChannel::class);
|
2019-04-05 19:30:41 +00:00
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->assertSame([
|
|
|
|
|
'occupied' => true,
|
|
|
|
|
'subscription_count' => 2,
|
|
|
|
|
'user_count' => 2,
|
|
|
|
|
], json_decode($response->getContent(), true));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_it_returns_404_for_invalid_channels()
|
2018-12-03 09:26:17 +00:00
|
|
|
{
|
2022-01-06 11:31:21 +00:00
|
|
|
$this->skipOnRedisReplication();
|
|
|
|
|
|
2018-12-03 09:26:17 +00:00
|
|
|
$this->expectException(HttpException::class);
|
|
|
|
|
$this->expectExceptionMessage('Unknown channel');
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->newActiveConnection(['my-channel']);
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = new Mocks\Connection;
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channel/invalid-channel';
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'channelName' => 'invalid-channel',
|
|
|
|
|
];
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2021-12-17 15:42:26 +00:00
|
|
|
$queryString = self::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:26:17 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$controller = app(FetchChannel::class);
|
2018-12-03 09:26:17 +00:00
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|