Light, fluffy, and always free - The AWS Local Emulator alternative
fix(sns): emit canonical error codes for Publish size violations (#906)
Stuart Langb65b7aa07ff0
diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.javaindex ac20180ba1..d32d6736e3 100644--- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java@@ -296,9 +296,10 @@ public class SnsService {public String publish(String topicArn, String targetArn, String phoneNumber, String message,String subject, Map<String, MessageAttributeValue> messageAttributes,String messageGroupId, String messageDeduplicationId, String region) {- int payloadSize = computePublishSize(message, subject, messageAttributes);+ int messageBytes = message == null ? 0 : message.getBytes(StandardCharsets.UTF_8).length;+ int payloadSize = computePublishSize(messageBytes, subject, messageAttributes);if (payloadSize > MAX_PUBLISH_SIZE) {- throw new AwsException("InvalidParameterException",+ throw new AwsException("InvalidParameter","Invalid parameter: Message too long", 400);}@@ -398,7 +399,8 @@ public class SnsService {@SuppressWarnings("unchecked")Map<String, MessageAttributeValue> attrs =(Map<String, MessageAttributeValue>) entry.get("MessageAttributes");- batchSize += computePublishSize(message, subject, attrs);+ int entryMessageBytes = message == null ? 0 : message.getBytes(StandardCharsets.UTF_8).length;+ batchSize += computePublishSize(entryMessageBytes, subject, attrs);}if (batchSize > MAX_PUBLISH_SIZE) {throw new AwsException("BatchRequestTooLong",@@ -1103,9 +1105,9 @@ public class SnsService {return AwsArnUtils.arnToQueueUrl(arn, baseUrl);}- private static int computePublishSize(String message, String subject,+ private static int computePublishSize(int messageBytes, String subject,Map<String, MessageAttributeValue> attributes) {- int total = message == null ? 0 : message.getBytes(StandardCharsets.UTF_8).length;+ int total = messageBytes;if (subject != null) {total += subject.getBytes(StandardCharsets.UTF_8).length;}diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.javaindex 1c78e04671..71f146c7fd 100644--- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java@@ -223,17 +223,17 @@ class SnsServiceTest {}@Test- void publish_messageExceedsSizeLimit_throwsInvalidParameterException() {+ void publish_messageExceedsSizeLimit_throwsInvalidParameter() {Topic topic = snsService.createTopic("size-topic", null, null, REGION);String tooBig = "x".repeat(262_145);AwsException ex = assertThrows(AwsException.class, () ->snsService.publish(topic.getTopicArn(), null, tooBig, null, REGION));- assertEquals("InvalidParameterException", ex.getErrorCode());+ assertEquals("InvalidParameter", ex.getErrorCode());assertTrue(ex.getMessage().toLowerCase().contains("too long"));}@Test- void publish_messagePlusAttributesExceedsLimit_throws() {+ void publish_messagePlusAttributesExceedsLimit_throwsInvalidParameter() {Topic topic = snsService.createTopic("size-topic", null, null, REGION);String body = "x".repeat(262_100);Map<String, io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue> attrs = Map.of(@@ -241,7 +241,8 @@ class SnsServiceTest {"y".repeat(200), "String"));AwsException ex = assertThrows(AwsException.class, () ->snsService.publish(topic.getTopicArn(), null, null, body, null, attrs, REGION));- assertEquals("InvalidParameterException", ex.getErrorCode());+ assertEquals("InvalidParameter", ex.getErrorCode());+ assertTrue(ex.getMessage().toLowerCase().contains("too long"));}@Test