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 GET requests.
47 public function testGetRequestFilter() {
48 $collection = $this->fixtures->sampleRouteCollection();
49 $collection->addCollection($this->fixtures->contentRouteCollection());
51 $request = Request::create('path/two', 'GET');
52 $routes = $this->matcher->filter($collection, $request);
53 $this->assertEquals(count($routes), 7, 'The correct number of routes was found.');
57 * Tests that XML-restricted routes get filtered out on JSON requests.
59 public function testJsonRequest() {
60 $collection = $this->fixtures->sampleRouteCollection();
61 $collection->addCollection($this->fixtures->contentRouteCollection());
63 $request = Request::create('path/two', 'POST');
64 $request->headers->set('Content-type', 'application/json');
65 $routes = $this->matcher->filter($collection, $request);
66 $this->assertEquals(count($routes), 6, 'The correct number of routes was found.');
67 $this->assertNotNull($routes->get('route_f'), 'The json route was found.');
68 $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
69 foreach ($routes as $name => $route) {
70 $this->assertEquals($name, 'route_f', 'The json route is the first one in the collection.');
76 * Tests route filtering on POST form submission requests.
78 public function testPostForm() {
79 $collection = $this->fixtures->sampleRouteCollection();
80 $collection->addCollection($this->fixtures->contentRouteCollection());
82 // Test that all XML and JSON restricted routes get filtered out on a POST
84 $request = Request::create('path/two', 'POST');
85 $request->headers->set('Content-type', 'application/www-form-urlencoded');
86 $routes = $this->matcher->filter($collection, $request);
87 $this->assertEquals(count($routes), 5, 'The correct number of routes was found.');
88 $this->assertNull($routes->get('route_f'), 'The json route was found.');
89 $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
93 * Confirms that the matcher throws an exception for no-route.
97 public function testNoRouteFound() {
98 $matcher = new ContentTypeHeaderMatcher();
100 $routes = $this->fixtures->contentRouteCollection();
101 $request = Request::create('path/two', 'POST');
102 $request->headers->set('Content-type', 'application/hal+json');
103 $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No route found that matches "Content-Type: application/hal+json"');
104 $matcher->filter($routes, $request);
108 * Confirms that the matcher throws an exception for missing request header.
112 public function testContentTypeRequestHeaderMissing() {
113 $matcher = new ContentTypeHeaderMatcher();
115 $routes = $this->fixtures->contentRouteCollection();
116 $request = Request::create('path/two', 'POST');
117 // Delete all request headers that Request::create() sets by default.
118 $request->headers = new ParameterBag();
119 $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No "Content-Type" request header specified');
120 $matcher->filter($routes, $request);