Light, fluffy, and always free - The AWS Local Emulator alternative
fix(sqs): honor MaximumMessageSize attribute on SendMessage (#782)
Stuart Langec9631e8c521
diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.javaindex 0f0b149db8..9596c464ae 100644--- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java@@ -355,9 +355,12 @@ public class SqsService {.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue","The specified queue does not exist.", 400));- if (body.length() > maxMessageSize) {+ int queueMaxMessageSize = parseMaxMessageSize(queue.getAttributes().get("MaximumMessageSize"));+ int totalSize = computeMessageSize(body, messageAttributes);+ if (totalSize > queueMaxMessageSize) {throw new AwsException("InvalidParameterValue",- "Message body must be shorter than " + maxMessageSize + " bytes.", 400);+ "One or more parameters are invalid. " ++ "Reason: Message must be shorter than " + queueMaxMessageSize + " bytes.", 400);}int queueDelaySeconds = parseDelaySecondsAttribute(queue.getAttributes().get("DelaySeconds"));@@ -446,6 +449,41 @@ public class SqsService {return message;}+ private int parseMaxMessageSize(String value) {+ if (value == null || value.isEmpty()) {+ return maxMessageSize;+ }+ try {+ return Math.min(1048576, Math.max(1024, Integer.parseInt(value)));+ } catch (NumberFormatException ignored) {+ return maxMessageSize;+ }+ }++ /**+ * Total wire-level message size, in bytes, matching AWS SQS accounting:+ * UTF-8 body bytes + per-attribute (name UTF-8 + type UTF-8 + value bytes).+ */+ private static int computeMessageSize(String body, Map<String, MessageAttributeValue> attributes) {+ int total = body == null ? 0 : body.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;+ if (attributes == null || attributes.isEmpty()) {+ return total;+ }+ for (Map.Entry<String, MessageAttributeValue> entry : attributes.entrySet()) {+ total += entry.getKey().getBytes(java.nio.charset.StandardCharsets.UTF_8).length;+ MessageAttributeValue value = entry.getValue();+ if (value.getDataType() != null) {+ total += value.getDataType().getBytes(java.nio.charset.StandardCharsets.UTF_8).length;+ }+ if (value.getBinaryValue() != null) {+ total += value.getBinaryValue().length;+ } else if (value.getStringValue() != null) {+ total += value.getStringValue().getBytes(java.nio.charset.StandardCharsets.UTF_8).length;+ }+ }+ return total;+ }+/*** Parse the queue-level DelaySeconds attribute. Returns 0 when the* attribute is null, empty, non-numeric, or negative -- the queue fallsdiff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.javaindex 4fa70e297b..c0017dd929 100644--- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.services.sns.SnsService;import io.github.hectorvent.floci.services.sqs.model.Message;+import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;import io.github.hectorvent.floci.services.sqs.model.Queue;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;@@ -439,6 +440,41 @@ class SqsServiceTest {"Dedup store must be empty after purge with clearFifoDeduplicationCacheOnPurge=true");}+ @Test+ void sendMessage_usesQueueMaximumMessageSizeAttribute() {+ Queue queue = sqsService.createQueue("big-queue",+ Map.of("MaximumMessageSize", "524288"));+ String body = "x".repeat(300_000);++ assertDoesNotThrow(() -> sqsService.sendMessage(queue.getQueueUrl(), body, 0),+ "Body within the queue's MaximumMessageSize must be accepted");+ }++ @Test+ void sendMessage_oversize_errorReportsQueueLimit() {+ Queue queue = sqsService.createQueue("limited-queue",+ Map.of("MaximumMessageSize", "2048"));+ String oversized = "x".repeat(3000);++ AwsException ex = assertThrows(AwsException.class,+ () -> sqsService.sendMessage(queue.getQueueUrl(), oversized, 0));+ assertTrue(ex.getMessage().contains("2048"),+ "Error message must reference the queue's configured MaximumMessageSize, got: " + ex.getMessage());+ }++ @Test+ void sendMessage_attributesCountTowardsLimit() {+ Queue queue = sqsService.createQueue("attr-limit-queue",+ Map.of("MaximumMessageSize", "2048"));+ String body = "x".repeat(2040);+ Map<String, MessageAttributeValue> attrs = Map.of(+ "key", new MessageAttributeValue("value", "String"));++ // Body alone fits; body + attributes exceed the 2048 byte limit.+ assertThrows(AwsException.class,+ () -> sqsService.sendMessage(queue.getQueueUrl(), body, 0, null, null, attrs, "us-east-1"));+ }+@Testvoid purgeQueueWithClearFifoDelegatesToSnsForFifoDedupOnSubscribedTopics() {final var sns = mock(SnsService.class);