|
28 | 28 | use Symfony\Component\HttpFoundation\Request; |
29 | 29 | use Symfony\Component\HttpKernel\Event\RequestEvent; |
30 | 30 | use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; |
| 31 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
31 | 32 | use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
32 | 33 | use Symfony\Component\Serializer\Exception\PartialDenormalizationException; |
33 | 34 | use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
@@ -369,4 +370,56 @@ public function testTurnPartialDenormalizationExceptionIntoValidationException() |
369 | 370 | $this->assertSame($violation->getCode(), 'ba785a8c-82cb-4283-967c-3cf342181b40'); |
370 | 371 | } |
371 | 372 | } |
| 373 | + |
| 374 | + public function testRequestWithEmptyContentType(): void |
| 375 | + { |
| 376 | + $serializerProphecy = $this->prophesize(SerializerInterface::class); |
| 377 | + $serializerProphecy->deserialize(Argument::cetera())->shouldNotBeCalled(); |
| 378 | + |
| 379 | + $serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class); |
| 380 | + $serializerContextBuilderProphecy->createFromRequest(Argument::cetera())->willReturn([]); |
| 381 | + |
| 382 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 383 | + $resourceMetadataFactoryProphecy->create(Argument::cetera())->willReturn(new ResourceMetadataCollection(Dummy::class, [ |
| 384 | + new ApiResource(operations: [ |
| 385 | + 'post' => new Post(inputFormats: self::FORMATS), |
| 386 | + ]), |
| 387 | + ]))->shouldBeCalled(); |
| 388 | + |
| 389 | + $listener = new DeserializeListener( |
| 390 | + $serializerProphecy->reveal(), |
| 391 | + $serializerContextBuilderProphecy->reveal(), |
| 392 | + $resourceMetadataFactoryProphecy->reveal() |
| 393 | + ); |
| 394 | + |
| 395 | + // in Symfony (at least up to 7.0.2, 6.4.2, 6.3.11, 5.4.34), a request |
| 396 | + // without a content-type and content-length header will result in the |
| 397 | + // variables set to an empty string, not null |
| 398 | + |
| 399 | + $request = new Request( |
| 400 | + server: [ |
| 401 | + 'REQUEST_METHOD' => 'POST', |
| 402 | + 'REQUEST_URI' => '/', |
| 403 | + 'CONTENT_TYPE' => '', |
| 404 | + 'CONTENT_LENGTH' => '', |
| 405 | + ], |
| 406 | + attributes: [ |
| 407 | + '_api_resource_class' => Dummy::class, |
| 408 | + '_api_operation_name' => 'post', |
| 409 | + '_api_receive' => true, |
| 410 | + ], |
| 411 | + content: '' |
| 412 | + ); |
| 413 | + |
| 414 | + $event = new RequestEvent( |
| 415 | + $this->prophesize(HttpKernelInterface::class)->reveal(), |
| 416 | + $request, |
| 417 | + \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, |
| 418 | + ); |
| 419 | + |
| 420 | + $this->expectException(UnsupportedMediaTypeHttpException::class); |
| 421 | + $this->expectExceptionMessage('The "Content-Type" header must exist.'); |
| 422 | + |
| 423 | + $listener->onKernelRequest($event); |
| 424 | + } |
372 | 425 | } |
0 commit comments