3 namespace Drupal\Tests\Core\Routing;
5 use Drupal\accept_header_routing_test\Routing\AcceptHeaderMatcher;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
11 * Confirm that the mime types partial matcher is functioning properly.
15 class AcceptHeaderMatcherTest extends UnitTestCase {
18 * A collection of shared fixture data for tests.
20 * @var \Drupal\Tests\Core\Routing\RoutingFixtures
25 * The matcher object that is going to be tested.
27 * @var \Drupal\accept_header_routing_test\Routing\AcceptHeaderMatcher
34 protected function setUp() {
37 $this->fixtures = new RoutingFixtures();
38 $this->matcher = new AcceptHeaderMatcher();
42 * Provides data for the Accept header filtering test.
44 * @see Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest::testAcceptFiltering()
46 public function acceptFilterProvider() {
48 // Check that JSON routes get filtered and prioritized correctly.
49 ['application/json, text/xml;q=0.9', 'json', 'route_c', 'route_e'],
50 // Tests a JSON request with alternative JSON MIME type Accept header.
51 ['application/x-json, text/xml;q=0.9', 'json', 'route_c', 'route_e'],
52 // Tests a standard HTML request.
53 ['text/html, text/xml;q=0.9', 'html', 'route_e', 'route_c'],
58 * Tests that requests using Accept headers get filtered correctly.
60 * @param string $accept_header
61 * The HTTP Accept header value of the request.
62 * @param string $format
64 * @param string $included_route
65 * The route name that should survive the filter and be ranked first.
66 * @param string $excluded_route
67 * The route name that should be filtered out during matching.
69 * @dataProvider acceptFilterProvider
71 public function testAcceptFiltering($accept_header, $format, $included_route, $excluded_route) {
72 $collection = $this->fixtures->sampleRouteCollection();
74 $request = Request::create('path/two', 'GET');
75 $request->headers->set('Accept', $accept_header);
76 $request->setRequestFormat($format);
77 $routes = $this->matcher->filter($collection, $request);
78 $this->assertEquals(count($routes), 4, 'The correct number of routes was found.');
79 $this->assertNotNull($routes->get($included_route), "Route $included_route was found when matching $accept_header.");
80 $this->assertNull($routes->get($excluded_route), "Route $excluded_route was not found when matching $accept_header.");
81 foreach ($routes as $name => $route) {
82 $this->assertEquals($name, $included_route, "Route $included_route is the first one in the collection when matching $accept_header.");
88 * Confirms that the AcceptHeaderMatcher throws an exception for no-route.
90 public function testNoRouteFound() {
91 // Remove the sample routes that would match any method.
92 $routes = $this->fixtures->sampleRouteCollection();
93 $routes->remove('route_a');
94 $routes->remove('route_b');
95 $routes->remove('route_c');
96 $routes->remove('route_d');
98 $request = Request::create('path/two', 'GET');
99 $request->headers->set('Accept', 'application/json, text/xml;q=0.9');
100 $request->setRequestFormat('json');
101 $this->setExpectedException(NotAcceptableHttpException::class, 'No route found for the specified formats application/json text/xml');
102 $this->matcher->filter($routes, $request);