3 namespace Drupal\Tests\Core\Routing;
5 use Drupal\Core\Routing\ContentTypeHeaderMatcher;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\ParameterBag;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
12 * Confirm that the content types partial matcher is functioning properly.
16 * @coversDefaultClass \Drupal\Core\Routing\ContentTypeHeaderMatcher
18 class ContentTypeHeaderMatcherTest extends UnitTestCase {
21 * A collection of shared fixture data for tests.
23 * @var RoutingFixtures
28 * The matcher object that is going to be tested.
30 * @var \Drupal\Core\Routing\ContentTypeHeaderMatcher
37 protected function setUp() {
40 $this->fixtures = new RoutingFixtures();
41 $this->matcher = new ContentTypeHeaderMatcher();
45 * Tests that routes are not filtered on safe requests.
47 * @dataProvider providerTestSafeRequestFilter
49 public function testSafeRequestFilter($method) {
50 $collection = $this->fixtures->sampleRouteCollection();
51 $collection->addCollection($this->fixtures->contentRouteCollection());
53 $request = Request::create('path/two', $method);
54 $routes = $this->matcher->filter($collection, $request);
55 $this->assertEquals(count($routes), 7, 'The correct number of routes was found.');
58 public function providerTestSafeRequestFilter() {
69 * Tests that XML-restricted routes get filtered out on JSON requests.
71 public function testJsonRequest() {
72 $collection = $this->fixtures->sampleRouteCollection();
73 $collection->addCollection($this->fixtures->contentRouteCollection());
75 $request = Request::create('path/two', 'POST');
76 $request->headers->set('Content-type', 'application/json');
77 $routes = $this->matcher->filter($collection, $request);
78 $this->assertEquals(count($routes), 6, 'The correct number of routes was found.');
79 $this->assertNotNull($routes->get('route_f'), 'The json route was found.');
80 $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
81 foreach ($routes as $name => $route) {
82 $this->assertEquals($name, 'route_f', 'The json route is the first one in the collection.');
88 * Tests route filtering on POST form submission requests.
90 public function testPostForm() {
91 $collection = $this->fixtures->sampleRouteCollection();
92 $collection->addCollection($this->fixtures->contentRouteCollection());
94 // Test that all XML and JSON restricted routes get filtered out on a POST
96 $request = Request::create('path/two', 'POST');
97 $request->headers->set('Content-type', 'application/www-form-urlencoded');
98 $routes = $this->matcher->filter($collection, $request);
99 $this->assertEquals(count($routes), 5, 'The correct number of routes was found.');
100 $this->assertNull($routes->get('route_f'), 'The json route was found.');
101 $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
105 * Confirms that the matcher throws an exception for no-route.
109 public function testNoRouteFound() {
110 $matcher = new ContentTypeHeaderMatcher();
112 $routes = $this->fixtures->contentRouteCollection();
113 $request = Request::create('path/two', 'POST');
114 $request->headers->set('Content-type', 'application/hal+json');
115 $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No route found that matches "Content-Type: application/hal+json"');
116 $matcher->filter($routes, $request);
120 * Confirms that the matcher throws an exception for missing request header.
124 public function testContentTypeRequestHeaderMissing() {
125 $matcher = new ContentTypeHeaderMatcher();
127 $routes = $this->fixtures->contentRouteCollection();
128 $request = Request::create('path/two', 'POST');
129 // Delete all request headers that Request::create() sets by default.
130 $request->headers = new ParameterBag();
131 $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No "Content-Type" request header specified');
132 $matcher->filter($routes, $request);