Skip to content

Commit 2fe5abb

Browse files
committed
A faulty Content-Type should return the 415 status code, not 406
1 parent d46ac32 commit 2fe5abb

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

features/security/validate_incoming_content-types.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Feature: Validate incoming content type
1111
"""
1212
something
1313
"""
14-
Then the response status code should be 406
14+
Then the response status code should be 415
1515
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
1616
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".'

src/EventListener/DeserializeListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\HttpFoundation\Request;
2424
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
2525
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
26+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
2627
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2728
use Symfony\Component\Serializer\SerializerInterface;
2829

@@ -113,7 +114,7 @@ private function getFormat(Request $request): string
113114
*/
114115
$contentType = $request->headers->get('CONTENT_TYPE');
115116
if (null === $contentType) {
116-
throw new NotAcceptableHttpException('The "Content-Type" header must exist.');
117+
throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.');
117118
}
118119

119120
$format = $this->formatMatcher->getFormat($contentType);
@@ -125,7 +126,7 @@ private function getFormat(Request $request): string
125126
}
126127
}
127128

128-
throw new NotAcceptableHttpException(sprintf(
129+
throw new UnsupportedMediaTypeHttpException(sprintf(
129130
'The content-type "%s" is not supported. Supported MIME types are "%s".',
130131
$contentType,
131132
implode('", "', $supportedMimeTypes)

tests/EventListener/DeserializeListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Prophecy\Argument;
2424
use Symfony\Component\HttpFoundation\Request;
2525
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
26-
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
26+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
2727
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2828
use Symfony\Component\Serializer\SerializerInterface;
2929

@@ -227,7 +227,7 @@ public function testContentNegotiation()
227227

228228
public function testNotSupportedContentType()
229229
{
230-
$this->expectException(NotAcceptableHttpException::class);
230+
$this->expectException(UnsupportedMediaTypeHttpException::class);
231231
$this->expectExceptionMessage('The content-type "application/rdf+xml" is not supported. Supported MIME types are "application/ld+json", "text/xml".');
232232

233233
$eventProphecy = $this->prophesize(GetResponseEvent::class);
@@ -257,7 +257,7 @@ public function testNotSupportedContentType()
257257

258258
public function testNoContentType()
259259
{
260-
$this->expectException(NotAcceptableHttpException::class);
260+
$this->expectException(UnsupportedMediaTypeHttpException::class);
261261
$this->expectExceptionMessage('The "Content-Type" header must exist.');
262262

263263
$eventProphecy = $this->prophesize(GetResponseEvent::class);

0 commit comments

Comments
 (0)