diff --git a/src/Response.php b/src/Response.php index 9c9c50e503..56c420dc49 100644 --- a/src/Response.php +++ b/src/Response.php @@ -164,7 +164,7 @@ class Response extends AbstractMessage implements ResponseInterface * Populate object from string * * @param string $string - * @return Response + * @return self * @throws Exception\InvalidArgumentException */ public static function fromString($string) @@ -235,11 +235,12 @@ public function getCookie() * * @param int $code * @throws Exception\InvalidArgumentException - * @return Response + * @return self */ public function setStatusCode($code) { - if (!is_numeric($code)) { + $const = get_class($this) . '::STATUS_CODE_' . $code; + if (!is_numeric($code) || !defined($const)) { $code = is_scalar($code) ? $code : gettype($code); throw new Exception\InvalidArgumentException(sprintf( 'Invalid status code provided: "%s"', @@ -260,9 +261,31 @@ public function getStatusCode() return $this->statusCode; } + /** + * Set custom HTTP status code + * + * @param int $code + * @throws Exception\InvalidArgumentException + * @return self + */ + public function setCustomStatusCode($code) + { + if (!is_numeric($code)) { + $code = is_scalar($code) ? $code : gettype($code); + throw new Exception\InvalidArgumentException(sprintf( + 'Invalid status code provided: "%s"', + $code + )); + } + + $this->statusCode = (int) $code; + return $this; + + } + /** * @param string $reasonPhrase - * @return Response + * @return self */ public function setReasonPhrase($reasonPhrase) { @@ -277,7 +300,7 @@ public function setReasonPhrase($reasonPhrase) */ public function getReasonPhrase() { - if ($this->reasonPhrase == null) { + if (null == $this->reasonPhrase and isset($this->recommendedReasonPhrases[$this->statusCode])) { return $this->recommendedReasonPhrases[$this->statusCode]; } return $this->reasonPhrase; diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 012495bfdb..efe0ffe095 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -57,14 +57,29 @@ public function testResponseCanSetStatusCode() $this->assertEquals(303, $response->getStatusCode()); } - public function testResponseSetStatusCodeWithUnknownCode() + public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode() { $response = new Response; + $this->setExpectedException('Zend\Http\Exception\InvalidArgumentException', 'Invalid status code'); $response->setStatusCode(606); - $this->assertEquals(606, $response->getStatusCode()); } - public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode() + public function testResponseGetReasonPhraseWillReturnEmptyPhraseAsDefault() + { + $response = new Response; + $response->setCustomStatusCode(998); + $this->assertSame('HTTP/1.1 998' . "\r\n\r\n", (string) $response); + } + + public function testResponseCanSetCustomStatusCode() + { + $response = new Response; + $this->assertEquals(200, $response->getStatusCode()); + $response->setCustomStatusCode('999'); + $this->assertEquals(999, $response->getStatusCode()); + } + + public function testResponseSetCustomStatusCodeThrowsExceptionOnInvalidCode() { $response = new Response; $this->setExpectedException( @@ -281,7 +296,7 @@ public function testToString() $response = Response::fromString($response_str); $this->assertEquals(strtolower(str_replace("\n", "\r\n", $response_str)), strtolower($response->toString()), 'Response convertion to string does not match original string'); - $this->assertEquals(strtolower(str_replace("\n", "\r\n", $response_str)), strtolower((string) $response), 'Response convertion to string does not match original string'); + $this->assertEquals(strtolower(str_replace("\n", "\r\n", $response_str)), strtolower((string)$response), 'Response convertion to string does not match original string'); } public function testToStringGzip() @@ -290,7 +305,7 @@ public function testToStringGzip() $response = Response::fromString($response_str); $this->assertEquals(strtolower($response_str), strtolower($response->toString()), 'Response convertion to string does not match original string'); - $this->assertEquals(strtolower($response_str), strtolower((string) $response), 'Response convertion to string does not match original string'); + $this->assertEquals(strtolower($response_str), strtolower((string)$response), 'Response convertion to string does not match original string'); } public function testGetHeaders() @@ -312,6 +327,7 @@ public function testGetVersion() public function testUnknownCode() { $response_str = $this->readResponse('response_unknown'); + $this->setExpectedException('InvalidArgumentException', 'Invalid status code provided: "550"'); $response = Response::fromString($response_str); $this->assertEquals(550, $response->getStatusCode()); }