ContentNegotiationRoutingTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Drupal\KernelTests\Core\Routing;
  3. use Drupal\Core\DependencyInjection\ContainerBuilder;
  4. use Drupal\KernelTests\KernelTestBase;
  5. use Drupal\Tests\Traits\Core\PathAliasTestTrait;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. /**
  9. * Tests content negotiation routing variations.
  10. *
  11. * @group ContentNegotiation
  12. */
  13. class ContentNegotiationRoutingTest extends KernelTestBase {
  14. use PathAliasTestTrait;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static $modules = ['conneg_test', 'path_alias'];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->installEntitySchema('path_alias');
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function register(ContainerBuilder $container) {
  30. parent::register($container);
  31. // \Drupal\KernelTests\KernelTestBase::register() removes the alias path
  32. // processor.
  33. if ($container->hasDefinition('path_alias.path_processor')) {
  34. $definition = $container->getDefinition('path_alias.path_processor');
  35. $definition->addTag('path_processor_inbound', ['priority' => 100])->addTag('path_processor_outbound', ['priority' => 300]);
  36. }
  37. }
  38. /**
  39. * Tests the content negotiation aspect of routing.
  40. */
  41. public function testContentRouting() {
  42. // Alias with extension pointing to no extension/constant content-type.
  43. $this->createPathAlias('/conneg/html', '/alias.html');
  44. // Alias with extension pointing to dynamic extension/linked content-type.
  45. $this->createPathAlias('/conneg/html?_format=json', '/alias.json');
  46. $tests = [
  47. // ['path', 'accept', 'content-type'],
  48. // Extension is part of the route path. Constant Content-type.
  49. ['conneg/simple.json', '', 'application/json'],
  50. ['conneg/simple.json', 'application/xml', 'application/json'],
  51. ['conneg/simple.json', 'application/json', 'application/json'],
  52. // No extension. Constant Content-type.
  53. ['conneg/html', '', 'text/html'],
  54. ['conneg/html', '*/*', 'text/html'],
  55. ['conneg/html', 'application/xml', 'text/html'],
  56. ['conneg/html', 'text/xml', 'text/html'],
  57. ['conneg/html', 'text/html', 'text/html'],
  58. // Dynamic extension. Linked Content-type.
  59. ['conneg/html?_format=json', '', 'application/json'],
  60. ['conneg/html?_format=json', '*/*', 'application/json'],
  61. ['conneg/html?_format=json', 'application/xml', 'application/json'],
  62. ['conneg/html?_format=json', 'application/json', 'application/json'],
  63. ['conneg/html?_format=xml', '', 'application/xml'],
  64. ['conneg/html?_format=xml', '*/*', 'application/xml'],
  65. ['conneg/html?_format=xml', 'application/json', 'application/xml'],
  66. ['conneg/html?_format=xml', 'application/xml', 'application/xml'],
  67. // Path with a variable. Variable contains a period.
  68. ['conneg/plugin/plugin.id', '', 'text/html'],
  69. ['conneg/plugin/plugin.id', '*/*', 'text/html'],
  70. ['conneg/plugin/plugin.id', 'text/xml', 'text/html'],
  71. ['conneg/plugin/plugin.id', 'text/html', 'text/html'],
  72. // Alias with extension pointing to no extension/constant content-type.
  73. ['alias.html', '', 'text/html'],
  74. ['alias.html', '*/*', 'text/html'],
  75. ['alias.html', 'text/xml', 'text/html'],
  76. ['alias.html', 'text/html', 'text/html'],
  77. ];
  78. foreach ($tests as $test) {
  79. $path = $test[0];
  80. $accept_header = $test[1];
  81. $content_type = $test[2];
  82. $message = "Testing path:$path Accept:$accept_header Content-type:$content_type";
  83. $request = Request::create('/' . $path);
  84. if ($accept_header) {
  85. $request->headers->set('Accept', $accept_header);
  86. }
  87. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  88. $kernel = \Drupal::getContainer()->get('http_kernel');
  89. $response = $kernel->handle($request);
  90. // Verbose message since simpletest doesn't let us provide a message and
  91. // see the error.
  92. $this->assertTrue(TRUE, $message);
  93. $this->assertEqual($response->getStatusCode(), Response::HTTP_OK);
  94. $this->assertStringContainsString($content_type, $response->headers->get('Content-type'));
  95. }
  96. }
  97. /**
  98. * Full negotiation by header only.
  99. */
  100. public function testFullNegotiation() {
  101. $this->enableModules(['accept_header_routing_test']);
  102. \Drupal::service('router.builder')->rebuild();
  103. $tests = [
  104. // ['path', 'accept', 'content-type'],
  105. // 406?
  106. ['conneg/negotiate', '', 'text/html'],
  107. // 406?
  108. ['conneg/negotiate', '', 'text/html'],
  109. // ['conneg/negotiate', '*/*', '??'],
  110. ['conneg/negotiate', 'application/json', 'application/json'],
  111. ['conneg/negotiate', 'application/xml', 'application/xml'],
  112. ['conneg/negotiate', 'application/json', 'application/json'],
  113. ['conneg/negotiate', 'application/xml', 'application/xml'],
  114. ];
  115. foreach ($tests as $test) {
  116. $path = $test[0];
  117. $accept_header = $test[1];
  118. $content_type = $test[2];
  119. $request = Request::create('/' . $path);
  120. $request->headers->set('Accept', $accept_header);
  121. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  122. $kernel = \Drupal::getContainer()->get('http_kernel');
  123. $response = $kernel->handle($request);
  124. $this->assertEqual($response->getStatusCode(), Response::HTTP_OK, "Testing path:$path Accept:$accept_header Content-type:$content_type");
  125. }
  126. }
  127. }