summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Middleware/ErrorHandler.php4
-rw-r--r--tests/Unit/Middleware/ErrorHandlerTest.php19
2 files changed, 23 insertions, 0 deletions
diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php
index 9fc34722..29b1fac1 100644
--- a/src/Middleware/ErrorHandler.php
+++ b/src/Middleware/ErrorHandler.php
@@ -48,6 +48,10 @@ class ErrorHandler implements MiddlewareInterface
$statusCode = $response->getStatusCode();
$contentType = $response->getHeader('content-type');
$contentType = array_shift($contentType);
+ if (!$contentType && strpos($response->getBody(), '<html') !== false) {
+ $contentType = 'text/html';
+ }
+
if (
$statusCode < 400
|| !$response instanceof Response
diff --git a/tests/Unit/Middleware/ErrorHandlerTest.php b/tests/Unit/Middleware/ErrorHandlerTest.php
index cce752d1..6c37b651 100644
--- a/tests/Unit/Middleware/ErrorHandlerTest.php
+++ b/tests/Unit/Middleware/ErrorHandlerTest.php
@@ -143,4 +143,23 @@ class ErrorHandlerTest extends TestCase
$return = $errorHandler->process($request, $returnResponseHandler);
$this->assertEquals($psrResponse, $return);
}
+
+ /**
+ * @covers \Engelsystem\Middleware\ErrorHandler::process
+ */
+ public function testProcessContentTypeSniffer()
+ {
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->createMock(ServerRequestInterface::class);
+ /** @var TwigLoader|MockObject $twigLoader */
+ $twigLoader = $this->createMock(TwigLoader::class);
+ $response = new Response('<!DOCTYPE html><html><body><h1>Hi!</h1></body></html>', 500);
+ $returnResponseHandler = new ReturnResponseMiddlewareHandler($response);
+
+ /** @var ErrorHandler|MockObject $errorHandler */
+ $errorHandler = new ErrorHandler($twigLoader);
+
+ $return = $errorHandler->process($request, $returnResponseHandler);
+ $this->assertEquals($response, $return);
+ }
}