Skip to content

Commit

Permalink
bug #54920 [Messenger] Comply with Amazon SQS requirements for messag…
Browse files Browse the repository at this point in the history
…e 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
-------

0d441bf62a [Messenger] Comply with Amazon SQS requirements for message body
  • Loading branch information
fabpot committed Jun 9, 2024
2 parents 2ca58f3 + af37c58 commit 2d91f57
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Tests/Transport/AmazonSqsSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 17 additions & 0 deletions Transport/AmazonSqsSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<string>} $encodedMessage
*
* @return array{body: string, headers?: array<string>}
*/
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;
}
}

0 comments on commit 2d91f57

Please sign in to comment.