add content-type header on exception response

This commit is contained in:
rchoquet 2017-06-15 11:46:38 +02:00
parent 3c2b1ff929
commit 9e2b408f25
2 changed files with 35 additions and 18 deletions

View File

@ -74,7 +74,7 @@ class ExceptionController
'logger' => $logger, 'logger' => $logger,
'currentContent' => $currentContent, 'currentContent' => $currentContent,
) )
)); ), 200, array('Content-Type' => $request->getMimeType($request->getRequestFormat()) ?: 'text/html'));
} }
/** /**

View File

@ -22,14 +22,9 @@ class ExceptionControllerTest extends TestCase
{ {
public function testShowActionCanBeForcedToShowErrorPage() public function testShowActionCanBeForcedToShowErrorPage()
{ {
$twig = new Environment( $twig = $this->createTwigEnv(array('TwigBundle:Exception:error404.html.twig' => '<html>not found</html>'));
new ArrayLoader(array(
'TwigBundle:Exception:error404.html.twig' => 'ok',
))
);
$request = Request::create('whatever', 'GET'); $request = $this->createRequest('html');
$request->headers->set('X-Php-Ob-Level', 1);
$request->attributes->set('showException', false); $request->attributes->set('showException', false);
$exception = FlattenException::create(new \Exception(), 404); $exception = FlattenException::create(new \Exception(), 404);
$controller = new ExceptionController($twig, /* "showException" defaults to --> */ true); $controller = new ExceptionController($twig, /* "showException" defaults to --> */ true);
@ -37,25 +32,47 @@ class ExceptionControllerTest extends TestCase
$response = $controller->showAction($request, $exception, null); $response = $controller->showAction($request, $exception, null);
$this->assertEquals(200, $response->getStatusCode()); // successful request $this->assertEquals(200, $response->getStatusCode()); // successful request
$this->assertEquals('ok', $response->getContent()); // content of the error404.html template $this->assertEquals('<html>not found</html>', $response->getContent());
} }
public function testFallbackToHtmlIfNoTemplateForRequestedFormat() public function testFallbackToHtmlIfNoTemplateForRequestedFormat()
{ {
$twig = new Environment( $twig = $this->createTwigEnv(array('TwigBundle:Exception:error.html.twig' => '<html></html>'));
new ArrayLoader(array(
'TwigBundle:Exception:error.html.twig' => 'html',
))
);
$request = Request::create('whatever'); $request = $this->createRequest('txt');
$request->headers->set('X-Php-Ob-Level', 1); $exception = FlattenException::create(new \Exception());
$request->setRequestFormat('txt'); $controller = new ExceptionController($twig, false);
$controller->showAction($request, $exception);
$this->assertEquals('html', $request->getRequestFormat());
}
public function testResponseHasRequestedMimeType()
{
$twig = $this->createTwigEnv(array('TwigBundle:Exception:error.json.twig' => '{}'));
$request = $this->createRequest('json');
$exception = FlattenException::create(new \Exception()); $exception = FlattenException::create(new \Exception());
$controller = new ExceptionController($twig, false); $controller = new ExceptionController($twig, false);
$response = $controller->showAction($request, $exception); $response = $controller->showAction($request, $exception);
$this->assertEquals('html', $request->getRequestFormat()); $this->assertEquals('json', $request->getRequestFormat());
$this->assertEquals($request->getMimeType('json'), $response->headers->get('Content-Type'));
}
private function createRequest($requestFormat)
{
$request = Request::create('whatever');
$request->headers->set('X-Php-Ob-Level', 1);
$request->setRequestFormat($requestFormat);
return $request;
}
private function createTwigEnv(array $templates)
{
return new Environment(new ArrayLoader($templates));
} }
} }