diff --git a/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php b/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php index e7efcdd67b2..13e18f35a94 100644 --- a/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php +++ b/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php @@ -42,7 +42,12 @@ class ContentTypeHeaderMatcher implements RouteFilterInterface { // We do not throw a // \Symfony\Component\Routing\Exception\ResourceNotFoundException here // because we don't want to return a 404 status code, but rather a 415. - throw new UnsupportedMediaTypeHttpException('No route found that matches "Content-Type: ' . $request->headers->get('Content-Type') . '"'); + if (!$request->headers->has('Content-Type')) { + throw new UnsupportedMediaTypeHttpException('No "Content-Type" request header specified'); + } + else { + throw new UnsupportedMediaTypeHttpException('No route found that matches "Content-Type: ' . $request->headers->get('Content-Type') . '"'); + } } /** diff --git a/core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php b/core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php index ec978388698..13a9048f500 100644 --- a/core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php @@ -4,12 +4,16 @@ 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; /** * Confirm that the content types partial matcher is functioning properly. * * @group Routing + * + * @coversDefaultClass \Drupal\Core\Routing\ContentTypeHeaderMatcher */ class ContentTypeHeaderMatcherTest extends UnitTestCase { @@ -88,8 +92,7 @@ class ContentTypeHeaderMatcherTest extends UnitTestCase { /** * Confirms that the matcher throws an exception for no-route. * - * @expectedException \Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException - * @expectedExceptionMessage No route found that matches "Content-Type: application/hal+json" + * @covers ::filter */ public function testNoRouteFound() { $matcher = new ContentTypeHeaderMatcher(); @@ -97,8 +100,24 @@ class ContentTypeHeaderMatcherTest extends UnitTestCase { $routes = $this->fixtures->contentRouteCollection(); $request = Request::create('path/two', 'POST'); $request->headers->set('Content-type', 'application/hal+json'); + $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No route found that matches "Content-Type: application/hal+json"'); + $matcher->filter($routes, $request); + } + + /** + * Confirms that the matcher throws an exception for missing request header. + * + * @covers ::filter + */ + public function testContentTypeRequestHeaderMissing() { + $matcher = new ContentTypeHeaderMatcher(); + + $routes = $this->fixtures->contentRouteCollection(); + $request = Request::create('path/two', 'POST'); + // Delete all request headers that Request::create() sets by default. + $request->headers = new ParameterBag(); + $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No "Content-Type" request header specified.'); $matcher->filter($routes, $request); - $this->fail('No exception was thrown.'); } }