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/5732' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 4, 2014
176 parents d4019fb + fba45eb + e231336 + 03b41a2 + 75434aa + 4c82c64 + 593b71b + 8cc68f1 + 1642118 + 89a44d0 + 5c10b75 + db7d808 + 4ee93be + 9ecbeb2 + d240054 + 78d3205 + 8903b2d + 464835b + c59b54d + 14ad3a0 + f4ac37b + 0ec8036 + 8839d0e + 07564e2 + a65cb66 + ecb09fb + 6de3f68 + cb8f354 + d7ffaed + 1819a00 + e2ca906 + f897f74 + 0568c55 + d7f0623 + 8b4896f + 2f83acc + a44d7dd + 6bce797 + 6ad2b62 + 1e41fbd + a0078e6 + 9db875b + 2879406 + 9acc850 + eddeb7d + 5a77057 + 33c15ff + 9f13ebf + a2ad2a5 + c0ba21c + 94e82a8 + 459f1f9 + e0c8424 + 5ef6a39 + 58fd018 + 65e3b0b + d21655a + 1d20fd1 + fe81bc5 + 96fadae + 072c64d + d3c9780 + 9c15ae8 + 66a4342 + 85c9491 + 8ec8384 + 004366f + 55a086d + ac7c7af + f02a226 + 00c4ac3 + 3f52720 + 5508474 + 49ed2f6 + d562686 + 67b42b2 + bdb1dae + 9809630 + 7304e37 + 752a5af + 8181c8f + 53bdac2 + c2f9414 + f6341e9 + 57cde95 + c750616 + 293054e + 7432649 + 63f13f6 + 927c00d + 44e0d4b + bc03833 + 1fd7c61 + de0cb77 + 66e902f + 24354dc + 9f886a2 + 845333c + 96e9a1e + 52fbeb6 + 7f76d90 + fec4cec + 24efdcc + 219c9ad + 3025666 + 17d48b4 + 00f4506 + 0800032 + fc89677 + e89d79b + f329014 + a1bfde3 + 7dec05c + 734f9f2 + 2756031 + 408f714 + fd53447 + f1f6e0d + e193ac7 + dad06c3 + 03ab3c8 + d0b9eec + e47995d + 411b935 + ddc8029 + a6c1451 + e823524 + eb63995 + 2de2423 + a95575b + 03a862c + e195cc6 + f4ac757 + a53de82 + 42763a8 + 6b14b45 + 2c344a8 + ff8337b + 18b53cb + e669f19 + 592011d + 076244a + df3d273 + df0ceea + 52b821e + b5e6fa9 + be5e28e + c3d9295 + d6e6fdc + d9404b8 + c2783a1 + 6d17b76 + 91a4143 + b94d28c + 10e0e2d + 94f241e + 59a3bd6 + d222b5d + e77747f + a05e97f + 3403941 + 8dc0bae + fc99eb3 + 9a10360 + 40c699b + 78c73ae + b0a1695 + 03dc917 + 615fcf5 + 2558f0c + 67c0313 + 164ae16 + 84fb01d + 40fb990 + 4e9279b + 26df3d9 commit 7aa497d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
33 changes: 28 additions & 5 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"',
Expand All @@ -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)
{
Expand All @@ -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;
Expand Down
26 changes: 21 additions & 5 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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());
}
Expand Down

0 comments on commit 7aa497d

Please sign in to comment.