Fixes for multi-region SQS.

This commit is contained in:
Steve Pulec 2014-11-16 17:57:46 -05:00
parent 1f8253a1a1
commit aa7233a2db
3 changed files with 24 additions and 6 deletions

View File

@ -43,7 +43,8 @@ def _decode_dict(d):
class BaseResponse(object):
region = 'us-east-1'
default_region = 'us-east-1'
region_regex = r'\.(.+?)\.amazonaws\.com'
def dispatch(self, request, full_url, headers):
querystring = {}
@ -76,9 +77,11 @@ class BaseResponse(object):
self.path = urlparse(full_url).path
self.querystring = querystring
self.method = request.method
region = re.search(r'\.(.+?)\.amazonaws\.com', full_url)
region = re.search(self.region_regex, full_url)
if region:
self.region = region.group(1)
else:
self.region = self.default_region
self.headers = dict(request.headers)
self.response_headers = headers

View File

@ -16,6 +16,8 @@ MAXIMUM_VISIBILTY_TIMEOUT = 43200
class QueuesResponse(BaseResponse):
region_regex = r'://(.+?)\.queue\.amazonaws\.com'
@property
def sqs_backend(self):
return sqs_backends[self.region]

View File

@ -10,6 +10,7 @@ import time
from moto import mock_sqs
from tests.helpers import requires_boto_gte
@mock_sqs
def test_create_queue():
conn = boto.connect_sqs('the_key', 'the_secret')
@ -21,6 +22,18 @@ def test_create_queue():
all_queues[0].get_timeout().should.equal(60)
@mock_sqs
def test_create_queues_in_multiple_region():
west1_conn = boto.sqs.connect_to_region("us-west-1")
west1_conn.create_queue("test-queue")
west2_conn = boto.sqs.connect_to_region("us-west-2")
west2_conn.create_queue("test-queue")
list(west1_conn.get_all_queues()).should.have.length_of(1)
list(west2_conn.get_all_queues()).should.have.length_of(1)
@mock_sqs
def test_get_queue():
conn = boto.connect_sqs('the_key', 'the_secret')
@ -113,13 +126,13 @@ def test_send_message_with_attributes():
conn = boto.connect_sqs('the_key', 'the_secret')
queue = conn.create_queue("test-queue", visibility_timeout=60)
queue.set_message_class(RawMessage)
body = 'this is a test message'
message = queue.new_message(body)
message_attributes = {
'test.attribute_name' : {'data_type' : 'String', 'string_value' : 'attribute value'},
'test.binary_attribute' : {'data_type' : 'Binary', 'binary_value' : 'binary value'},
'test.number_attribute' : {'data_type' : 'Number', 'string_value' : 'string value'}
'test.attribute_name': {'data_type': 'String', 'string_value': 'attribute value'},
'test.binary_attribute': {'data_type': 'Binary', 'binary_value': 'binary value'},
'test.number_attribute': {'data_type': 'Number', 'string_value': 'string value'}
}
message.message_attributes = message_attributes