Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Backwards Incompatible Config Additions #57

Merged
merged 4 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/HealthCheckServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function boot()
{
$this->configure();

$this->app->make('router')->get($this->withBasePath(config('healthcheck.route-paths.health')), [
$this->app->make('router')->get($this->withBasePath(config('healthcheck.route-paths.health', '/health')), [
'middleware' => config('healthcheck.middleware'),
'uses' => HealthCheckController::class,
'as' => config('healthcheck.route-name')
Expand All @@ -36,13 +36,14 @@ public function boot()
]);
}

$this->app->make('router')->get($this->withBasePath(config('healthcheck.route-paths.ping')), PingController::class);
$this->app->make('router')->get($this->withBasePath(config('healthcheck.route-paths.ping', '/ping')), PingController::class);
}

protected function configure()
{
$this->mergeConfigFrom(__DIR__.'/../config/healthcheck.php', 'healthcheck');
$configPath = $this->app->basePath().'/config/healthcheck.php';

$this->publishes([
__DIR__.'/../config/healthcheck.php' => $configPath,
], 'config');
Expand Down
92 changes: 91 additions & 1 deletion tests/Controllers/HealthCheckControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Controllers;

use Illuminate\Http\Response;
use Illuminate\Routing\RouteCollection;
use Tests\TestCase;
use UKFast\HealthCheck\Controllers\HealthCheckController;
use UKFast\HealthCheck\HealthCheck;
Expand Down Expand Up @@ -32,8 +33,45 @@ public function returns_overall_status_of_okay_when_everything_is_up()
/**
* @test
*/
public function overrides_default_path()
public function overrides_default_ping_path()
{
app('router')->setRoutes(app(RouteCollection::class));

$response = $this->get('/ping');
$response->assertStatus(404);

$response = $this->get('/pingz');
$response->assertStatus(404);

config([
'healthcheck.route-paths.ping' => '/pingz',
]);

// Manually re-boot the service provider to override the path
$this->app->getProvider(HealthCheckServiceProvider::class)->boot();

$this->setChecks([AlwaysUpCheck::class]);

$response = $this->get('/pingz');
$this->assertSame('pong', $response->getContent());

$response = $this->get('/ping');
$response->assertStatus(404);
}

/**
* @test
*/
public function overrides_default_health_path()
{
app('router')->setRoutes(app(RouteCollection::class));

$response = $this->get('/health');
$response->assertStatus(404);

$response = $this->get('/healthz');
$response->assertStatus(404);

config([
'healthcheck.route-paths.health' => '/healthz',
]);
Expand All @@ -44,6 +82,58 @@ public function overrides_default_path()
$this->setChecks([AlwaysUpCheck::class]);

$response = $this->get('/healthz');
$this->assertSame([
'status' => 'OK',
'always-up' => ['status' => 'OK'],
], json_decode($response->getContent(), true));

$response = $this->get('/health');
$response->assertStatus(404);
}

/**
* @test
*/
public function defaults_the_ping_path_if_config_is_not_set()
{
app('router')->setRoutes(app(RouteCollection::class));

$response = $this->get('/ping');
$response->assertStatus(404);

config([
'healthcheck.route-paths' => null,
]);

// Manually re-boot the service provider to override the path
$this->app->getProvider(HealthCheckServiceProvider::class)->boot();

$this->setChecks([AlwaysUpCheck::class]);

$response = $this->get('/ping');
$this->assertSame('pong', $response->getContent());

}

/**
* @test
*/
public function defaults_the_health_path_if_config_is_not_set()
{
config([
'healthcheck.route-paths' => null,
]);

app('router')->setRoutes(app(RouteCollection::class));

$response = $this->get('/health');


$this->app->getProvider(HealthCheckServiceProvider::class)->boot();

$this->setChecks([AlwaysUpCheck::class]);

$response = $this->get('/health');

$this->assertSame([
'status' => 'OK',
Expand Down