From 65a5a94e13e49b29b34287ede881d323505a281b Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Thu, 21 Apr 2011 23:25:36 -0400 Subject: add unit tests for call_app_with_subpath_as_path_info --- pyramid/request.py | 12 ++++---- pyramid/tests/test_request.py | 67 +++++++++++++++++++++++++++++++++++++++++++ pyramid/tests/test_static.py | 4 +-- 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/pyramid/request.py b/pyramid/request.py index 7bf7c320d..0fe8b9379 100644 --- a/pyramid/request.py +++ b/pyramid/request.py @@ -414,7 +414,7 @@ def call_app_with_subpath_as_path_info(request, app): new_script_name = '' # compute new_path_info - new_path_info = '/' + '/'.join([quote_path_segment(x) for x in subpath]) + new_path_info = '/' + '/'.join([x.encode('utf-8') for x in subpath]) if new_path_info != '/': # don't want a sole double-slash if path_info != '/': # if orig path_info is '/', we're already done @@ -426,11 +426,6 @@ def call_app_with_subpath_as_path_info(request, app): # compute new_script_name workback = (script_name + path_info).split('/') - # strip trailing slash from workback to avoid appending undue slash - # to end of script_name - if workback and (workback[-1] == ''): - workback = workback[:-1] - tmp = [] while workback: if tmp == subpath: @@ -438,6 +433,11 @@ def call_app_with_subpath_as_path_info(request, app): el = workback.pop() if el: tmp.insert(0, el.decode('utf-8')) + + # strip all trailing slashes from workback to avoid appending undue slashes + # to end of script_name + while workback and (workback[-1] == ''): + workback = workback[:-1] new_script_name = '/'.join(workback) diff --git a/pyramid/tests/test_request.py b/pyramid/tests/test_request.py index 66a451220..60d59ece6 100644 --- a/pyramid/tests/test_request.py +++ b/pyramid/tests/test_request.py @@ -325,6 +325,66 @@ class Test_add_global_response_headers(unittest.TestCase): request.response_callbacks[0](None, response) self.assertEqual(response.headerlist, [('c', 1)] ) +class Test_call_app_with_subpath_as_path_info(unittest.TestCase): + def _callFUT(self, request, app): + from pyramid.request import call_app_with_subpath_as_path_info + return call_app_with_subpath_as_path_info(request, app) + + def test_it_all_request_and_environment_data_missing(self): + request = DummyRequest({}) + response = self._callFUT(request, 'app') + self.assertTrue(request.copied) + self.assertEqual(response, 'app') + self.assertEqual(request.environ['SCRIPT_NAME'], '') + self.assertEqual(request.environ['PATH_INFO'], '/') + + def test_it_with_subpath_and_path_info(self): + request = DummyRequest({'PATH_INFO':'/hello'}) + request.subpath = ('hello',) + response = self._callFUT(request, 'app') + self.assertTrue(request.copied) + self.assertEqual(response, 'app') + self.assertEqual(request.environ['SCRIPT_NAME'], '') + self.assertEqual(request.environ['PATH_INFO'], '/hello') + + def test_it_with_subpath_and_path_info_path_info_endswith_slash(self): + request = DummyRequest({'PATH_INFO':'/hello/'}) + request.subpath = ('hello',) + response = self._callFUT(request, 'app') + self.assertTrue(request.copied) + self.assertEqual(response, 'app') + self.assertEqual(request.environ['SCRIPT_NAME'], '') + self.assertEqual(request.environ['PATH_INFO'], '/hello/') + + def test_it_with_subpath_and_path_info_extra_script_name(self): + request = DummyRequest({'PATH_INFO':'/hello', 'SCRIPT_NAME':'/script'}) + request.subpath = ('hello',) + response = self._callFUT(request, 'app') + self.assertTrue(request.copied) + self.assertEqual(response, 'app') + self.assertEqual(request.environ['SCRIPT_NAME'], '/script') + self.assertEqual(request.environ['PATH_INFO'], '/hello') + + def test_it_with_extra_slashes_in_path_info(self): + request = DummyRequest({'PATH_INFO':'//hello/', + 'SCRIPT_NAME':'/script'}) + request.subpath = ('hello',) + response = self._callFUT(request, 'app') + self.assertTrue(request.copied) + self.assertEqual(response, 'app') + self.assertEqual(request.environ['SCRIPT_NAME'], '/script') + self.assertEqual(request.environ['PATH_INFO'], '/hello/') + + def test_subpath_path_info_and_script_name_have_utf8(self): + la = 'La Pe\xc3\xb1a' + request = DummyRequest({'PATH_INFO':'/'+la, 'SCRIPT_NAME':'/'+la}) + request.subpath = (unicode(la, 'utf-8'), ) + response = self._callFUT(request, 'app') + self.assertTrue(request.copied) + self.assertEqual(response, 'app') + self.assertEqual(request.environ['SCRIPT_NAME'], '/' + la) + self.assertEqual(request.environ['PATH_INFO'], '/' + la) + class DummyRequest: def __init__(self, environ=None): if environ is None: @@ -334,6 +394,13 @@ class DummyRequest: def add_response_callback(self, callback): self.response_callbacks = [callback] + def get_response(self, app): + return app + + def copy(self): + self.copied = True + return self + class DummyResponse: def __init__(self): self.headerlist = [] diff --git a/pyramid/tests/test_static.py b/pyramid/tests/test_static.py index 0095b29d3..588dc32e2 100644 --- a/pyramid/tests/test_static.py +++ b/pyramid/tests/test_static.py @@ -296,7 +296,7 @@ class Test_static_view(unittest.TestCase): self.assertEqual(request.environ['SCRIPT_NAME'], '/scriptname/path_info') - def test_with_subpath_new_script_name_fixes_trailing_double_slashes(self): + def test_with_subpath_new_script_name_fixes_trailing_slashes(self): view = self._makeOne('fixtures', package_name='another') context = DummyContext() request = DummyRequest() @@ -305,7 +305,7 @@ class Test_static_view(unittest.TestCase): view(context, request) self.assertEqual(request.copied, True) self.assertEqual(request.environ['PATH_INFO'], '/sub/path/') - self.assertEqual(request.environ['SCRIPT_NAME'], '/path_info/') + self.assertEqual(request.environ['SCRIPT_NAME'], '/path_info') class TestStaticURLInfo(unittest.TestCase): def _getTargetClass(self): -- cgit v1.2.3