Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct request.app context (for handlers not just middlewares) #2577

Merged
merged 3 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/2577.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correct `request.app` context (for handlers not just middlewares).
3 changes: 1 addition & 2 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,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

async def _handle(self, request):
match_info = await self._router.resolve(request)
Expand Down
36 changes: 25 additions & 11 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,32 +1290,46 @@ async 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'], ''),
])
async def test_subapp_middleware_context(loop, test_client, route, expected):
async def test_subapp_middleware_context(
loop, test_client, route, expected, middlewares):
values = []

def show_app_context(appname):
@web.middleware
async def middleware(request, handler):
values.append('{} see {}'.format(appname, request.app['my_value']))
values.append('{}: {}'.format(
appname, request.app['my_value']))
return await handler(request)
return middleware

async def handler(request):
return web.Response(text='Ok')
def make_handler(appname):
async 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 = await test_client(app)
Expand Down