From 2fe5abb3760a1729eb859a9e9127faf0466c5743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 1 Jul 2019 15:37:53 +0200 Subject: [PATCH] A faulty Content-Type should return the 415 status code, not 406 --- features/security/validate_incoming_content-types.feature | 2 +- src/EventListener/DeserializeListener.php | 5 +++-- tests/EventListener/DeserializeListenerTest.php | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/features/security/validate_incoming_content-types.feature b/features/security/validate_incoming_content-types.feature index 71d981c089f..29579b1ef3e 100644 --- a/features/security/validate_incoming_content-types.feature +++ b/features/security/validate_incoming_content-types.feature @@ -11,6 +11,6 @@ Feature: Validate incoming content type """ something """ - Then the response status code should be 406 + Then the response status code should be 415 And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" And the JSON node "hydra:description" should be equal to 'The content-type "text/plain" is not supported. Supported MIME types are "application/ld+json", "application/hal+json", "application/vnd.api+json", "application/xml", "text/xml", "application/json", "text/html".' diff --git a/src/EventListener/DeserializeListener.php b/src/EventListener/DeserializeListener.php index 2eecf7682db..6717d76ff20 100644 --- a/src/EventListener/DeserializeListener.php +++ b/src/EventListener/DeserializeListener.php @@ -23,6 +23,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; +use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\SerializerInterface; @@ -113,7 +114,7 @@ private function getFormat(Request $request): string */ $contentType = $request->headers->get('CONTENT_TYPE'); if (null === $contentType) { - throw new NotAcceptableHttpException('The "Content-Type" header must exist.'); + throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.'); } $format = $this->formatMatcher->getFormat($contentType); @@ -125,7 +126,7 @@ private function getFormat(Request $request): string } } - throw new NotAcceptableHttpException(sprintf( + throw new UnsupportedMediaTypeHttpException(sprintf( 'The content-type "%s" is not supported. Supported MIME types are "%s".', $contentType, implode('", "', $supportedMimeTypes) diff --git a/tests/EventListener/DeserializeListenerTest.php b/tests/EventListener/DeserializeListenerTest.php index 16426e65798..516661ff26a 100644 --- a/tests/EventListener/DeserializeListenerTest.php +++ b/tests/EventListener/DeserializeListenerTest.php @@ -23,7 +23,7 @@ use Prophecy\Argument; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; +use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\SerializerInterface; @@ -227,7 +227,7 @@ public function testContentNegotiation() public function testNotSupportedContentType() { - $this->expectException(NotAcceptableHttpException::class); + $this->expectException(UnsupportedMediaTypeHttpException::class); $this->expectExceptionMessage('The content-type "application/rdf+xml" is not supported. Supported MIME types are "application/ld+json", "text/xml".'); $eventProphecy = $this->prophesize(GetResponseEvent::class); @@ -257,7 +257,7 @@ public function testNotSupportedContentType() public function testNoContentType() { - $this->expectException(NotAcceptableHttpException::class); + $this->expectException(UnsupportedMediaTypeHttpException::class); $this->expectExceptionMessage('The "Content-Type" header must exist.'); $eventProphecy = $this->prophesize(GetResponseEvent::class);