Skip to content

Commit

Permalink
Throw exception when invalid response status code is used.
Browse files Browse the repository at this point in the history
Closes #11677
  • Loading branch information
ADmad committed Feb 1, 2018
1 parent 6c2ea06 commit 6deb743
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Http/Response.php
Expand Up @@ -975,11 +975,19 @@ public function withStatus($code, $reasonPhrase = '')
* @param int $code The code to set.
* @param string $reasonPhrase The response reason phrase.
* @return void
* @throws \InvalidArgumentException For invalid status code arguments.
*/
protected function _setStatus($code, $reasonPhrase = '')
{
if (!isset($this->_statusCodes[$code])) {
throw new InvalidArgumentException(sprintf(
'Invalid status code: %s. Use a valid HTTP status code in range 1xx - 5xx.',
$code
));
}

$this->_status = $code;
if (empty($reasonPhrase) && isset($this->_statusCodes[$code])) {
if (empty($reasonPhrase)) {
$reasonPhrase = $this->_statusCodes[$code];
}
$this->_reasonPhrase = $reasonPhrase;
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Http/ResponseTest.php
Expand Up @@ -2835,6 +2835,19 @@ public function testWithStatusCode()
$this->assertNotEquals($response, $response2);
}

/**
* Test invalid status codes
*
* @return void
*/
public function testWithStatusInvalid()
{
$this->expectException(\InvalidArgumentException::class);
$this-> expectExceptionMessage('Invalid status code: 1001. Use a valid HTTP status code in range 1xx - 5xx.');
$response = new Response();
$response->withStatus(1001);
}

/**
* Test get reason phrase.
*
Expand Down

0 comments on commit 6deb743

Please sign in to comment.