Skip to content

Commit

Permalink
add channels test
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 17, 2023
1 parent 8e01699 commit 074fde9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
22 changes: 21 additions & 1 deletion tests/Feature/Ratchet/ChannelsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,28 @@
$this->subscribe('test-channel-one');
$this->subscribe('test-channel-two');

$response = await($this->getWithSignature('channels?info=user_count'));
$response = await($this->signedRequest('channels?info=user_count'));

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('{"channels":{"test-channel-one":{"user_count":1},"test-channel-two":{"user_count":1}}}', $response->getBody()->getContents());
});

it('can return filtered channels', function () {
$this->subscribe('test-channel-one');
$this->subscribe('test-channel-two');

$response = await($this->signedRequest('channels?filter_by_prefix=test-channel-t&info=user_count'));

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('{"channels":{"test-channel-two":{"user_count":1}}}', $response->getBody()->getContents());
});

it('returns empty results if no metrics requested', function () {
$this->subscribe('test-channel-one');
$this->subscribe('test-channel-two');

$response = await($this->signedRequest('channels'));

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('{"channels":{"test-channel-one":[],"test-channel-two":[]}}', $response->getBody()->getContents());
});
8 changes: 4 additions & 4 deletions tests/Feature/Ratchet/EventsBatchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
uses(RatchetTestCase::class);

it('can receive an event batch trigger', function () {
$response = await($this->postToServerWithSignature('batch_events', [[
$response = await($this->signedPostRequest('batch_events', [[
'name' => 'NewEvent',
'channel' => 'test-channel',
'data' => ['some' => 'data'],
Expand All @@ -18,7 +18,7 @@
});

it('can receive an event batch trigger with multiple events', function () {
$response = await($this->postToServerWithSignature('batch_events', [
$response = await($this->signedPostRequest('batch_events', [
[
'name' => 'NewEvent',
'channel' => 'test-channel',
Expand All @@ -36,7 +36,7 @@
});

it('can receive an event batch trigger with multiple events and return info for each', function () {
$response = await($this->postToServerWithSignature('batch_events', [
$response = await($this->signedPostRequest('batch_events', [
[
'name' => 'NewEvent',
'channel' => 'test-channel',
Expand All @@ -62,7 +62,7 @@
});

it('can receive an event batch trigger with multiple events and return info for some', function () {
$response = await($this->postToServerWithSignature('batch_events', [
$response = await($this->signedPostRequest('batch_events', [
[
'name' => 'NewEvent',
'channel' => 'test-channel',
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/Ratchet/EventsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
uses(RatchetTestCase::class);

it('can receive and event trigger', function () {
$response = await($this->postToServerWithSignature('events', [
$response = await($this->signedPostRequest('events', [
'name' => 'NewEvent',
'channel' => 'test-channel',
'data' => ['some' => 'data'],
Expand All @@ -18,7 +18,7 @@
});

it('can receive and event trigger for multiple channels', function () {
$response = await($this->postToServerWithSignature('events', [
$response = await($this->signedPostRequest('events', [
'name' => 'NewEvent',
'channels' => ['test-channel-one', 'test-channel-two'],
'data' => ['some' => 'data'],
Expand All @@ -31,7 +31,7 @@
it('can return user counts when requested', function () {
$this->subscribe('test-channel-one');

$response = await($this->postToServerWithSignature('events', [
$response = await($this->signedPostRequest('events', [
'name' => 'NewEvent',
'channels' => ['test-channel-one', 'test-channel-two'],
'data' => ['some' => 'data'],
Expand All @@ -45,7 +45,7 @@
it('can return subscription counts when requested', function () {
$this->subscribe('test-channel-two');

$response = await($this->postToServerWithSignature('events', [
$response = await($this->signedPostRequest('events', [
'name' => 'NewEvent',
'channels' => ['test-channel-one', 'test-channel-two'],
'data' => ['some' => 'data'],
Expand All @@ -59,7 +59,7 @@
it('can return user and subscription counts when requested', function () {
$this->subscribe('test-channel-two');

$response = await($this->postToServerWithSignature('events', [
$response = await($this->signedPostRequest('events', [
'name' => 'NewEvent',
'channels' => ['test-channel-one', 'test-channel-two'],
'data' => ['some' => 'data'],
Expand All @@ -75,14 +75,14 @@
$this->subscribe('test-channel-two', connection: $connection);

$promiseOne = $this->messagePromise($connection);
$response = await($this->postToServerWithSignature('events', [
$response = await($this->signedPostRequest('events', [
'name' => 'NewEvent',
'channels' => ['test-channel-one', 'test-channel-two'],
'data' => ['some' => 'data'],
]));

$promiseTwo = $this->messagePromise($connection);
$response = await($this->postToServerWithSignature('events', [
$response = await($this->signedPostRequest('events', [
'name' => 'NewEvent',
'channels' => ['test-channel-one', 'test-channel-two'],
'data' => ['some' => 'data'],
Expand Down
30 changes: 15 additions & 15 deletions tests/RatchetTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,36 +285,36 @@ public function request(string $path, string $method = 'GET', mixed $data = '',
);
}

public function signedRequest(string $path, string $method = 'GET', mixed $data = '', string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface
{
$hash = md5(json_encode($data));
$timestamp = time();
$query = "auth_key=pusher-key&auth_timestamp={$timestamp}&auth_version=1.0&body_md5={$hash}";
$string = "POST\n/apps/{$appId}/{$path}\n$query";
$signature = hash_hmac('sha256', $string, 'pusher-secret');
$path = Str::contains($path, '?') ? "{$path}&{$query}" : "{$path}?{$query}";

return $this->request("{$path}&auth_signature={$signature}", $method, $data, $host, $port, $appId);
}

/**
* Post a request to the server.
*/
public function postToServer(
string $path,
array $data = [],
string $host = '0.0.0.0',
string $port = '8080',
string $appId = '123456'
): PromiseInterface {
public function postReqeust(string $path, array $data = [], string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface {
return $this->request($path, 'POST', $data, $host, $port, $appId);
}

/**
* Post a signed request to the server.
*/
public function postToServerWithSignature(
string $path,
array $data = [],
string $host = '0.0.0.0',
string $port = '8080',
string $appId = '123456'
): PromiseInterface {
public function signedPostRequest(string $path, array $data = [], string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface {
$hash = md5(json_encode($data));
$timestamp = time();
$query = "auth_key=pusher-key&auth_timestamp={$timestamp}&auth_version=1.0&body_md5={$hash}";
$string = "POST\n/apps/{$appId}/{$path}\n$query";
$signature = hash_hmac('sha256', $string, 'pusher-secret');

return $this->postToServer("{$path}?{$query}&auth_signature={$signature}", $data, $host, $port, $appId);
return $this->postReqeust("{$path}?{$query}&auth_signature={$signature}", $data, $host, $port, $appId);
}

public function getWithSignature(
Expand Down

0 comments on commit 074fde9

Please sign in to comment.