Skip to content
Snippets Groups Projects
Commit 97f0d43d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2811133 by Wim Leers, neclimdul, dawehner: Confusing error response set...

Issue #2811133 by Wim Leers, neclimdul, dawehner: Confusing error response set by ContentTypeHeaderMatcher when forgetting the Content-Type request header
parent 74b8e7ad
No related branches found
No related tags found
No related merge requests found
......@@ -42,7 +42,12 @@ public function filter(RouteCollection $collection, Request $request) {
// 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') . '"');
}
}
/**
......
......@@ -4,12 +4,16 @@
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 @@ public function testPostForm() {
/**
* 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 @@ public function testNoRouteFound() {
$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.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment