Skip to content

[TwigBundle] Add Content-Type header for exception response #23052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function showAction(Request $request, FlattenException $exception, DebugL
'logger' => $logger,
'currentContent' => $currentContent,
)
));
), 200, array('Content-Type' => $request->getMimeType($request->getRequestFormat()) ?: 'text/html'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,57 @@ 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);

$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);

$response = $controller->showAction($request, $exception);
$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('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));
}
}