View source
<?php
namespace Drupal\Tests\Core\Routing;
use Drupal\Core\Routing\ContentTypeHeaderMatcher;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
class ContentTypeHeaderMatcherTest extends UnitTestCase {
protected $fixtures;
protected $matcher;
protected function setUp() : void {
parent::setUp();
$this->fixtures = new RoutingFixtures();
$this->matcher = new ContentTypeHeaderMatcher();
}
public function testSafeRequestFilter($method) {
$collection = $this->fixtures
->sampleRouteCollection();
$collection
->addCollection($this->fixtures
->contentRouteCollection());
$request = Request::create('path/two', $method);
$routes = $this->matcher
->filter($collection, $request);
$this
->assertCount(7, $routes, 'The correct number of routes was found.');
}
public function providerTestSafeRequestFilter() {
return [
[
'GET',
],
[
'HEAD',
],
[
'OPTIONS',
],
[
'TRACE',
],
[
'DELETE',
],
];
}
public function testJsonRequest() {
$collection = $this->fixtures
->sampleRouteCollection();
$collection
->addCollection($this->fixtures
->contentRouteCollection());
$request = Request::create('path/two', 'POST');
$request->headers
->set('Content-type', 'application/json');
$routes = $this->matcher
->filter($collection, $request);
$this
->assertCount(6, $routes, 'The correct number of routes was found.');
$this
->assertNotNull($routes
->get('route_f'), 'The json route was found.');
$this
->assertNull($routes
->get('route_g'), 'The xml route was not found.');
foreach ($routes as $name => $route) {
$this
->assertEquals('route_f', $name, 'The json route is the first one in the collection.');
break;
}
}
public function testPostForm() {
$collection = $this->fixtures
->sampleRouteCollection();
$collection
->addCollection($this->fixtures
->contentRouteCollection());
$request = Request::create('path/two', 'POST');
$request->headers
->set('Content-type', 'application/www-form-urlencoded');
$routes = $this->matcher
->filter($collection, $request);
$this
->assertCount(5, $routes, 'The correct number of routes was found.');
$this
->assertNull($routes
->get('route_f'), 'The json route was found.');
$this
->assertNull($routes
->get('route_g'), 'The xml route was not found.');
}
public function testNoRouteFound() {
$matcher = new ContentTypeHeaderMatcher();
$routes = $this->fixtures
->contentRouteCollection();
$request = Request::create('path/two', 'POST');
$request->headers
->set('Content-type', 'application/hal+json');
$this
->expectException(UnsupportedMediaTypeHttpException::class);
$this
->expectExceptionMessage('No route found that matches "Content-Type: application/hal+json"');
$matcher
->filter($routes, $request);
}
public function testContentTypeRequestHeaderMissing() {
$matcher = new ContentTypeHeaderMatcher();
$routes = $this->fixtures
->contentRouteCollection();
$request = Request::create('path/two', 'POST');
$request->headers = new ParameterBag();
$this
->expectException(UnsupportedMediaTypeHttpException::class);
$this
->expectExceptionMessage('No "Content-Type" request header specified');
$matcher
->filter($routes, $request);
}
}