diff options
author | Ben Bangert <ben@groovie.org> | 2010-02-28 10:56:46 -0800 |
---|---|---|
committer | Ben Bangert <ben@groovie.org> | 2010-02-28 10:56:46 -0800 |
commit | 70248103ac63d6a763bf1d8f067d8f3751aa74d9 (patch) | |
tree | 7ab431f1842ec4f28b5e71c814a8fb97258d6a88 | |
parent | 1c02dc6d28ad9eee37263a343f30d4a11ba0f685 (diff) | |
download | routes-70248103ac63d6a763bf1d8f067d8f3751aa74d9.tar.gz |
Ensure singleton usage is still default in middleware so Routes upgrade doesn't break url_for users.
--HG--
branch : trunk
-rw-r--r-- | routes/middleware.py | 18 | ||||
-rw-r--r-- | tests/test_functional/test_middleware.py | 27 |
2 files changed, 38 insertions, 7 deletions
diff --git a/routes/middleware.py b/routes/middleware.py index 0fa9a9e..b772038 100644 --- a/routes/middleware.py +++ b/routes/middleware.py @@ -13,7 +13,7 @@ class RoutesMiddleware(object): """Routing middleware that handles resolving the PATH_INFO in addition to optionally recognizing method overriding.""" def __init__(self, wsgi_app, mapper, use_method_override=True, - path_info=True): + path_info=True, singleton=True): """Create a Route middleware object Using the use_method_override keyword will require Paste to be @@ -32,6 +32,7 @@ class RoutesMiddleware(object): """ self.app = wsgi_app self.mapper = mapper + self.singleton = singleton self.use_method_override = use_method_override self.path_info = path_info log_debug = self.log_debug = logging.DEBUG >= log.getEffectiveLevel() @@ -73,11 +74,18 @@ class RoutesMiddleware(object): # Run the actual route matching # -- Assignment of environ to config triggers route matching - results = self.mapper.routematch(environ=environ) - if results: - match, route = results[0], results[1] + if self.singleton: + config = request_config() + config.mapper = self.mapper + config.environ = environ + match = config.mapper_dict + route = config.route else: - match = route = None + results = self.mapper.routematch(environ=environ) + if results: + match, route = results[0], results[1] + else: + match = route = None if old_method: environ['REQUEST_METHOD'] = old_method diff --git a/tests/test_functional/test_middleware.py b/tests/test_functional/test_middleware.py index cd87ebd..6b51901 100644 --- a/tests/test_functional/test_middleware.py +++ b/tests/test_functional/test_middleware.py @@ -30,7 +30,7 @@ def test_no_query(): map.create_regs(['content', 'myapp']) app = RoutesMiddleware(simple_app, map) - env = {'PATH_INFO': '/', 'REQUEST_METHOD': 'GET'} + env = {'PATH_INFO': '/', 'REQUEST_METHOD': 'GET', 'HTTP_HOST': 'localhost'} def start_response_wrapper(status, headers, exc=None): pass response = ''.join(app(env, start_response_wrapper)) @@ -44,11 +44,34 @@ def test_content_split(): map.create_regs(['content', 'myapp']) app = RoutesMiddleware(simple_app, map) + env = {'PATH_INFO': '/', 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'text/plain;text/html', + 'HTTP_HOST': 'localhost'} + def start_response_wrapper(status, headers, exc=None): + pass + response = ''.join(app(env, start_response_wrapper)) + assert 'matchdict items are []' in response + +def test_no_singleton(): + map = Mapper(explicit=False) + map.minimization = True + map.connect('myapp/*path_info', controller='myapp') + map.connect('project/*path_info', controller='myapp') + map.create_regs(['content', 'myapp']) + + app = RoutesMiddleware(simple_app, map, singleton=False) env = {'PATH_INFO': '/', 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'text/plain;text/html'} def start_response_wrapper(status, headers, exc=None): pass response = ''.join(app(env, start_response_wrapper)) - assert 'matchdict items are []' in response + assert 'matchdict items are []' in response + + # Now a match + env = {'PATH_INFO': '/project/fred', 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'text/plain;text/html'} + def start_response_wrapper(status, headers, exc=None): + pass + response = ''.join(app(env, start_response_wrapper)) + assert "matchdict items are [('action', u'index'), ('controller', u'myapp'), ('path_info', 'fred')]" in response + def test_path_info(): map = Mapper(explicit=False) |