attempting to move to upstream httpretty

This commit is contained in:
Steve Pulec 2013-05-03 19:33:13 -04:00
commit 47bd4c49a3
23 changed files with 237 additions and 1160 deletions

View file

@ -1,7 +1,7 @@
import functools
import re
from moto.packages.httpretty import HTTPretty
from httpretty import HTTPretty
from .responses import metadata_response
from .utils import convert_regex_to_flask_path

View file

@ -1,50 +1,75 @@
import datetime
import json
from urlparse import parse_qs
from urlparse import parse_qs, urlparse
from moto.core.utils import headers_to_dict, camelcase_to_underscores, method_names_from_class
class BaseResponse(object):
def dispatch(self, uri, method, body, headers):
if body:
querystring = parse_qs(body)
def dispatch(self, request, full_url, headers):
if hasattr(request, 'body'):
# Boto
self.body = request.body
else:
# Flask server
self.body = request.data
querystring = parse_qs(urlparse(full_url).query)
if not querystring:
querystring = parse_qs(self.body)
if not querystring:
querystring = headers_to_dict(headers)
self.path = uri.path
self.uri = full_url
self.path = urlparse(full_url).path
self.querystring = querystring
self.method = request.method
action = querystring.get('Action', [""])[0]
self.headers = dict(request.headers)
self.response_headers = headers
return self.call_action()
def call_action(self):
headers = self.response_headers
action = self.querystring.get('Action', [""])[0]
action = camelcase_to_underscores(action)
method_names = method_names_from_class(self.__class__)
if action in method_names:
method = getattr(self, action)
return method()
response = method()
if isinstance(response, basestring):
return 200, headers, response
else:
body, new_headers = response
status = new_headers.pop('status', 200)
headers.update(new_headers)
return status, headers, body
raise NotImplementedError("The {} action has not been implemented".format(action))
def metadata_response(uri, method, body, headers):
def metadata_response(request, full_url, headers):
"""
Mock response for localhost metadata
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html
"""
parsed_url = urlparse(full_url)
tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
path = uri.path.lstrip("/latest/meta-data/")
path = parsed_url.path.lstrip("/latest/meta-data/")
if path == '':
return "iam/"
result = "iam/"
elif path == 'iam/':
return 'security-credentials/'
result = 'security-credentials/'
elif path == 'iam/security-credentials/':
return 'default-role'
result = 'default-role'
elif path == 'iam/security-credentials/default-role':
return json.dumps(dict(
result = json.dumps(dict(
AccessKeyId="test-key",
SecretAccessKey="test-secret-key",
Token="test-session-token",
Expiration=tomorrow.strftime("%Y-%m-%dT%H:%M:%SZ")
))
return 200, headers, result

View file

@ -1,4 +1,3 @@
from collections import namedtuple
import inspect
import random
import re
@ -91,23 +90,12 @@ class convert_flask_to_httpretty_response(object):
return "{}.{}".format(outer, self.callback.__name__)
def __call__(self, args=None, **kwargs):
hostname = request.host_url
method = request.method
path = request.path
query = request.query_string
# Mimic the HTTPretty URIInfo class
URI = namedtuple('URI', 'hostname method path query')
uri = URI(hostname, method, path, query)
body = request.data or query
headers = dict(request.headers)
result = self.callback(uri, method, body, headers)
result = self.callback(request, request.url, headers)
if isinstance(result, basestring):
# result is just the response
return result
else:
# result is a responce, headers tuple
response, headers = result
status = headers.pop('status', None)
# result is a status, headers, response tuple
status, headers, response = result
return response, status, headers