This commit is contained in:
Steve Pulec 2017-02-23 21:37:43 -05:00
commit f37bad0e00
260 changed files with 6363 additions and 3766 deletions

View file

@ -1,6 +1,6 @@
from __future__ import unicode_literals
from .models import sqs_backends
from ..core.models import MockAWS, base_decorator, HttprettyMockAWS, deprecated_base_decorator
from ..core.models import base_decorator, deprecated_base_decorator
sqs_backend = sqs_backends['us-east-1']
mock_sqs = base_decorator(sqs_backends)

View file

@ -1,7 +1,6 @@
from __future__ import unicode_literals
import hashlib
import time
import re
from xml.sax.saxutils import escape
@ -18,7 +17,9 @@ from .exceptions import (
DEFAULT_ACCOUNT_ID = 123456789012
DEFAULT_SENDER_ID = "AIDAIT2UOQQY3AUEKVGXU"
class Message(object):
def __init__(self, message_id, body):
self.id = message_id
self._body = body
@ -122,7 +123,8 @@ class Queue(object):
self.last_modified_timestamp = now
self.maximum_message_size = 64 << 10
self.message_retention_period = 86400 * 4 # four days
self.queue_arn = 'arn:aws:sqs:{0}:123456789012:{1}'.format(self.region, self.name)
self.queue_arn = 'arn:aws:sqs:{0}:123456789012:{1}'.format(
self.region, self.name)
self.receive_message_wait_time_seconds = 0
@classmethod
@ -177,7 +179,8 @@ class Queue(object):
def attributes(self):
result = {}
for attribute in self.camelcase_attributes:
result[attribute] = getattr(self, camelcase_to_underscores(attribute))
result[attribute] = getattr(
self, camelcase_to_underscores(attribute))
return result
@property
@ -201,6 +204,7 @@ class Queue(object):
class SQSBackend(BaseBackend):
def __init__(self, region_name):
self.region_name = region_name
self.queues = {}
@ -214,7 +218,8 @@ class SQSBackend(BaseBackend):
def create_queue(self, name, visibility_timeout, wait_time_seconds):
queue = self.queues.get(name)
if queue is None:
queue = Queue(name, visibility_timeout, wait_time_seconds, self.region_name)
queue = Queue(name, visibility_timeout,
wait_time_seconds, self.region_name)
self.queues[name] = queue
return queue

View file

@ -27,7 +27,8 @@ class SQSResponse(BaseResponse):
@property
def attribute(self):
if not hasattr(self, '_attribute'):
self._attribute = dict([(a['name'], a['value']) for a in self._get_list_prefix('Attribute')])
self._attribute = dict([(a['name'], a['value'])
for a in self._get_list_prefix('Attribute')])
return self._attribute
def _get_queue_name(self):
@ -59,7 +60,7 @@ class SQSResponse(BaseResponse):
def create_queue(self):
queue_name = self.querystring.get("QueueName")[0]
queue = self.sqs_backend.create_queue(queue_name, visibility_timeout=self.attribute.get('VisibilityTimeout'),
wait_time_seconds=self.attribute.get('WaitTimeSeconds'))
wait_time_seconds=self.attribute.get('WaitTimeSeconds'))
template = self.response_template(CREATE_QUEUE_RESPONSE)
return template.render(queue=queue)
@ -108,7 +109,8 @@ class SQSResponse(BaseResponse):
def set_queue_attributes(self):
queue_name = self._get_queue_name()
if "Attribute.Name" in self.querystring:
key = camelcase_to_underscores(self.querystring.get("Attribute.Name")[0])
key = camelcase_to_underscores(
self.querystring.get("Attribute.Name")[0])
value = self.querystring.get("Attribute.Value")[0]
self.sqs_backend.set_queue_attribute(queue_name, key, value)
for a in self._get_list_prefix("Attribute"):
@ -171,20 +173,25 @@ class SQSResponse(BaseResponse):
messages = []
for index in range(1, 11):
# Loop through looking for messages
message_key = 'SendMessageBatchRequestEntry.{0}.MessageBody'.format(index)
message_key = 'SendMessageBatchRequestEntry.{0}.MessageBody'.format(
index)
message_body = self.querystring.get(message_key)
if not message_body:
# Found all messages
break
message_user_id_key = 'SendMessageBatchRequestEntry.{0}.Id'.format(index)
message_user_id_key = 'SendMessageBatchRequestEntry.{0}.Id'.format(
index)
message_user_id = self.querystring.get(message_user_id_key)[0]
delay_key = 'SendMessageBatchRequestEntry.{0}.DelaySeconds'.format(index)
delay_key = 'SendMessageBatchRequestEntry.{0}.DelaySeconds'.format(
index)
delay_seconds = self.querystring.get(delay_key, [None])[0]
message = self.sqs_backend.send_message(queue_name, message_body[0], delay_seconds=delay_seconds)
message = self.sqs_backend.send_message(
queue_name, message_body[0], delay_seconds=delay_seconds)
message.user_id = message_user_id
message_attributes = parse_message_attributes(self.querystring, base='SendMessageBatchRequestEntry.{0}.'.format(index))
message_attributes = parse_message_attributes(
self.querystring, base='SendMessageBatchRequestEntry.{0}.'.format(index))
if type(message_attributes) == tuple:
return message_attributes[0], message_attributes[1]
message.message_attributes = message_attributes
@ -216,7 +223,8 @@ class SQSResponse(BaseResponse):
message_ids = []
for index in range(1, 11):
# Loop through looking for messages
receipt_key = 'DeleteMessageBatchRequestEntry.{0}.ReceiptHandle'.format(index)
receipt_key = 'DeleteMessageBatchRequestEntry.{0}.ReceiptHandle'.format(
index)
receipt_handle = self.querystring.get(receipt_key)
if not receipt_handle:
# Found all messages
@ -224,7 +232,8 @@ class SQSResponse(BaseResponse):
self.sqs_backend.delete_message(queue_name, receipt_handle[0])
message_user_id_key = 'DeleteMessageBatchRequestEntry.{0}.Id'.format(index)
message_user_id_key = 'DeleteMessageBatchRequestEntry.{0}.Id'.format(
index)
message_user_id = self.querystring.get(message_user_id_key)[0]
message_ids.append(message_user_id)
@ -258,7 +267,8 @@ class SQSResponse(BaseResponse):
except ValueError:
return ERROR_MAX_VISIBILITY_TIMEOUT_RESPONSE, dict(status=400)
messages = self.sqs_backend.receive_messages(queue_name, message_count, wait_time, visibility_timeout)
messages = self.sqs_backend.receive_messages(
queue_name, message_count, wait_time, visibility_timeout)
template = self.response_template(RECEIVE_MESSAGE_RESPONSE)
output = template.render(messages=messages)
return output
@ -444,7 +454,8 @@ ERROR_TOO_LONG_RESPONSE = """<ErrorResponse xmlns="http://queue.amazonaws.com/do
<RequestId>6fde8d1e-52cd-4581-8cd9-c512f4c64223</RequestId>
</ErrorResponse>"""
ERROR_MAX_VISIBILITY_TIMEOUT_RESPONSE = "Invalid request, maximum visibility timeout is {0}".format(MAXIMUM_VISIBILTY_TIMEOUT)
ERROR_MAX_VISIBILITY_TIMEOUT_RESPONSE = "Invalid request, maximum visibility timeout is {0}".format(
MAXIMUM_VISIBILTY_TIMEOUT)
ERROR_INEXISTENT_QUEUE = """<ErrorResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">
<Error>

View file

@ -22,25 +22,32 @@ def parse_message_attributes(querystring, base='', value_namespace='Value.'):
# Found all attributes
break
data_type_key = base + 'MessageAttribute.{0}.{1}DataType'.format(index, value_namespace)
data_type_key = base + \
'MessageAttribute.{0}.{1}DataType'.format(index, value_namespace)
data_type = querystring.get(data_type_key)
if not data_type:
raise MessageAttributesInvalid("The message attribute '{0}' must contain non-empty message attribute value.".format(name[0]))
raise MessageAttributesInvalid(
"The message attribute '{0}' must contain non-empty message attribute value.".format(name[0]))
data_type_parts = data_type[0].split('.')
if len(data_type_parts) > 2 or data_type_parts[0] not in ['String', 'Binary', 'Number']:
raise MessageAttributesInvalid("The message attribute '{0}' has an invalid message attribute type, the set of supported type prefixes is Binary, Number, and String.".format(name[0]))
raise MessageAttributesInvalid(
"The message attribute '{0}' has an invalid message attribute type, the set of supported type prefixes is Binary, Number, and String.".format(name[0]))
type_prefix = 'String'
if data_type_parts[0] == 'Binary':
type_prefix = 'Binary'
value_key = base + 'MessageAttribute.{0}.{1}{2}Value'.format(index, value_namespace, type_prefix)
value_key = base + \
'MessageAttribute.{0}.{1}{2}Value'.format(
index, value_namespace, type_prefix)
value = querystring.get(value_key)
if not value:
raise MessageAttributesInvalid("The message attribute '{0}' must contain non-empty message attribute value for message attribute type '{1}'.".format(name[0], data_type[0]))
raise MessageAttributesInvalid(
"The message attribute '{0}' must contain non-empty message attribute value for message attribute type '{1}'.".format(name[0], data_type[0]))
message_attributes[name[0]] = {'data_type': data_type[0], type_prefix.lower() + '_value': value[0]}
message_attributes[name[0]] = {'data_type': data_type[
0], type_prefix.lower() + '_value': value[0]}
index += 1