View source
<?php
namespace Drupal\system\Tests\Routing;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ContentNegotiationRoutingTest extends KernelTestBase {
public static $modules = [
'system',
'conneg_test',
];
protected function setUp() {
\Drupal::unsetContainer();
parent::setUp();
$this
->installSchema('system', [
'router',
'url_alias',
]);
\Drupal::service('router.builder')
->rebuild();
}
public function containerBuild(ContainerBuilder $container) {
parent::containerBuild($container);
if ($container
->hasDefinition('path_processor_alias')) {
$definition = $container
->getDefinition('path_processor_alias');
$definition
->addTag('path_processor_inbound', [
'priority' => 100,
])
->addTag('path_processor_outbound', [
'priority' => 300,
]);
}
}
function testContentRouting() {
$path_alias_storage = $this->container
->get('path.alias_storage');
$path_alias_storage
->save('/conneg/html', '/alias.html');
$path_alias_storage
->save('/conneg/html?_format=json', '/alias.json');
$tests = [
[
'conneg/simple.json',
'',
'application/json',
],
[
'conneg/simple.json',
'application/xml',
'application/json',
],
[
'conneg/simple.json',
'application/json',
'application/json',
],
[
'conneg/html',
'',
'text/html',
],
[
'conneg/html',
'*/*',
'text/html',
],
[
'conneg/html',
'application/xml',
'text/html',
],
[
'conneg/html',
'text/xml',
'text/html',
],
[
'conneg/html',
'text/html',
'text/html',
],
[
'conneg/html?_format=json',
'',
'application/json',
],
[
'conneg/html?_format=json',
'*/*',
'application/json',
],
[
'conneg/html?_format=json',
'application/xml',
'application/json',
],
[
'conneg/html?_format=json',
'application/json',
'application/json',
],
[
'conneg/html?_format=xml',
'',
'application/xml',
],
[
'conneg/html?_format=xml',
'*/*',
'application/xml',
],
[
'conneg/html?_format=xml',
'application/json',
'application/xml',
],
[
'conneg/html?_format=xml',
'application/xml',
'application/xml',
],
[
'conneg/plugin/plugin.id',
'',
'text/html',
],
[
'conneg/plugin/plugin.id',
'*/*',
'text/html',
],
[
'conneg/plugin/plugin.id',
'text/xml',
'text/html',
],
[
'conneg/plugin/plugin.id',
'text/html',
'text/html',
],
[
'alias.html',
'',
'text/html',
],
[
'alias.html',
'*/*',
'text/html',
],
[
'alias.html',
'text/xml',
'text/html',
],
[
'alias.html',
'text/html',
'text/html',
],
];
foreach ($tests as $test) {
$path = $test[0];
$accept_header = $test[1];
$content_type = $test[2];
$message = "Testing path:{$path} Accept:{$accept_header} Content-type:{$content_type}";
$request = Request::create('/' . $path);
if ($accept_header) {
$request->headers
->set('Accept', $accept_header);
}
$kernel = \Drupal::getContainer()
->get('http_kernel');
$response = $kernel
->handle($request);
$this
->assertTrue(TRUE, $message);
$this
->assertEqual($response
->getStatusCode(), Response::HTTP_OK);
$this
->assertTrue(strpos($response->headers
->get('Content-type'), $content_type) !== FALSE);
}
}
public function testFullNegotiation() {
$this
->enableModules([
'accept_header_routing_test',
]);
\Drupal::service('router.builder')
->rebuild();
$tests = [
[
'conneg/negotiate',
'',
'text/html',
],
[
'conneg/negotiate',
'',
'text/html',
],
[
'conneg/negotiate',
'application/json',
'application/json',
],
[
'conneg/negotiate',
'application/xml',
'application/xml',
],
[
'conneg/negotiate',
'application/json',
'application/json',
],
[
'conneg/negotiate',
'application/xml',
'application/xml',
],
];
foreach ($tests as $test) {
$path = $test[0];
$accept_header = $test[1];
$content_type = $test[2];
$message = "Testing path:{$path} Accept:{$accept_header} Content-type:{$content_type}";
$request = Request::create('/' . $path);
$request->headers
->set('Accept', $accept_header);
$kernel = \Drupal::getContainer()
->get('http_kernel');
$response = $kernel
->handle($request);
$this
->pass($message);
$this
->assertEqual($response
->getStatusCode(), Response::HTTP_OK);
}
}
}