Skip to content

Commit

Permalink
feat: add response status code type
Browse files Browse the repository at this point in the history
- `isInformational()`
- `isSuccessful()`
- `isRedirection()`
- `isClientError()`
- `isServerError()`
  • Loading branch information
SonyPradana committed Oct 18, 2023
1 parent 33c3b0e commit 62a6325
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/System/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 19 additions & 0 deletions tests/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 62a6325

Please sign in to comment.