diff --git a/moto/sns/models.py b/moto/sns/models.py index ebdf5cd16..41e83aba4 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -24,6 +24,7 @@ from .utils import make_arn_for_topic, make_arn_for_subscription DEFAULT_ACCOUNT_ID = 123456789012 DEFAULT_PAGE_SIZE = 100 +MAXIMUM_MESSAGE_LENGTH = 262144 # 256 KiB class Topic(BaseModel): @@ -327,6 +328,9 @@ class SNSBackend(BaseBackend): # Note that the AWS docs around length are wrong: https://github.com/spulec/moto/issues/1503 raise ValueError('Subject must be less than 100 characters') + if len(message) > MAXIMUM_MESSAGE_LENGTH: + raise InvalidParameterValue("An error occurred (InvalidParameter) when calling the Publish operation: Invalid parameter: Message too long") + try: topic = self.get_topic(arn) message_id = topic.publish(message, subject=subject, diff --git a/tests/test_sns/test_publishing_boto3.py b/tests/test_sns/test_publishing_boto3.py index 65d2f25cc..3d598d406 100644 --- a/tests/test_sns/test_publishing_boto3.py +++ b/tests/test_sns/test_publishing_boto3.py @@ -10,6 +10,7 @@ import sure # noqa import responses from botocore.exceptions import ClientError +from nose.tools import assert_raises from moto import mock_sns, mock_sqs @@ -308,6 +309,20 @@ def test_publish_subject(): raise RuntimeError('Should have raised an InvalidParameter exception') +@mock_sns +def test_publish_message_too_long(): + sns = boto3.resource('sns', region_name='us-east-1') + topic = sns.create_topic(Name='some-topic') + + with assert_raises(ClientError): + topic.publish( + Message="".join(["." for i in range(0, 262145)])) + + # message short enough - does not raise an error + topic.publish( + Message="".join(["." for i in range(0, 262144)])) + + def _setup_filter_policy_test(filter_policy): sns = boto3.resource('sns', region_name='us-east-1') topic = sns.create_topic(Name='some-topic')