Skip to content

Commit

Permalink
Merge pull request #3 from onatskyy/read_base64
Browse files Browse the repository at this point in the history
added ability read encoded Headers
  • Loading branch information
onatskyy authored Sep 15, 2023
2 parents 58c2974 + 76f2605 commit 23bf7e4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions SnsQsConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ private function convertMessage(SqsMessage $sqsMessage): SnsQsMessage
if (isset($data['Message'])) {
$message->setBody((string) $data['Message']);
}

if (isset($data['MessageAttributes']['Headers'])) {
$headersData = json_decode($data['MessageAttributes']['Headers']['Value'], true);
$rawHeaderData = $data['MessageAttributes']['Headers']['Value'];
if (isset($rawHeaderData[0]) && '[' !== $rawHeaderData[0]) {
$rawHeaderData = base64_decode($rawHeaderData);
}
$headersData = json_decode($rawHeaderData, true);

$message->setHeaders($headersData[0]);
$message->setProperties($headersData[1]);
Expand Down
31 changes: 31 additions & 0 deletions Tests/SnsQsConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@
final class SnsQsConsumerTest extends TestCase
{
public function testReceivesSnsMessage(): void
{
$context = $this->createMock(SnsQsContext::class);
$context->expects($this->once())
->method('createMessage')
->willReturn(new SnsQsMessage());

$sqsConsumer = $this->createMock(SqsConsumer::class);
$sqsConsumer->expects($this->once())
->method('receive')
->willReturn(new SqsMessage(json_encode([
'Type' => 'Notification',
'TopicArn' => 'arn:aws:sns:us-east-2:12345:topic-name',
'Message' => 'The Body',
'MessageAttributes' => [
'Headers' => [
'Type' => 'String',
'Value' => base64_encode('[{"headerKey":"headerVal"},{"propKey": "propVal"}]'),
],
],
])));

$consumer = new SnsQsConsumer($context, $sqsConsumer, new SnsQsQueue('queue'));
$result = $consumer->receive();

$this->assertInstanceOf(SnsQsMessage::class, $result);
$this->assertSame('The Body', $result->getBody());
$this->assertSame(['headerKey' => 'headerVal'], $result->getHeaders());
$this->assertSame(['propKey' => 'propVal'], $result->getProperties());
}

public function testReceivesSnsMessageWithUnencodedHeaders(): void
{
$context = $this->createMock(SnsQsContext::class);
$context->expects($this->once())
Expand Down

0 comments on commit 23bf7e4

Please sign in to comment.