From af37c58d29d852e2d8326bd5d4621179c3b6fd3d Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 7 May 2024 11:00:46 +0200 Subject: [PATCH] [Messenger] Comply with Amazon SQS requirements for message body --- Tests/Transport/AmazonSqsSenderTest.php | 15 +++++++++++++++ Transport/AmazonSqsSender.php | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/Tests/Transport/AmazonSqsSenderTest.php b/Tests/Transport/AmazonSqsSenderTest.php index 80840c8..d11a5d8 100644 --- a/Tests/Transport/AmazonSqsSenderTest.php +++ b/Tests/Transport/AmazonSqsSenderTest.php @@ -72,4 +72,19 @@ public function testSendWithAmazonSqsXrayTraceHeaderStamp() $sender = new AmazonSqsSender($connection, $serializer); $sender->send($envelope); } + + public function testSendEncodeBodyToRespectAmazonRequirements() + { + $envelope = new Envelope(new DummyMessage('Oy')); + $encoded = ['body' => "\x7", 'headers' => ['type' => DummyMessage::class]]; + + $connection = $this->createMock(Connection::class); + $connection->expects($this->once())->method('send')->with(base64_encode($encoded['body']), $encoded['headers']); + + $serializer = $this->createMock(SerializerInterface::class); + $serializer->method('encode')->with($envelope)->willReturn($encoded); + + $sender = new AmazonSqsSender($connection, $serializer); + $sender->send($envelope); + } } diff --git a/Transport/AmazonSqsSender.php b/Transport/AmazonSqsSender.php index 1994313..b253c82 100644 --- a/Transport/AmazonSqsSender.php +++ b/Transport/AmazonSqsSender.php @@ -38,6 +38,7 @@ public function __construct(Connection $connection, SerializerInterface $seriali public function send(Envelope $envelope): Envelope { $encodedMessage = $this->serializer->encode($envelope); + $encodedMessage = $this->complyWithAmazonSqsRequirements($encodedMessage); /** @var DelayStamp|null $delayStamp */ $delayStamp = $envelope->last(DelayStamp::class); @@ -75,4 +76,20 @@ public function send(Envelope $envelope): Envelope return $envelope; } + + /** + * @see https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html + * + * @param array{body: string, headers?: array} $encodedMessage + * + * @return array{body: string, headers?: array} + */ + private function complyWithAmazonSqsRequirements(array $encodedMessage): array + { + if (preg_match('/[^\x20-\x{D7FF}\xA\xD\x9\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u', $encodedMessage['body'])) { + $encodedMessage['body'] = base64_encode($encodedMessage['body']); + } + + return $encodedMessage; + } }