diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index d867dba440c..21d92273ce3 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -93,7 +93,14 @@ def __init__(self, method, handler, *, issubclass(handler, AbstractView)): pass else: - handler = asyncio.coroutine(handler) + @asyncio.coroutine + def handler_wrapper(*args, **kwargs): + result = old_handler(*args, **kwargs) + if asyncio.iscoroutine(result): + result = yield from result + return result + old_handler = handler + handler = handler_wrapper self._method = method self._handler = handler diff --git a/tests/test_web_urldispatcher.py b/tests/test_web_urldispatcher.py index acc6f28f49c..689bfbc2c36 100644 --- a/tests/test_web_urldispatcher.py +++ b/tests/test_web_urldispatcher.py @@ -2,6 +2,9 @@ import os import shutil import tempfile +import functools +import asyncio +import aiohttp.web @pytest.fixture(scope='function') @@ -44,3 +47,19 @@ def test_access_root_of_static_handler(tmp_dir_path, create_app_and_client): assert r.status == 404 # data = (yield from r.read()) yield from r.release() + + +@pytest.mark.run_loop +def test_partialy_applied_handler(create_app_and_client): + app, client = yield from create_app_and_client() + + @asyncio.coroutine + def handler(data, request): + return aiohttp.web.Response(body=data) + + app.router.add_route('GET', '/', functools.partial(handler, b'hello')) + + r = yield from client.get('/') + data = (yield from r.read()) + assert data == b'hello' + yield from r.release()