From 0b173853c7c0a276e57b43b79ddb996e803fd139 Mon Sep 17 00:00:00 2001 From: elazar Date: Sat, 21 Jul 2018 08:51:03 -0500 Subject: [PATCH] Prevent SqsProducer from sending messages with empty bodies Fixes #435 --- pkg/sqs/SqsProducer.php | 4 ++-- pkg/sqs/Tests/SqsProducerTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/sqs/SqsProducer.php b/pkg/sqs/SqsProducer.php index 3acdce608..5be9e48d3 100644 --- a/pkg/sqs/SqsProducer.php +++ b/pkg/sqs/SqsProducer.php @@ -42,11 +42,11 @@ public function send(PsrDestination $destination, PsrMessage $message) InvalidMessageException::assertMessageInstanceOf($message, SqsMessage::class); $body = $message->getBody(); - if (is_scalar($body) || null === $body) { + if (is_scalar($body) && strlen($body) > 0) { $body = (string) $body; } else { throw new InvalidMessageException(sprintf( - 'The message body must be a scalar or null. Got: %s', + 'The message body must be a non-empty string. Got: %s', is_object($body) ? get_class($body) : gettype($body) )); } diff --git a/pkg/sqs/Tests/SqsProducerTest.php b/pkg/sqs/Tests/SqsProducerTest.php index 445b9cef0..2e37c31c6 100644 --- a/pkg/sqs/Tests/SqsProducerTest.php +++ b/pkg/sqs/Tests/SqsProducerTest.php @@ -31,7 +31,7 @@ public function testCouldBeConstructedWithRequiredArguments() public function testShouldThrowIfBodyOfInvalidType() { $this->expectException(InvalidMessageException::class); - $this->expectExceptionMessage('The message body must be a scalar or null. Got: stdClass'); + $this->expectExceptionMessage('The message body must be a non-empty string. Got: stdClass'); $producer = new SqsProducer($this->createSqsContextMock()); @@ -72,7 +72,7 @@ public function testShouldThrowIfSendMessageFailed() ; $destination = new SqsDestination('queue-name'); - $message = new SqsMessage(); + $message = new SqsMessage('foo'); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Message was not sent');