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

[3.7] Preserve view handler function attributes across middlewares (#4195). #4276

Merged
merged 1 commit into from
Oct 26, 2019
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/4174.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve view handler function attributes across middlewares
6 changes: 4 additions & 2 deletions aiohttp/web_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
import warnings
from functools import partial
from functools import partial, update_wrapper
from typing import ( # noqa
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -453,7 +453,9 @@ async def _handle(self, request: Request) -> StreamResponse:
for app in match_info.apps[::-1]:
for m, new_style in app._middlewares_handlers: # type: ignore # noqa
if new_style:
handler = partial(m, handler=handler)
handler = update_wrapper(
partial(m, handler=handler), handler
)
else:
handler = await m(app, handler) # type: ignore

Expand Down
67 changes: 67 additions & 0 deletions tests/test_web_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ async def test_middleware_chain(loop, aiohttp_client) -> None:
async def handler(request):
return web.Response(text='OK')

handler.annotation = "annotation_value"

async def handler2(request):
return web.Response(text='OK')

middleware_annotation_seen_values = []

def make_middleware(num):
@web.middleware
async def middleware(request, handler):
middleware_annotation_seen_values.append(
getattr(handler, "annotation", None)
)
resp = await handler(request)
resp.text = resp.text + '[{}]'.format(num)
return resp
Expand All @@ -65,11 +75,68 @@ async def middleware(request, handler):
app.middlewares.append(make_middleware(1))
app.middlewares.append(make_middleware(2))
app.router.add_route('GET', '/', handler)
app.router.add_route('GET', '/r2', handler2)
client = await aiohttp_client(app)
resp = await client.get('/')
assert 200 == resp.status
txt = await resp.text()
assert 'OK[2][1]' == txt
assert middleware_annotation_seen_values == [
'annotation_value', 'annotation_value'
]

# check that attributes from handler are not applied to handler2
resp = await client.get('/r2')
assert 200 == resp.status
assert middleware_annotation_seen_values == [
'annotation_value', 'annotation_value', None, None
]


async def test_middleware_subapp(loop, aiohttp_client) -> None:
async def sub_handler(request):
return web.Response(text='OK')

sub_handler.annotation = "annotation_value"

async def handler(request):
return web.Response(text='OK')

middleware_annotation_seen_values = []

def make_middleware(num):
@web.middleware
async def middleware(request, handler):
annotation = getattr(handler, "annotation", None)
if annotation is not None:
middleware_annotation_seen_values.append(
"{}/{}".format(annotation, num)
)
return await handler(request)
return middleware

app = web.Application()
app.middlewares.append(make_middleware(1))
app.router.add_route('GET', '/r2', handler)

subapp = web.Application()
subapp.middlewares.append(make_middleware(2))
subapp.router.add_route('GET', '/', sub_handler)
app.add_subapp("/sub", subapp)

client = await aiohttp_client(app)
resp = await client.get('/sub/')
assert 200 == resp.status
await resp.text()
assert middleware_annotation_seen_values == [
'annotation_value/1', 'annotation_value/2'
]

# check that attributes from sub_handler are not applied to handler
del middleware_annotation_seen_values[:]
resp = await client.get('/r2')
assert 200 == resp.status
assert middleware_annotation_seen_values == []


@pytest.fixture
Expand Down