From 0379e163301a3521cd033a28e770eae9a55b06be Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Mon, 3 Dec 2018 10:26:17 +0100 Subject: [PATCH] More tests --- tests/HttpApi/FetchChannelTest.php | 120 +++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/HttpApi/FetchChannelTest.php diff --git a/tests/HttpApi/FetchChannelTest.php b/tests/HttpApi/FetchChannelTest.php new file mode 100644 index 0000000..34643bd --- /dev/null +++ b/tests/HttpApi/FetchChannelTest.php @@ -0,0 +1,120 @@ +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/channel/my-channel?appId=1234&channelName=my-channel&auth_signature={$auth_signature}&{$queryParameters}"); + + $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(); + + $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/channel/my-channel\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/channel/my-channel?appId=1234&channelName=my-channel&auth_signature={$auth_signature}&{$queryParameters}"); + + $controller = app(FetchChannelController::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([ + 'occupied' => true, + 'subscription_count' => 2 + ], json_decode($response->getContent(), true)); + } + + /** @test */ + public function it_returns_404_for_invalid_channels() + { + $this->expectException(HttpException::class); + $this->expectExceptionMessage('Unknown channel'); + + $this->getConnectedWebSocketConnection(['my-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/channel/my-channel\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/channel/my-channel?appId=1234&channelName=invalid-channel&auth_signature={$auth_signature}&{$queryParameters}"); + + $controller = app(FetchChannelController::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([ + 'occupied' => true, + 'subscription_count' => 2 + ], json_decode($response->getContent(), true)); + } + +} \ No newline at end of file