From e47df9af7c0bdb1ea8b90d4aee4cecfcf9b07c1f Mon Sep 17 00:00:00 2001 From: Alexey Popravka Date: Mon, 4 Dec 2017 18:32:13 +0200 Subject: [PATCH] Correct request.app context (for handlers not just middlewares) (#2577) * yield _fix_request_current_app middleware uncoditionally; update test; (see #2550) * Add change notes --- CHANGES/2577.bugfix | 1 + aiohttp/web.py | 3 +-- tests/test_web_functional.py | 38 ++++++++++++++++++++++++------------ 3 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 CHANGES/2577.bugfix diff --git a/CHANGES/2577.bugfix b/CHANGES/2577.bugfix new file mode 100644 index 00000000000..9b7117e752d --- /dev/null +++ b/CHANGES/2577.bugfix @@ -0,0 +1 @@ +Correct `request.app` context (for handlers not just middlewares). diff --git a/aiohttp/web.py b/aiohttp/web.py index 14affe0edca..1abe4702f44 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -295,8 +295,7 @@ def _prepare_middleware(self): 'see #2252'.format(m), DeprecationWarning, stacklevel=2) yield m, False - if self._middlewares: - yield _fix_request_current_app(self), True + yield _fix_request_current_app(self), True @asyncio.coroutine def _handle(self, request): diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index 526a2214720..9f724ea8e29 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -1401,35 +1401,49 @@ def on_signal(app): assert [app, subapp1, subapp2] == order -@pytest.mark.parametrize('route,expected', [ - ('/sub/', ['app see root', 'subapp see sub']), - ('/', ['app see root']), +@pytest.mark.parametrize('route,expected,middlewares', [ + ('/sub/', ['A: root', 'C: sub', 'D: sub'], 'AC'), + ('/', ['A: root', 'B: root'], 'AC'), + ('/sub/', ['A: root', 'D: sub'], 'A'), + ('/', ['A: root', 'B: root'], 'A'), + ('/sub/', ['C: sub', 'D: sub'], 'C'), + ('/', ['B: root'], 'C'), + ('/sub/', ['D: sub'], ''), + ('/', ['B: root'], ''), ]) @asyncio.coroutine -def test_subapp_middleware_context(loop, test_client, route, expected): +def test_subapp_middleware_context(loop, test_client, route, expected, + middlewares): values = [] def show_app_context(appname): @web.middleware @asyncio.coroutine def middleware(request, handler): - values.append('{} see {}'.format(appname, request.app['my_value'])) + values.append('{}: {}'.format( + appname, request.app['my_value'])) return (yield from handler(request)) return middleware - @asyncio.coroutine - def handler(request): - return web.Response(text='Ok') + def make_handler(appname): + @asyncio.coroutine + def handler(request): + values.append('{}: {}'.format( + appname, request.app['my_value'])) + return web.Response(text='Ok') + return handler app = web.Application() app['my_value'] = 'root' - app.middlewares.append(show_app_context('app')) - app.router.add_get('/', handler) + if 'A' in middlewares: + app.middlewares.append(show_app_context('A')) + app.router.add_get('/', make_handler('B')) subapp = web.Application() subapp['my_value'] = 'sub' - subapp.middlewares.append(show_app_context('subapp')) - subapp.router.add_get('/', handler) + if 'C' in middlewares: + subapp.middlewares.append(show_app_context('C')) + subapp.router.add_get('/', make_handler('D')) app.add_subapp('/sub/', subapp) client = yield from test_client(app)