Merge pull request #1667 from iainb/enforce-message-size

SNS - Enforce 'Message too long' exception when publishing messages
This commit is contained in:
Steve Pulec 2018-06-08 17:59:10 -04:00 committed by GitHub
commit db4c84c680
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -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,

View File

@ -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')