Merge "get swob.Request.blank to parse path"

This commit is contained in:
Jenkins 2013-01-16 20:44:36 +00:00 committed by Gerrit Code Review
commit 0fce08a255
2 changed files with 35 additions and 11 deletions

View File

@ -728,22 +728,28 @@ class Request(object):
"""
headers = headers or {}
environ = environ or {}
if '?' in path:
path_info, query_string = path.split('?')
else:
path_info = path
query_string = ''
parsed_path = urlparse.urlparse(path)
server_name = 'localhost'
if parsed_path.netloc:
server_name = parsed_path.netloc.split(':', 1)[0]
server_port = parsed_path.port
if server_port is None:
server_port = {'http': 80,
'https': 443}.get(parsed_path.scheme, 80)
if parsed_path.scheme and parsed_path.scheme not in ['http', 'https']:
raise TypeError('Invalid scheme: %s' % parsed_path.scheme)
env = {
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': '',
'QUERY_STRING': query_string,
'PATH_INFO': urllib2.unquote(path_info),
'SERVER_NAME': 'localhost',
'SERVER_PORT': '80',
'HTTP_HOST': 'localhost:80',
'QUERY_STRING': parsed_path.query,
'PATH_INFO': urllib2.unquote(parsed_path.path),
'SERVER_NAME': server_name,
'SERVER_PORT': str(server_port),
'HTTP_HOST': '%s:%d' % (server_name, server_port),
'SERVER_PROTOCOL': 'HTTP/1.0',
'wsgi.version': (1, 0),
'wsgi.url_scheme': 'http',
'wsgi.url_scheme': parsed_path.scheme or 'http',
'wsgi.errors': StringIO(''),
'wsgi.multithread': False,
'wsgi.multiprocess': False

View File

@ -287,6 +287,24 @@ class TestRequest(unittest.TestCase):
self.assertEquals(req.headers['Content-Type'], 'text/plain')
self.assertEquals(req.method, 'POST')
def test_blank_parsing(self):
req = swift.common.swob.Request.blank('http://test.com/')
self.assertEquals(req.environ['wsgi.url_scheme'], 'http')
self.assertEquals(req.environ['SERVER_PORT'], '80')
self.assertEquals(req.environ['SERVER_NAME'], 'test.com')
req = swift.common.swob.Request.blank('https://test.com:456/')
self.assertEquals(req.environ['wsgi.url_scheme'], 'https')
self.assertEquals(req.environ['SERVER_PORT'], '456')
req = swift.common.swob.Request.blank('test.com/')
self.assertEquals(req.environ['wsgi.url_scheme'], 'http')
self.assertEquals(req.environ['SERVER_PORT'], '80')
self.assertEquals(req.environ['PATH_INFO'], 'test.com/')
self.assertRaises(TypeError, swift.common.swob.Request.blank,
'ftp://test.com/')
def test_params(self):
req = swift.common.swob.Request.blank('/?a=b&c=d')
self.assertEquals(req.params['a'], 'b')