Changing to MessageDecodingFailedException so that invalid messages are rejected
This commit is contained in:
parent
88042a317a
commit
4be827d3ca
@ -28,6 +28,16 @@ class PhpSerializerTest extends TestCase
|
||||
$this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope)));
|
||||
}
|
||||
|
||||
public function testDecodingFailsWithMissingBodyKey()
|
||||
{
|
||||
$this->expectException(MessageDecodingFailedException::class);
|
||||
$this->expectExceptionMessage('Encoded envelope should have at least a "body".');
|
||||
|
||||
$serializer = new PhpSerializer();
|
||||
|
||||
$serializer->decode([]);
|
||||
}
|
||||
|
||||
public function testDecodingFailsWithBadFormat()
|
||||
{
|
||||
$this->expectException(MessageDecodingFailedException::class);
|
||||
|
@ -108,6 +108,37 @@ class SerializerTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getMissingKeyTests
|
||||
*/
|
||||
public function testDecodingFailsWithMissingKeys(array $data, string $expectedMessage)
|
||||
{
|
||||
$this->expectException(MessageDecodingFailedException::class);
|
||||
$this->expectExceptionMessage($expectedMessage);
|
||||
|
||||
$serializer = new Serializer();
|
||||
|
||||
$serializer->decode($data);
|
||||
}
|
||||
|
||||
public function getMissingKeyTests()
|
||||
{
|
||||
yield 'no_body' => [
|
||||
['headers' => ['type' => 'bar']],
|
||||
'Encoded envelope should have at least a "body" and some "headers".',
|
||||
];
|
||||
|
||||
yield 'no_headers' => [
|
||||
['body' => '{}'],
|
||||
'Encoded envelope should have at least a "body" and some "headers".',
|
||||
];
|
||||
|
||||
yield 'no_headers_type' => [
|
||||
['body' => '{}', 'headers' => ['foo' => 'bar']],
|
||||
'Encoded envelope does not have a "type" header.',
|
||||
];
|
||||
}
|
||||
|
||||
public function testDecodingFailsWithBadClass()
|
||||
{
|
||||
$this->expectException(MessageDecodingFailedException::class);
|
||||
|
@ -12,7 +12,6 @@
|
||||
namespace Symfony\Component\Messenger\Transport\Serialization;
|
||||
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
|
||||
|
||||
/**
|
||||
@ -28,7 +27,7 @@ class PhpSerializer implements SerializerInterface
|
||||
public function decode(array $encodedEnvelope): Envelope
|
||||
{
|
||||
if (empty($encodedEnvelope['body'])) {
|
||||
throw new InvalidArgumentException('Encoded envelope should have at least a "body".');
|
||||
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
|
||||
}
|
||||
|
||||
return $this->safelyUnserialize($encodedEnvelope['body']);
|
||||
|
@ -12,7 +12,6 @@
|
||||
namespace Symfony\Component\Messenger\Transport\Serialization;
|
||||
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Messenger\Exception\LogicException;
|
||||
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
|
||||
use Symfony\Component\Messenger\Stamp\SerializerStamp;
|
||||
@ -63,11 +62,11 @@ class Serializer implements SerializerInterface
|
||||
public function decode(array $encodedEnvelope): Envelope
|
||||
{
|
||||
if (empty($encodedEnvelope['body']) || empty($encodedEnvelope['headers'])) {
|
||||
throw new InvalidArgumentException('Encoded envelope should have at least a "body" and some "headers".');
|
||||
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body" and some "headers".');
|
||||
}
|
||||
|
||||
if (empty($encodedEnvelope['headers']['type'])) {
|
||||
throw new InvalidArgumentException('Encoded envelope does not have a "type" header.');
|
||||
throw new MessageDecodingFailedException('Encoded envelope does not have a "type" header.');
|
||||
}
|
||||
|
||||
$stamps = $this->decodeStamps($encodedEnvelope);
|
||||
|
Reference in New Issue
Block a user