3 namespace Drupal\KernelTests\Core\Routing;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
11 * Tests content negotiation routing variations.
13 * @group ContentNegotiation
15 class ContentNegotiationRoutingTest extends KernelTestBase {
20 public static $modules = ['conneg_test'];
25 public function register(ContainerBuilder $container) {
26 parent::register($container);
28 // \Drupal\KernelTests\KernelTestBase::register() removes the alias path
30 if ($container->hasDefinition('path_processor_alias')) {
31 $definition = $container->getDefinition('path_processor_alias');
32 $definition->addTag('path_processor_inbound', ['priority' => 100])->addTag('path_processor_outbound', ['priority' => 300]);
37 * Tests the content negotiation aspect of routing.
39 public function testContentRouting() {
40 /** @var \Drupal\Core\Path\AliasStorageInterface $path_alias_storage */
41 $path_alias_storage = $this->container->get('path.alias_storage');
42 // Alias with extension pointing to no extension/constant content-type.
43 $path_alias_storage->save('/conneg/html', '/alias.html');
45 // Alias with extension pointing to dynamic extension/linked content-type.
46 $path_alias_storage->save('/conneg/html?_format=json', '/alias.json');
49 // ['path', 'accept', 'content-type'],
51 // Extension is part of the route path. Constant Content-type.
52 ['conneg/simple.json', '', 'application/json'],
53 ['conneg/simple.json', 'application/xml', 'application/json'],
54 ['conneg/simple.json', 'application/json', 'application/json'],
55 // No extension. Constant Content-type.
56 ['conneg/html', '', 'text/html'],
57 ['conneg/html', '*/*', 'text/html'],
58 ['conneg/html', 'application/xml', 'text/html'],
59 ['conneg/html', 'text/xml', 'text/html'],
60 ['conneg/html', 'text/html', 'text/html'],
61 // Dynamic extension. Linked Content-type.
62 ['conneg/html?_format=json', '', 'application/json'],
63 ['conneg/html?_format=json', '*/*', 'application/json'],
64 ['conneg/html?_format=json', 'application/xml', 'application/json'],
65 ['conneg/html?_format=json', 'application/json', 'application/json'],
66 ['conneg/html?_format=xml', '', 'application/xml'],
67 ['conneg/html?_format=xml', '*/*', 'application/xml'],
68 ['conneg/html?_format=xml', 'application/json', 'application/xml'],
69 ['conneg/html?_format=xml', 'application/xml', 'application/xml'],
71 // Path with a variable. Variable contains a period.
72 ['conneg/plugin/plugin.id', '', 'text/html'],
73 ['conneg/plugin/plugin.id', '*/*', 'text/html'],
74 ['conneg/plugin/plugin.id', 'text/xml', 'text/html'],
75 ['conneg/plugin/plugin.id', 'text/html', 'text/html'],
77 // Alias with extension pointing to no extension/constant content-type.
78 ['alias.html', '', 'text/html'],
79 ['alias.html', '*/*', 'text/html'],
80 ['alias.html', 'text/xml', 'text/html'],
81 ['alias.html', 'text/html', 'text/html'],
84 foreach ($tests as $test) {
86 $accept_header = $test[1];
87 $content_type = $test[2];
88 $message = "Testing path:$path Accept:$accept_header Content-type:$content_type";
89 $request = Request::create('/' . $path);
91 $request->headers->set('Accept', $accept_header);
94 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
95 $kernel = \Drupal::getContainer()->get('http_kernel');
96 $response = $kernel->handle($request);
97 // Verbose message since simpletest doesn't let us provide a message and
99 $this->assertTrue(TRUE, $message);
100 $this->assertEqual($response->getStatusCode(), Response::HTTP_OK);
101 $this->assertTrue(strpos($response->headers->get('Content-type'), $content_type) !== FALSE);
106 * Full negotiation by header only.
108 public function testFullNegotiation() {
109 $this->enableModules(['accept_header_routing_test']);
110 \Drupal::service('router.builder')->rebuild();
112 // ['path', 'accept', 'content-type'],
114 ['conneg/negotiate', '', 'text/html'], // 406?
115 ['conneg/negotiate', '', 'text/html'], // 406?
116 // ['conneg/negotiate', '*/*', '??'],
117 ['conneg/negotiate', 'application/json', 'application/json'],
118 ['conneg/negotiate', 'application/xml', 'application/xml'],
119 ['conneg/negotiate', 'application/json', 'application/json'],
120 ['conneg/negotiate', 'application/xml', 'application/xml'],
123 foreach ($tests as $test) {
125 $accept_header = $test[1];
126 $content_type = $test[2];
127 $message = "Testing path:$path Accept:$accept_header Content-type:$content_type";
128 $request = Request::create('/' . $path);
129 $request->headers->set('Accept', $accept_header);
131 /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
132 $kernel = \Drupal::getContainer()->get('http_kernel');
133 $response = $kernel->handle($request);
134 // Verbose message since simpletest doesn't let us provide a message and
136 $this->pass($message);
137 $this->assertEqual($response->getStatusCode(), Response::HTTP_OK);