8000 bug #54920 [Messenger] Comply with Amazon SQS requirements for messag… · symfony/symfony@4d0aeed · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d0aeed

Browse files
committed
bug #54920 [Messenger] Comply with Amazon SQS requirements for message body (VincentLanglet)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Messenger] Comply with Amazon SQS requirements for message body | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #49844 | License | MIT Commits ------- 0d441bf [Messenger] Comply with Amazon SQS requirements for message body
2 parents ab461c5 + 0d441bf commit 4d0aeed

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsSenderTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,19 @@ public function testSendWithAmazonSqsXrayTraceHeaderStamp()
7272
$sender = new AmazonSqsSender($connection, $serializer);
7373
$sender->send($envelope);
7474
}
75+
76+
public function testSendEncodeBodyToRespectAmazonRequirements()
77+
{
78+
$envelope = new Envelope(new DummyMessage('Oy'));
79+
$encoded = ['body' => "\x7", 'headers' => ['type' => DummyMessage::class]];
80+
81+
$connection = $this->createMock(Connection::class);
82+
$connection->expects($this->once())->method('send')->with(base64_encode($encoded['body']), $encoded['headers']);
83+
84+
$serializer = $this->createMock(SerializerInterface::class);
85+
$serializer->method('encode')->with($envelope)->willReturn($encoded);
86+
87+
$sender = new AmazonSqsSender($connection, $serializer);
88+
$sender->send($envelope);
89+
}
7590
}

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsSender.php

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __construct(Connection $connection, SerializerInterface $seriali
3838
public function send(Envelope $envelope): Envelope
3939
{
4040
$encodedMessage = $this->serializer->encode($envelope);
41+
$encodedMessage = $this->complyWithAmazonSqsRequirements($encodedMessage);
4142

4243
/** @var DelayStamp|null $delayStamp */
4344
$delayStamp = $envelope->last(DelayStamp::class);
@@ -75,4 +76,20 @@ public function send(Envelope $envelope): Envelope
7576

7677
return $envelope;
7778
}
79+
80+
/**
81+
* @see https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html
82+
*
83+
* @param array{body: string, headers?: array<string>} $encodedMessage
84+
*
85+
* @return array{body: string, headers?: array<string>}
86+
*/
87+
private function complyWithAmazonSqsRequirements(array $encodedMessage): array
88+
{
89+
if (preg_match('/[^\x20-\x{D7FF}\xA\xD\x9\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u', $encodedMessage['body'])) {
90+
$encodedMessage['body'] = base64_encode($encodedMessage['body']);
91+
}
92+
93+
return $encodedMessage;
94+
}
7895
}

0 commit comments

Comments
 (0)
0