Skip to content

Commit 074fde9

Browse files
committed
add channels test
1 parent 8e01699 commit 074fde9

4 files changed

+47
-27
lines changed

tests/Feature/Ratchet/ChannelsControllerTest.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,28 @@
1010
$this->subscribe('test-channel-one');
1111
$this->subscribe('test-channel-two');
1212

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

1515
$this->assertSame(200, $response->getStatusCode());
1616
$this->assertSame('{"channels":{"test-channel-one":{"user_count":1},"test-channel-two":{"user_count":1}}}', $response->getBody()->getContents());
1717
});
18+
19+
it('can return filtered channels', function () {
20+
$this->subscribe('test-channel-one');
21+
$this->subscribe('test-channel-two');
22+
23+
$response = await($this->signedRequest('channels?filter_by_prefix=test-channel-t&info=user_count'));
24+
25+
$this->assertSame(200, $response->getStatusCode());
26+
$this->assertSame('{"channels":{"test-channel-two":{"user_count":1}}}', $response->getBody()->getContents());
27+
});
28+
29+
it('returns empty results if no metrics requested', function () {
30+
$this->subscribe('test-channel-one');
31+
$this->subscribe('test-channel-two');
32+
33+
$response = await($this->signedRequest('channels'));
34+
35+
$this->assertSame(200, $response->getStatusCode());
36+
$this->assertSame('{"channels":{"test-channel-one":[],"test-channel-two":[]}}', $response->getBody()->getContents());
37+
});

tests/Feature/Ratchet/EventsBatchControllerTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
uses(RatchetTestCase::class);
88

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

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

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

6464
it('can receive an event batch trigger with multiple events and return info for some', function () {
65-
$response = await($this->postToServerWithSignature('batch_events', [
65+
$response = await($this->signedPostRequest('batch_events', [
6666
[
6767
'name' => 'NewEvent',
6868
'channel' => 'test-channel',

tests/Feature/Ratchet/EventsControllerTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
uses(RatchetTestCase::class);
88

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

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

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

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

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

7777
$promiseOne = $this->messagePromise($connection);
78-
$response = await($this->postToServerWithSignature('events', [
78+
$response = await($this->signedPostRequest('events', [
7979
'name' => 'NewEvent',
8080
'channels' => ['test-channel-one', 'test-channel-two'],
8181
'data' => ['some' => 'data'],
8282
]));
8383

8484
$promiseTwo = $this->messagePromise($connection);
85-
$response = await($this->postToServerWithSignature('events', [
85+
$response = await($this->signedPostRequest('events', [
8686
'name' => 'NewEvent',
8787
'channels' => ['test-channel-one', 'test-channel-two'],
8888
'data' => ['some' => 'data'],

tests/RatchetTestCase.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -285,36 +285,36 @@ public function request(string $path, string $method = 'GET', mixed $data = '',
285285
);
286286
}
287287

288+
public function signedRequest(string $path, string $method = 'GET', mixed $data = '', string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface
289+
{
290+
$hash = md5(json_encode($data));
291+
$timestamp = time();
292+
$query = "auth_key=pusher-key&auth_timestamp={$timestamp}&auth_version=1.0&body_md5={$hash}";
293+
$string = "POST\n/apps/{$appId}/{$path}\n$query";
294+
$signature = hash_hmac('sha256', $string, 'pusher-secret');
295+
$path = Str::contains($path, '?') ? "{$path}&{$query}" : "{$path}?{$query}";
296+
297+
return $this->request("{$path}&auth_signature={$signature}", $method, $data, $host, $port, $appId);
298+
}
299+
288300
/**
289301
* Post a request to the server.
290302
*/
291-
public function postToServer(
292-
string $path,
293-
array $data = [],
294-
string $host = '0.0.0.0',
295-
string $port = '8080',
296-
string $appId = '123456'
297-
): PromiseInterface {
303+
public function postReqeust(string $path, array $data = [], string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface {
298304
return $this->request($path, 'POST', $data, $host, $port, $appId);
299305
}
300306

301307
/**
302308
* Post a signed request to the server.
303309
*/
304-
public function postToServerWithSignature(
305-
string $path,
306-
array $data = [],
307-
string $host = '0.0.0.0',
308-
string $port = '8080',
309-
string $appId = '123456'
310-
): PromiseInterface {
310+
public function signedPostRequest(string $path, array $data = [], string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface {
311311
$hash = md5(json_encode($data));
312312
$timestamp = time();
313313
$query = "auth_key=pusher-key&auth_timestamp={$timestamp}&auth_version=1.0&body_md5={$hash}";
314314
$string = "POST\n/apps/{$appId}/{$path}\n$query";
315315
$signature = hash_hmac('sha256', $string, 'pusher-secret');
316316

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

320320
public function getWithSignature(

0 commit comments

Comments
 (0)