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,
'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()
{
$twig = new Environment(
new ArrayLoader(array(
'TwigBundle:Exception:error404.html.twig' => 'ok',
))
);
$twig = $this->createTwigEnv(array('TwigBundle:Exception:error404.html.twig' => '<html>not found</html>'));
$request = Request::create('whatever', 'GET');
$request->headers->set('X-Php-Ob-Level', 1);
$request = $this->createRequest('html');
$request->attributes->set('showException', false);
$exception = FlattenException::create(new \Exception(), 404);
$controller = new ExceptionController($twig, /* "showException" defaults to --> */ true);
@ -37,25 +32,47 @@ class ExceptionControllerTest extends TestCase
$response = $controller->showAction($request, $exception, null);
$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()
{
$twig = new Environment(
new ArrayLoader(array(
'TwigBundle:Exception:error.html.twig' => 'html',
))
);
$twig = $this->createTwigEnv(array('TwigBundle:Exception:error.html.twig' => '<html></html>'));
$request = Request::create('whatever');
$request->headers->set('X-Php-Ob-Level', 1);
$request->setRequestFormat('txt');
$request = $this->createRequest('txt');
$exception = FlattenException::create(new \Exception());
$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());
$controller = new ExceptionController($twig, false);
$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));
}
}