From 79204abcc710d4619f8c9ca1534e10b74a61396c Mon Sep 17 00:00:00 2001 From: Roy Palacios <2920326@planet.ftn.fedex.com> Date: Tue, 1 Oct 2019 14:45:33 -0500 Subject: [PATCH 1/3] Issue 178 - Invalid status code provided while processing the Http Response --- src/Response.php | 17 ++++++++++++----- test/ResponseTest.php | 12 +++++++++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Response.php b/src/Response.php index 7edab23587..bed5f4b331 100644 --- a/src/Response.php +++ b/src/Response.php @@ -88,6 +88,9 @@ class Response extends AbstractMessage implements ResponseInterface const STATUS_CODE_599 = 599; /**#@-*/ + const MIN_STATUS_CODE_VALUE = 100; + const MAX_STATUS_CODE_VALUE = 599; + /** * @var array Recommended Reason Phrases */ @@ -279,12 +282,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 )); } diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 6d1f48abef..37a4e8203d 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -115,6 +115,14 @@ public function testResponseCanSetStatusCode() $this->assertEquals(303, $response->getStatusCode()); } + public function testResponseCanSetStatusCodeNonStandard() + { + $response = new Response(); + $this->assertEquals(200, $response->getStatusCode()); + $response->setStatusCode('520'); + $this->assertEquals(520, $response->getStatusCode()); + } + public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode() { $response = new Response(); @@ -142,7 +150,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'); } @@ -540,8 +548,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()); } From 868b6817874f6fbbcf9adcd5a990c4c14567d013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Tue, 3 Dec 2019 10:24:11 +0000 Subject: [PATCH 2/3] Test whole range of allowed status codes 100-599 (inclusive) --- test/ResponseTest.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 37a4e8203d..b62aaa15e4 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -107,20 +107,24 @@ public function testRequestCanSetHeaders() $this->assertSame($headers, $response->getHeaders()); } - public function testResponseCanSetStatusCode() + public function validStatusCode() { - $response = new Response(); - $this->assertEquals(200, $response->getStatusCode()); - $response->setStatusCode('303'); - $this->assertEquals(303, $response->getStatusCode()); + for ($i = 100; $i <= 599; ++$i) { + yield $i => [$i]; + } } - public function testResponseCanSetStatusCodeNonStandard() + /** + * @dataProvider validStatusCode + * + * @param int $statusCode + */ + public function testResponseCanSetStatusCode($statusCode) { $response = new Response(); - $this->assertEquals(200, $response->getStatusCode()); - $response->setStatusCode('520'); - $this->assertEquals(520, $response->getStatusCode()); + $this->assertSame(200, $response->getStatusCode()); + $response->setStatusCode($statusCode); + $this->assertSame($statusCode, $response->getStatusCode()); } public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode() From 07e61ddd7012d9ae57296c1bf9c36850c0b4ca8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Tue, 3 Dec 2019 10:24:33 +0000 Subject: [PATCH 3/3] Mark newly added constants as internal --- src/Response.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Response.php b/src/Response.php index bed5f4b331..178639ddb5 100644 --- a/src/Response.php +++ b/src/Response.php @@ -88,7 +88,14 @@ 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; /**