View source
<?php
namespace Drupal\system\Tests\Routing;
use Drupal\Component\Utility\Html;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ExceptionHandlingTest extends KernelTestBase {
public static $modules = [
'system',
'router_test',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'router',
]);
\Drupal::service('router.builder')
->rebuild();
}
public function testJson403() {
$request = Request::create('/router_test/test15');
$request->query
->set('_format', 'json');
$request
->setRequestFormat('json');
$kernel = \Drupal::getContainer()
->get('http_kernel');
$response = $kernel
->handle($request);
$this
->assertEqual($response
->getStatusCode(), Response::HTTP_FORBIDDEN);
$this
->assertEqual($response->headers
->get('Content-type'), 'application/json');
$this
->assertEqual('{"message":""}', $response
->getContent());
}
public function testJson404() {
$request = Request::create('/not-found');
$request->query
->set('_format', 'json');
$request
->setRequestFormat('json');
$kernel = \Drupal::getContainer()
->get('http_kernel');
$response = $kernel
->handle($request);
$this
->assertEqual($response
->getStatusCode(), Response::HTTP_NOT_FOUND);
$this
->assertEqual($response->headers
->get('Content-type'), 'application/json');
$this
->assertEqual('{"message":"No route found for \\u0022GET \\/not-found\\u0022"}', $response
->getContent());
}
public function testHtml403() {
$request = Request::create('/router_test/test15');
$request
->setFormat('html', [
'text/html',
]);
$kernel = \Drupal::getContainer()
->get('http_kernel');
$response = $kernel
->handle($request)
->prepare($request);
$this
->assertEqual($response
->getStatusCode(), Response::HTTP_FORBIDDEN);
$this
->assertEqual($response->headers
->get('Content-type'), 'text/html; charset=UTF-8');
}
public function testHtml404() {
$request = Request::create('/not-found');
$request
->setFormat('html', [
'text/html',
]);
$kernel = \Drupal::getContainer()
->get('http_kernel');
$response = $kernel
->handle($request)
->prepare($request);
$this
->assertEqual($response
->getStatusCode(), Response::HTTP_NOT_FOUND);
$this
->assertEqual($response->headers
->get('Content-type'), 'text/html; charset=UTF-8');
}
public function testBacktraceEscaping() {
$this
->config('system.logging')
->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)
->save();
$request = Request::create('/router_test/test17');
$request
->setFormat('html', [
'text/html',
]);
$kernel = \Drupal::getContainer()
->get('http_kernel');
$response = $kernel
->handle($request)
->prepare($request);
$this
->assertEqual($response
->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
$this
->assertEqual($response->headers
->get('Content-type'), 'text/html; charset=UTF-8');
$this
->assertTrue(strpos($response
->getContent(), Html::escape('<script>alert(\'xss\')</script>')) !== FALSE);
$this
->assertTrue(strpos($response
->getContent(), '<script>alert(\'xss\')</script>') === FALSE);
}
public function testExceptionEscaping() {
$this
->config('system.logging')
->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)
->save();
$request = Request::create('/router_test/test24');
$request
->setFormat('html', [
'text/html',
]);
$kernel = \Drupal::getContainer()
->get('http_kernel');
$response = $kernel
->handle($request)
->prepare($request);
$this
->assertEqual($response
->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
$this
->assertEqual($response->headers
->get('Content-type'), 'text/html; charset=UTF-8');
$this
->setRawContent($response
->getContent());
$this
->assertRaw(Html::escape('Escaped content: <p> <br> <h3>'));
$this
->assertNoRaw('<p> <br> <h3>');
}
}