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

Commit

Permalink
Merge branch 'hotfix/52' into develop
Browse files Browse the repository at this point in the history
Forward port #52
  • Loading branch information
weierophinney committed Mar 24, 2016
2 parents ab3fafd + 534f5c7 commit 2bd2653
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ details.

### Fixed

- Nothing.
- [#52](https://github.com/zendframework/zend-stratigility/pull/52) fixes the
behavior of the `FinalHandler` with regards to exception handling, ensuring
that the reason phrase reported corresponds to the HTTP status code used.

## 1.2.0 - 2016-03-17

Expand Down
11 changes: 6 additions & 5 deletions src/FinalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ public function setOriginalResponse(ResponseInterface $response = null)
*/
private function handleError($error, RequestInterface $request, ResponseInterface $response)
{
$response = $response->withStatus(
Utils::getStatusCode($error, $response),
$response->getReasonPhrase()
);

$statusCode = Utils::getStatusCode($error, $response);
$reasonPhrase = $response->getStatusCode() === $statusCode
? $response->getReasonPhrase()
: '';
$response = $response->withStatus($statusCode, $reasonPhrase);
$message = $response->getReasonPhrase() ?: 'Unknown Error';

if (isset($this->options['env']) && $this->options['env'] !== 'production') {
$message = $this->createDevelopmentErrorMessage($error);
}
Expand Down
30 changes: 28 additions & 2 deletions test/FinalHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@

class FinalHandlerTest extends TestCase
{
/**
* @var Escaper
*/
private $escaper;

/**
* @var FinalHandler
*/
private $final;

/**
* @var Request
*/
private $request;

/**
* @var Response
*/
private $response;

public function setUp()
{
$psrRequest = new PsrRequest([], [], 'http://example.com/', 'GET', 'php://memory');
Expand Down Expand Up @@ -149,13 +169,19 @@ public function testCreates404ResponseWhenNoErrorIsPresent()
$this->assertEquals(404, $response->getStatusCode());
}

public function testErrorResponsePreservesOriginalReasonPhraseIfSet()
public function testErrorResponsePreservesResponseReasonPhraseIfStatusCodeMatchesExceptionCode()
{
$this->response = $this->response->withStatus(500, 'It broke!');
$response = call_user_func($this->final, $this->request, $this->response, new \Exception('foo'));
$response = call_user_func($this->final, $this->request, $this->response, new \Exception('foo', 500));
$this->assertSame($this->response->getReasonPhrase(), $response->getReasonPhrase());
}

public function testErrorResponseUsesStandardHttpStatusCodeReasonPhraseIfExceptionCodeCausesStatusCodeToChange()
{
$response = call_user_func($this->final, $this->request, $this->response, new \Exception('foo', 418));
$this->assertSame("I'm a teapot", $response->getReasonPhrase());
}

public function test404ResponseIncludesOriginalRequestUri()
{
$originalUrl = 'http://local.example.com/bar/foo';
Expand Down

0 comments on commit 2bd2653

Please sign in to comment.