From 27dc73fe0458ae5334c4547d700f912681a4bcfd Mon Sep 17 00:00:00 2001 From: anc Date: Fri, 5 Jul 2013 13:18:16 +0100 Subject: [PATCH] Fix make_pre_authed_request function to not fail when path arg is None. The default value for the path arg to the function is None. However, if the path arg is not set or is set to None an exception is generated when the path value is passed to urllib.unquote(). The function doc states that if path is not provided then the value in the env is used, so fix is to implement this behaviour. Change-Id: I085124acb6ef3cecb2375bb97d27996e0c6fd36e Fixes: bug #1198149 --- swift/common/wsgi.py | 1 + test/unit/common/test_wsgi.py | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/swift/common/wsgi.py b/swift/common/wsgi.py index 891183a7a9..77456ffeec 100644 --- a/swift/common/wsgi.py +++ b/swift/common/wsgi.py @@ -432,6 +432,7 @@ def make_pre_authed_request(env, method=None, path=None, body=None, :returns: Fresh swob.Request object. """ query_string = None + path = path or '' if path and '?' in path: path, query_string = path.split('?', 1) newenv = make_pre_authed_env(env, method, path=unquote(path), agent=agent, diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py index 38d97aafcf..86f8baedd6 100644 --- a/test/unit/common/test_wsgi.py +++ b/test/unit/common/test_wsgi.py @@ -484,6 +484,45 @@ class TestWSGI(unittest.TestCase): self.assertEquals(r.body, 'the body') self.assertEquals(r.environ['swift.source'], 'UT') + def test_pre_auth_req_with_empty_env_no_path(self): + r = wsgi.make_pre_authed_request( + {}, 'GET') + self.assertEquals(r.path, quote('')) + self.assertTrue('SCRIPT_NAME' in r.environ) + self.assertTrue('PATH_INFO' in r.environ) + + def test_pre_auth_req_with_env_path(self): + r = wsgi.make_pre_authed_request( + {'PATH_INFO': '/unquoted path with %20'}, 'GET') + self.assertEquals(r.path, quote('/unquoted path with %20')) + self.assertEquals(r.environ['SCRIPT_NAME'], '') + + def test_pre_auth_req_with_env_script(self): + r = wsgi.make_pre_authed_request({'SCRIPT_NAME': '/hello'}, 'GET') + self.assertEquals(r.path, quote('/hello')) + + def test_pre_auth_req_with_env_path_and_script(self): + env = {'PATH_INFO': '/unquoted path with %20', + 'SCRIPT_NAME': '/script'} + r = wsgi.make_pre_authed_request(env, 'GET') + expected_path = quote(env['SCRIPT_NAME'] + env['PATH_INFO']) + self.assertEquals(r.path, expected_path) + env = {'PATH_INFO': '', 'SCRIPT_NAME': '/script'} + r = wsgi.make_pre_authed_request(env, 'GET') + self.assertEquals(r.path, '/script') + env = {'PATH_INFO': '/path', 'SCRIPT_NAME': ''} + r = wsgi.make_pre_authed_request(env, 'GET') + self.assertEquals(r.path, '/path') + env = {'PATH_INFO': '', 'SCRIPT_NAME': ''} + r = wsgi.make_pre_authed_request(env, 'GET') + self.assertEquals(r.path, '') + + def test_pre_auth_req_path_overrides_env(self): + env = {'PATH_INFO': '/path', 'SCRIPT_NAME': '/script'} + r = wsgi.make_pre_authed_request(env, 'GET', '/override') + self.assertEquals(r.path, '/override') + self.assertEquals(r.environ['SCRIPT_NAME'], '') + self.assertEquals(r.environ['PATH_INFO'], '/override') class TestWSGIContext(unittest.TestCase):