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

Fix #1416 #1460

Merged
merged 2 commits into from
Dec 7, 2016
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
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CHANGES

- Introduce `router.post_init()` for solving #1373

-
- Allow to raise web exceptions on router resolving stage #1460

-

Expand Down
9 changes: 7 additions & 2 deletions aiohttp/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ class AbstractRouter(ABC):
def __init__(self):
self._frozen = False

@abstractmethod
def post_init(self, app):
pass # pragma: no cover
"""Post init stage.

It's not an abstract method for sake of backward compatibility
but if router wans to be aware about application it should
override it.

"""

@property
def frozen(self):
Expand Down
5 changes: 4 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,10 @@ def app(self):

@asyncio.coroutine
def _prepare_hook(self, response):
for app in self.match_info.apps:
match_info = self._match_info
if match_info is None:
return
for app in match_info.apps:
yield from app.on_response_prepare.send(self, response)


Expand Down
20 changes: 19 additions & 1 deletion tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import pytest

from aiohttp import web
from aiohttp import abc, web
from aiohttp.web_urldispatcher import SystemRoute


Expand Down Expand Up @@ -270,3 +270,21 @@ def test_system_route():
assert "<SystemRoute 201: test>" == repr(route)
assert 201 == route.status
assert 'test' == route.reason


@asyncio.coroutine
def test_412_is_returned(loop, test_client):

class MyRouter(abc.AbstractRouter):

@asyncio.coroutine
def resolve(self, request):
raise web.HTTPPreconditionFailed()

app = web.Application(router=MyRouter(), loop=loop)

client = yield from test_client(app)

resp = yield from client.get('/')

assert resp.status == 412