Skip to content

Commit

Permalink
Merge pull request #246 from maartenpaauw/bugfix/only-return-failure-…
Browse files Browse the repository at this point in the history
…status-code-when-results-contains-failures

Only return failure status code when results contains failures
  • Loading branch information
freekmurze authored Dec 9, 2024
2 parents 2680f29 + 2dee669 commit 2c23228
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Http/Controllers/HealthCheckJsonResultsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public function __invoke(Request $request, ResultStore $resultStore): Response

$checkResults = $resultStore->latestResults();

return response($checkResults?->toJson() ?? '', config('health.json_results_failure_status', 200))
$statusCode = $checkResults?->containsFailingCheck()
? config('health.json_results_failure_status', Response::HTTP_OK)
: Response::HTTP_OK;

return response($checkResults?->toJson() ?? '', $statusCode)
->header('Content-Type', 'application/json')
->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
}
Expand Down
17 changes: 13 additions & 4 deletions tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,25 @@
->and($storedCheckResults->storedCheckResults)->toHaveCount(1);
});

it('will return the configured status for a unhealthy check', function () {
$this->check->replyWith(fn () => false);

it('will return the configured status code for an unhealthy check', function () {
config()->set('health.json_results_failure_status', Response::HTTP_SERVICE_UNAVAILABLE);

artisan(RunHealthChecksCommand::class);

$json = getJson('/')
->assertStatus(Response::HTTP_SERVICE_UNAVAILABLE)
->assertServiceUnavailable()
->json();

assertMatchesSnapshot($json);
});

it('will return http ok status code when there are no failing checks', function () {
$this->check->fakeDiskUsagePercentage(50);

config()->set('health.json_results_failure_status', Response::HTTP_SERVICE_UNAVAILABLE);

artisan(RunHealthChecksCommand::class);

getJson('/')
->assertOk();
});

0 comments on commit 2c23228

Please sign in to comment.