From 62a632530084da14320420160567afceb4ba6425 Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Wed, 18 Oct 2023 17:54:00 +0700 Subject: [PATCH] feat: add response status code type - `isInformational()` - `isSuccessful()` - `isRedirection()` - `isClientError()` - `isServerError()` --- src/System/Http/Response.php | 40 ++++++++++++++++++++++++++++++++++++ tests/Http/ResponseTest.php | 19 +++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/System/Http/Response.php b/src/System/Http/Response.php index 40245dda..7ee36ccc 100644 --- a/src/System/Http/Response.php +++ b/src/System/Http/Response.php @@ -446,4 +446,44 @@ public function followRequest(Request $request, array $header_name = []) return $this; } + + /** + * Informational status code 1xx. + */ + public function isInformational(): bool + { + return $this->respone_code > 99 && $this->respone_code < 201; + } + + /** + * Successful status code 2xx. + */ + public function isSuccessful(): bool + { + return $this->respone_code > 199 && $this->respone_code < 301; + } + + /** + * Redirection status code 3xx. + */ + public function isRedirection(): bool + { + return $this->respone_code > 299 && $this->respone_code < 401; + } + + /** + * Client error status code 4xx. + */ + public function isClientError(): bool + { + return $this->respone_code > 399 && $this->respone_code < 501; + } + + /** + * Server error status code 5xx. + */ + public function isServerError(): bool + { + return $this->respone_code > 499 && $this->respone_code < 601; + } } diff --git a/tests/Http/ResponseTest.php b/tests/Http/ResponseTest.php index 2b4472b7..95e71e07 100644 --- a/tests/Http/ResponseTest.php +++ b/tests/Http/ResponseTest.php @@ -148,4 +148,23 @@ public function itCanGetResponeContent() $this->assertEquals('content', $res->getContent()); } + + /** @test */ + public function itCanGetTypeOfResponseCode() + { + $res = new Response('content', rand(100, 199)); + $this->assertTrue($res->isInformational()); + + $res = new Response('content', rand(200, 299)); + $this->assertTrue($res->isSuccessful()); + + $res = new Response('content', rand(300, 399)); + $this->assertTrue($res->isRedirection()); + + $res = new Response('content', rand(400, 499)); + $this->assertTrue($res->isClientError()); + + $res = new Response('content', rand(500, 599)); + $this->assertTrue($res->isServerError()); + } }