Skip to content

Commit fa0b682

Browse files
authored
Allow custom success status codes (#362)
* Allow custom success status codes * Add tests * Update README.md
1 parent 8a320f9 commit fa0b682

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ return [
127127
* When reaching out to the sites these headers will be added.
128128
*/
129129
'additional_headers' => [],
130+
131+
/*
132+
* Allow status codes other than 200 to be considered as successful uptime checks.
133+
*/
134+
'additional_status_codes' => [],
130135
],
131136

132137
'certificate_check' => [

config/uptime-monitor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
* When reaching out to the sites these headers will be added.
114114
*/
115115
'additional_headers' => [],
116+
117+
/*
118+
* All status codes in this array will be interpreted as successful requests.
119+
*/
120+
'additional_status_codes' => [],
116121
],
117122

118123
'certificate_check' => [

src/MonitorCollection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ protected function getPromises(): Generator
5555
'headers' => $this->promiseHeaders($monitor),
5656
'body' => $monitor->uptime_check_payload,
5757
])
58+
)->then(
59+
function (ResponseInterface $response) {
60+
return $response;
61+
},
62+
function (TransferException $exception) {
63+
if (in_array($exception->getCode(), config('uptime-monitor.uptime_check.additional_status_codes', []))) {
64+
return $exception->getResponse();
65+
}
66+
67+
throw $exception;
68+
}
5869
);
5970

6071
yield $promise;

tests/Integration/Commands/CheckUptimeCommandTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,40 @@ public function it_can_use_a_custom_response_checker()
100100
$this->assertEquals(UptimeStatus::DOWN, $monitor->uptime_status);
101101
$this->assertEquals(ResponseCheckerFailureFake::FAILURE_REASON, $monitor->uptime_check_failure_reason);
102102
}
103+
104+
/** @test */
105+
public function it_handles_additional_status_codes_as_valid_responses()
106+
{
107+
config()->set('uptime-monitor.uptime_check.additional_status_codes', [401]);
108+
109+
$monitor = Monitor::factory()->create([
110+
'uptime_status' => UptimeStatus::NOT_YET_CHECKED,
111+
]);
112+
113+
$this->server->setResponseBody('', 401);
114+
115+
Artisan::call('monitor:check-uptime');
116+
117+
$monitor = $monitor->fresh();
118+
119+
$this->assertEquals(UptimeStatus::UP, $monitor->uptime_status);
120+
}
121+
122+
/** @test */
123+
public function it_handles_additional_status_codes_as_valid_responses_and_still_fails_on_others()
124+
{
125+
config()->set('uptime-monitor.uptime_check.additional_status_codes', [401]);
126+
127+
$monitor = Monitor::factory()->create([
128+
'uptime_status' => UptimeStatus::NOT_YET_CHECKED,
129+
]);
130+
131+
$this->server->setResponseBody('', 418);
132+
133+
Artisan::call('monitor:check-uptime');
134+
135+
$monitor = $monitor->fresh();
136+
137+
$this->assertEquals(UptimeStatus::DOWN, $monitor->uptime_status);
138+
}
103139
}

0 commit comments

Comments
 (0)