laravel-websockets/tests/HttpApi/FetchChannelTest.php

135 lines
4.3 KiB
PHP
Raw Normal View History

2018-12-03 09:26:17 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Tests\HttpApi;
2020-03-04 09:58:39 +00:00
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelController;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
2018-12-03 09:26:17 +00:00
use GuzzleHttp\Psr7\Request;
use Illuminate\Http\JsonResponse;
2020-03-04 09:58:39 +00:00
use Pusher\Pusher;
2018-12-03 09:26:17 +00:00
use Symfony\Component\HttpKernel\Exception\HttpException;
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();
$requestPath = '/apps/1234/channel/my-channel';
$routeParams = [
'appId' => '1234',
'channelName' => 'my-channel',
];
2018-12-03 09:26:17 +00:00
$queryString = Pusher::build_auth_query_string('TestKey', 'InvalidSecret', 'GET', $requestPath);
2018-12-03 09:26:17 +00:00
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
2018-12-03 09:26:17 +00:00
$controller = app(FetchChannelController::class);
$controller->onOpen($connection, $request);
}
/** @test */
public function it_returns_the_channel_information()
{
$this->getConnectedWebSocketConnection(['my-channel']);
$this->getConnectedWebSocketConnection(['my-channel']);
$connection = new Connection();
$requestPath = '/apps/1234/channel/my-channel';
$routeParams = [
'appId' => '1234',
'channelName' => 'my-channel',
];
2018-12-03 09:26:17 +00:00
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
2018-12-03 09:26:17 +00:00
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
2018-12-03 09:26:17 +00:00
$controller = app(FetchChannelController::class);
$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));
}
/** @test */
public function it_returns_the_channel_information_for_presence_channel()
{
$this->joinPresenceChannel('presence-global', 'user:1');
$this->joinPresenceChannel('presence-global', 'user:2');
$this->joinPresenceChannel('presence-global', 'user:2');
$connection = new Connection();
$requestPath = '/apps/1234/channel/presence-global';
$routeParams = [
'appId' => '1234',
'channelName' => 'presence-global',
];
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(FetchChannelController::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([
'occupied' => true,
'subscription_count' => 3,
'user_count' => 2,
], json_decode($response->getContent(), true));
}
2018-12-03 09:26:17 +00:00
/** @test */
public function it_returns_404_for_invalid_channels()
{
$this->expectException(HttpException::class);
$this->expectExceptionMessage('Unknown channel');
$this->getConnectedWebSocketConnection(['my-channel']);
$connection = new Connection();
$requestPath = '/apps/1234/channel/invalid-channel';
$routeParams = [
'appId' => '1234',
'channelName' => 'invalid-channel',
];
2018-12-03 09:26:17 +00:00
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
2018-12-03 09:26:17 +00:00
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
2018-12-03 09:26:17 +00:00
$controller = app(FetchChannelController::class);
$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));
}
2018-12-04 21:22:33 +00:00
}