Skip to content

Commit

Permalink
Allow custom success status codes (#362)
Browse files Browse the repository at this point in the history
* Allow custom success status codes

* Add tests

* Update README.md
  • Loading branch information
emilsundberg authored Oct 20, 2024
1 parent 8a320f9 commit fa0b682
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ return [
* When reaching out to the sites these headers will be added.
*/
'additional_headers' => [],

/*
* Allow status codes other than 200 to be considered as successful uptime checks.
*/
'additional_status_codes' => [],
],

'certificate_check' => [
Expand Down
5 changes: 5 additions & 0 deletions config/uptime-monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
* When reaching out to the sites these headers will be added.
*/
'additional_headers' => [],

/*
* All status codes in this array will be interpreted as successful requests.
*/
'additional_status_codes' => [],
],

'certificate_check' => [
Expand Down
11 changes: 11 additions & 0 deletions src/MonitorCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ protected function getPromises(): Generator
'headers' => $this->promiseHeaders($monitor),
'body' => $monitor->uptime_check_payload,
])
)->then(
function (ResponseInterface $response) {
return $response;
},
function (TransferException $exception) {
if (in_array($exception->getCode(), config('uptime-monitor.uptime_check.additional_status_codes', []))) {
return $exception->getResponse();
}

throw $exception;
}
);

yield $promise;
Expand Down
36 changes: 36 additions & 0 deletions tests/Integration/Commands/CheckUptimeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,40 @@ public function it_can_use_a_custom_response_checker()
$this->assertEquals(UptimeStatus::DOWN, $monitor->uptime_status);
$this->assertEquals(ResponseCheckerFailureFake::FAILURE_REASON, $monitor->uptime_check_failure_reason);
}

/** @test */
public function it_handles_additional_status_codes_as_valid_responses()
{
config()->set('uptime-monitor.uptime_check.additional_status_codes', [401]);

$monitor = Monitor::factory()->create([
'uptime_status' => UptimeStatus::NOT_YET_CHECKED,
]);

$this->server->setResponseBody('', 401);

Artisan::call('monitor:check-uptime');

$monitor = $monitor->fresh();

$this->assertEquals(UptimeStatus::UP, $monitor->uptime_status);
}

/** @test */
public function it_handles_additional_status_codes_as_valid_responses_and_still_fails_on_others()
{
config()->set('uptime-monitor.uptime_check.additional_status_codes', [401]);

$monitor = Monitor::factory()->create([
'uptime_status' => UptimeStatus::NOT_YET_CHECKED,
]);

$this->server->setResponseBody('', 418);

Artisan::call('monitor:check-uptime');

$monitor = $monitor->fresh();

$this->assertEquals(UptimeStatus::DOWN, $monitor->uptime_status);
}
}

0 comments on commit fa0b682

Please sign in to comment.