Add content type headers

This commit is contained in:
Dave Smith-Hayes 2024-05-22 21:13:05 -04:00
parent 88fd8be833
commit 8d5c16fc29
2 changed files with 29 additions and 2 deletions

View File

@ -53,7 +53,9 @@ abstract class Controller
*/ */
public function render(string $templateName, array $data = []): Response 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); $renderedTemplate = $this->view->fetchFromString($html, $data);
$this->response->getBody()->write($renderedTemplate); $this->response->getBody()->write($renderedTemplate);
return $this->response; return $this->response->withHeader('Content-Type', 'text/html');
} }
/** /**

View File

@ -28,4 +28,29 @@ class ControllerTest extends TestCase
$this->assertEquals('<p>Dave</p>', $response->getBody()); $this->assertEquals('<p>Dave</p>', $response->getBody());
} }
public function testHtmlInlineResponseCodes(): void
{
$testController = new class extends Controller {
public function respond(): Response
{
$response = $this->renderInline(
'<p>{{ name }}</p>',
[ '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('<p>Oh no!</p>', $response->getBody());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
}
} }