diff --git a/app/src/Controller/Controller.php b/app/src/Controller/Controller.php index 9364401..00c0933 100644 --- a/app/src/Controller/Controller.php +++ b/app/src/Controller/Controller.php @@ -53,7 +53,9 @@ abstract class Controller */ public function render(string $templateName, array $data = []): Response { - return $this->view->render($this->response, $templateName, $data); + return $this->view + ->render($this->response, $templateName, $data) + ->withHeader('Content-Type', 'text/html'); } /** @@ -65,7 +67,7 @@ abstract class Controller { $renderedTemplate = $this->view->fetchFromString($html, $data); $this->response->getBody()->write($renderedTemplate); - return $this->response; + return $this->response->withHeader('Content-Type', 'text/html'); } /** diff --git a/app/tests/Controller/ControllerTest.php b/app/tests/Controller/ControllerTest.php index 7c68eae..d6ee8b0 100644 --- a/app/tests/Controller/ControllerTest.php +++ b/app/tests/Controller/ControllerTest.php @@ -28,4 +28,29 @@ class ControllerTest extends TestCase $this->assertEquals('
Dave
', $response->getBody()); } + + public function testHtmlInlineResponseCodes(): void + { + $testController = new class extends Controller { + public function respond(): Response + { + $response = $this->renderInline( + '{{ name }}
', + [ 'name' => 'Oh no!' ] + ); + + return $response->withStatus(400); + } + }; + + $app = $this->getAppInstance(); + $app->get('/test', $testController); + + $request = $this->createRequest('GET', '/test'); + $response = $app->handle($request); + + $this->assertEquals(400, $response->getStatusCode()); + $this->assertEquals('Oh no!
', $response->getBody()); + $this->assertEquals('text/html', $response->getHeaderLine('Content-Type')); + } }