Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/194' into develop
Browse files Browse the repository at this point in the history
Close #194
Close #178
  • Loading branch information
michalbundyra committed Dec 3, 2019
2 parents 9dd3205 + fa71698 commit b8c93fa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- Nothing.
- [#194](https://github.com/zendframework/zend-http/pull/194) changes range of valid HTTP status codes to 100-599 (inclusive).

### Deprecated

Expand Down
24 changes: 19 additions & 5 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ class Response extends AbstractMessage implements ResponseInterface
const STATUS_CODE_599 = 599;
/**#@-*/

/**
* @internal
*/
const MIN_STATUS_CODE_VALUE = 100;

/**
* @internal
*/
const MAX_STATUS_CODE_VALUE = 599;

/**
* @var array Recommended Reason Phrases
*/
Expand Down Expand Up @@ -279,12 +289,16 @@ public function getCookie()
*/
public function setStatusCode($code)
{
$const = get_class($this) . '::STATUS_CODE_' . $code;
if (! is_numeric($code) || ! defined($const)) {
$code = is_scalar($code) ? $code : gettype($code);
if (! is_numeric($code)
|| is_float($code)
|| $code < static::MIN_STATUS_CODE_VALUE
|| $code > static::MAX_STATUS_CODE_VALUE
) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid status code provided: "%s"',
$code
'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
is_scalar($code) ? $code : gettype($code),
static::MIN_STATUS_CODE_VALUE,
static::MAX_STATUS_CODE_VALUE
));
}

Expand Down
24 changes: 17 additions & 7 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,24 @@ public function testRequestCanSetHeaders()
$this->assertSame($headers, $response->getHeaders());
}

public function testResponseCanSetStatusCode()
public function validStatusCode()
{
for ($i = 100; $i <= 599; ++$i) {
yield $i => [$i];
}
}

/**
* @dataProvider validStatusCode
*
* @param int $statusCode
*/
public function testResponseCanSetStatusCode($statusCode)
{
$response = new Response();
$this->assertEquals(200, $response->getStatusCode());
$response->setStatusCode('303');
$this->assertEquals(303, $response->getStatusCode());
$this->assertSame(200, $response->getStatusCode());
$response->setStatusCode($statusCode);
$this->assertSame($statusCode, $response->getStatusCode());
}

public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode()
Expand Down Expand Up @@ -142,7 +154,7 @@ public function testResponseSetCustomStatusCodeThrowsExceptionOnInvalidCode()
{
$response = new Response();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid status code provided: "foo"');
$this->expectExceptionMessage('Invalid status code "foo"; must be an integer between 100 and 599, inclusive');

$response->setStatusCode('foo');
}
Expand Down Expand Up @@ -540,8 +552,6 @@ public function testGetVersion()
public function testUnknownCode()
{
$responseStr = $this->readResponse('response_unknown');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid status code provided: "550"');
$response = Response::fromString($responseStr);
$this->assertEquals(550, $response->getStatusCode());
}
Expand Down

0 comments on commit b8c93fa

Please sign in to comment.