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

Commit

Permalink
Merge pull request #52 from michaelmoussa/bugfix/51
Browse files Browse the repository at this point in the history
[BUG FIX] "500 OK" error responses
  • Loading branch information
weierophinney committed Mar 24, 2016
2 parents 0c5903a + e7d1b83 commit 4ed19b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
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 4ed19b5

Please sign in to comment.