Skip to content

Commit

Permalink
get swob.Request.blank to parse path
Browse files Browse the repository at this point in the history
Change-Id: I3327c915b3b868bb1829103adb718632e58b1b4a
  • Loading branch information
dpgoetz committed Jan 16, 2013
1 parent 4a909cf commit 54b7594
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
28 changes: 17 additions & 11 deletions swift/common/swob.py
Expand Up @@ -728,22 +728,28 @@ def blank(cls, path, environ=None, headers=None, body=None):
"""
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
Expand Down
18 changes: 18 additions & 0 deletions test/unit/common/test_swob.py
Expand Up @@ -287,6 +287,24 @@ def test_blank_body_precedence(self):
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')
Expand Down

0 comments on commit 54b7594

Please sign in to comment.