From f0cee423dce0ed8c8d65dbfdfc531b188627a50d Mon Sep 17 00:00:00 2001 From: kirills-morozovs <94073530+kirills-morozovs@users.noreply.github.com> Date: Thu, 9 May 2024 20:23:01 +0300 Subject: [PATCH] Add health check endpoint (laravel#190) (#191) --- .../Controllers/HealthCheckController.php | 18 +++++++++++++++++ src/Servers/Reverb/Factory.php | 2 ++ .../Reverb/HealthCheckControllerTest.php | 20 +++++++++++++++++++ tests/ReverbTestCase.php | 14 +++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 src/Protocols/Pusher/Http/Controllers/HealthCheckController.php create mode 100644 tests/Feature/Protocols/Pusher/Reverb/HealthCheckControllerTest.php diff --git a/src/Protocols/Pusher/Http/Controllers/HealthCheckController.php b/src/Protocols/Pusher/Http/Controllers/HealthCheckController.php new file mode 100644 index 00000000..ed7fd2af --- /dev/null +++ b/src/Protocols/Pusher/Http/Controllers/HealthCheckController.php @@ -0,0 +1,18 @@ + 'OK']); + } +} diff --git a/src/Servers/Reverb/Factory.php b/src/Servers/Reverb/Factory.php index 5d9a7c9b..6dd4c726 100644 --- a/src/Servers/Reverb/Factory.php +++ b/src/Servers/Reverb/Factory.php @@ -13,6 +13,7 @@ use Laravel\Reverb\Protocols\Pusher\Http\Controllers\ConnectionsController; use Laravel\Reverb\Protocols\Pusher\Http\Controllers\EventsBatchController; use Laravel\Reverb\Protocols\Pusher\Http\Controllers\EventsController; +use Laravel\Reverb\Protocols\Pusher\Http\Controllers\HealthCheckController; use Laravel\Reverb\Protocols\Pusher\Http\Controllers\PusherController; use Laravel\Reverb\Protocols\Pusher\Http\Controllers\UsersTerminateController; use Laravel\Reverb\Protocols\Pusher\Managers\ArrayChannelConnectionManager; @@ -101,6 +102,7 @@ protected static function pusherRoutes(): RouteCollection $routes->add('channel', Route::get('/apps/{appId}/channels/{channel}', new ChannelController)); $routes->add('channel_users', Route::get('/apps/{appId}/channels/{channel}/users', new ChannelUsersController)); $routes->add('users_terminate', Route::post('/apps/{appId}/users/{userId}/terminate_connections', new UsersTerminateController)); + $routes->add('health_check', Route::get('/up', new HealthCheckController)); return $routes; } diff --git a/tests/Feature/Protocols/Pusher/Reverb/HealthCheckControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/HealthCheckControllerTest.php new file mode 100644 index 00000000..693e33f7 --- /dev/null +++ b/tests/Feature/Protocols/Pusher/Reverb/HealthCheckControllerTest.php @@ -0,0 +1,20 @@ +stopServer(); + await($this->requestWithoutAppId('up')); +})->throws(RuntimeException::class); + +it('can respond to a health check request', function () { + $response = await($this->requestWithoutAppId('up')); + + expect($response->getStatusCode())->toBe(200); + expect($response->getBody()->getContents())->toBe('{"health":"OK"}'); + expect($response->getHeader('Content-Length'))->toBe(['15']); +}); diff --git a/tests/ReverbTestCase.php b/tests/ReverbTestCase.php index 3031e87c..93cdd9e3 100644 --- a/tests/ReverbTestCase.php +++ b/tests/ReverbTestCase.php @@ -141,6 +141,20 @@ public function request(string $path, string $method = 'GET', mixed $data = '', ); } + /** + * Send a request to the server without specifying app ID. + */ + public function requestWithoutAppId(string $path, string $method = 'GET', mixed $data = '', string $host = '0.0.0.0', string $port = '8080'): PromiseInterface + { + return (new Browser($this->loop)) + ->request( + $method, + "http://{$host}:{$port}/{$path}", + [], + ($data) ? json_encode($data) : '' + ); + } + /** * Send a signed request to the server. */