8000 bug #23052 [TwigBundle] Add Content-Type header for exception respons… · symfony/symfony@c8884e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c8884e7

Browse files
committed
bug #23052 [TwigBundle] Add Content-Type header for exception response (rchoquet)
This PR was merged into the 2.7 branch. Discussion ---------- [TwigBundle] Add Content-Type header for exception response | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | This PR comes after I was looking to customize the way exceptions are served for a JSON API (grabbed the info at http://symfony.com/doc/current/controller/error_pages.html#overriding-the-default-exceptioncontroller). I noticed that even when changing the request format to 'json' so that the right json.twig template is served: ```php // in my override of the ExceptionController public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) { $request->setRequestFormat('json'); return parent::showAction($request, $exception, $logger); } ``` the response Content-Type header was still 'text/html'. By now, the response Content-Type should be corresponding to the given request format. I also feel there's some room for improvement with the general "displaying error for a JSON API" chapter as it feels strange that there's no configuration option to just say "serve me anything as json", but that's another issue. Commits ------- 9e2b408 add content-type header on exception response
2 parents b8c94d0 + 9e2b408 commit c8884e7

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function showAction(Request $request, FlattenException $exception, DebugL
7474
'logger' => $logger,
7575
'currentContent' => $currentContent,
7676
)
77-
));
77+
), 200, array('Content-Type' => $request->getMimeType($request->getRequestFormat()) ?: 'text/html'));
7878
}
7979

8080
/**

src/Symfony/Bundle/TwigBundle/Tests/Controller/ExceptionControllerTest.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,57 @@ class ExceptionControllerTest extends TestCase
2222
{
2323
public function testShowActionCanBeForcedToShowErrorPage()
2424
{
25-
$twig = new Environment(
26-
new ArrayLoader(array(
27-
'TwigBundle:Exception:error404.html.twig' => 'ok',
28-
))
29-
);
25+
$twig = $this->createTwigEnv(array('TwigBundle:Exception:error404.html.twig' => '<html>not found</html>'));
3026

31-
$request = Request::create('whatever', 'GET');
32-
$request->headers->set('X-Php-Ob-Level', 1);
27+
$request = $this->createRequest('html');
3328
$request->attributes->set('showException', false);
3429
$exception = FlattenException::create(new \Exception(), 404);
3530
$controller = new ExceptionController($twig, /* "showException" defaults to --> */ true);
3631

3732
$response = $controller->showAction($request, $exception, null);
3833

3934
$this->assertEquals(200, $response->getStatusCode()); // successful request
40-
$this->assertEquals('ok', $response->getContent()); // content of the error404.html template
35+
$this->assertEquals('<html>not found</html>', $response->getContent());
4136
}
4237

4338
public function testFallbackToHtmlIfNoTemplateForRequestedFormat()
4439
{
45-
$twig = new Environment(
46-
new ArrayLoader(array(
47-
'TwigBundle:Exception:error.html.twig' => 'html',
48-
))
49-
);
40+
$twig = $this->createTwigEnv(array('TwigBundle:Exception:error.html.twig' => '<html></html>'));
5041

51-
$request = Request::create('whatever');
52-
$request->headers->set('X-Php-Ob-Level', 1);
53-
$request->setRequestFormat('txt');
42+
$request = $this->createRequest('txt');
5443
$exception = FlattenException::create(new \Exception());
5544
$controller = new ExceptionController($twig, false);
5645

57-
$response = $controller->showAction($request, $exception);
46+
$controller->showAction($request, $exception);
5847

5948
$this->assertEquals('html', $request->getRequestFormat());
6049
}
50+
51+
public function testResponseHasRequestedMimeType()
52+
{
53+
$twig = $this->createTwigEnv(array('TwigBundle:Exception:error.json.twig' => '{}'));
54+
55+
$request = $this->createRequest('json');
56+
$exception = FlattenException::create(new \Exception());
57+
$controller = new ExceptionController($twig, false);
58+
59+
$response = $controller->showAction($request, $exception);
60+
61+
$this->assertEquals('json', $request->getRequestFormat());
62+
$this->assertEquals($request->getMimeType('json'), $response->headers->get('Content-Type'));
63+
}
64+
65+
private function createRequest($requestFormat)
66+
{
67+
$request = Request::create('whatever');
68+
$request->headers->set('X-Php-Ob-Level', 1);
69+
$request->setRequestFormat($requestFormat);
70+
71+
return $request;
72+
}
73+
74+
private function createTwigEnv(array $templates)
75+
{
76+
return new Environment(new ArrayLoader($templates));
77+
}
6178
}

0 commit comments

Comments
 (0)
0