class AcceptHeaderMatcherTest in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest
- 9 core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest
Confirm that the mime types partial matcher is functioning properly.
@group Routing
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest
Expanded class hierarchy of AcceptHeaderMatcherTest
File
- core/
tests/ Drupal/ Tests/ Core/ Routing/ AcceptHeaderMatcherTest.php, line 15
Namespace
Drupal\Tests\Core\RoutingView source
class AcceptHeaderMatcherTest extends UnitTestCase {
/**
* A collection of shared fixture data for tests.
*
* @var \Drupal\Tests\Core\Routing\RoutingFixtures
*/
protected $fixtures;
/**
* The matcher object that is going to be tested.
*
* @var \Drupal\accept_header_routing_test\Routing\AcceptHeaderMatcher
*/
protected $matcher;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->fixtures = new RoutingFixtures();
$this->matcher = new AcceptHeaderMatcher();
}
/**
* Provides data for the Accept header filtering test.
*
* @see Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest::testAcceptFiltering()
*/
public function acceptFilterProvider() {
return [
// Check that JSON routes get filtered and prioritized correctly.
[
'application/json, text/xml;q=0.9',
'json',
'route_c',
'route_e',
],
// Tests a JSON request with alternative JSON MIME type Accept header.
[
'application/x-json, text/xml;q=0.9',
'json',
'route_c',
'route_e',
],
// Tests a standard HTML request.
[
'text/html, text/xml;q=0.9',
'html',
'route_e',
'route_c',
],
];
}
/**
* Tests that requests using Accept headers get filtered correctly.
*
* @param string $accept_header
* The HTTP Accept header value of the request.
* @param string $format
* The request format.
* @param string $included_route
* The route name that should survive the filter and be ranked first.
* @param string $excluded_route
* The route name that should be filtered out during matching.
*
* @dataProvider acceptFilterProvider
*/
public function testAcceptFiltering($accept_header, $format, $included_route, $excluded_route) {
$collection = $this->fixtures
->sampleRouteCollection();
$request = Request::create('path/two', 'GET');
$request->headers
->set('Accept', $accept_header);
$request
->setRequestFormat($format);
$routes = $this->matcher
->filter($collection, $request);
$this
->assertCount(4, $routes, 'The correct number of routes was found.');
$this
->assertNotNull($routes
->get($included_route), "Route {$included_route} was found when matching {$accept_header}.");
$this
->assertNull($routes
->get($excluded_route), "Route {$excluded_route} was not found when matching {$accept_header}.");
foreach ($routes as $name => $route) {
$this
->assertEquals($name, $included_route, "Route {$included_route} is the first one in the collection when matching {$accept_header}.");
break;
}
}
/**
* Confirms that the AcceptHeaderMatcher throws an exception for no-route.
*/
public function testNoRouteFound() {
// Remove the sample routes that would match any method.
$routes = $this->fixtures
->sampleRouteCollection();
$routes
->remove('route_a');
$routes
->remove('route_b');
$routes
->remove('route_c');
$routes
->remove('route_d');
$request = Request::create('path/two', 'GET');
$request->headers
->set('Accept', 'application/json, text/xml;q=0.9');
$request
->setRequestFormat('json');
$this
->expectException(NotAcceptableHttpException::class);
$this
->expectExceptionMessage('No route found for the specified formats application/json text/xml');
$this->matcher
->filter($routes, $request);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AcceptHeaderMatcherTest:: |
protected | property | A collection of shared fixture data for tests. | |
AcceptHeaderMatcherTest:: |
protected | property | The matcher object that is going to be tested. | |
AcceptHeaderMatcherTest:: |
public | function | Provides data for the Accept header filtering test. | |
AcceptHeaderMatcherTest:: |
protected | function |
Overrides UnitTestCase:: |
|
AcceptHeaderMatcherTest:: |
public | function | Tests that requests using Accept headers get filtered correctly. | |
AcceptHeaderMatcherTest:: |
public | function | Confirms that the AcceptHeaderMatcher throws an exception for no-route. | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |