Skip to content

Commit

Permalink
allow raising HTTPError on PRE/POST Request events
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLudwig committed Jul 10, 2017
1 parent 2c082e9 commit 54c1462
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 11 additions & 3 deletions rw/httpbase.py
Expand Up @@ -117,9 +117,17 @@ def start_request(self, server_conn, request_conn):
def _handle_request(self, request_scope, request):
handler = self.handler(self, request)
request_scope['handler'] = handler
yield PRE_REQUEST()
yield handler._execute([])
yield POST_REQUEST()
try:
yield PRE_REQUEST()
yield handler._execute([])
yield POST_REQUEST()
except Exception as e:
# Ensure exceptions in PRE and POST_REQUEST are
# also forwarded to the exception handler.
# It is not just important for logging but making
# `raise HTTPError()` in event handlers work.
handler._transforms = []
handler._handle_request_exception(e)

def _request_finished(self, request_future):
# access result to throw exceptions that might have occurred during
Expand Down
18 changes: 18 additions & 0 deletions test/test_httpbase.py
@@ -1,3 +1,4 @@
import tornado.web
from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog, gen_test

import rw.httpbase
Expand All @@ -15,3 +16,20 @@ def get_app(self):
def test_hello_world(self):
response = self.fetch('/')
assert response.body.decode('utf-8') == u'Hello World'


class HTTPServerEventTest(AsyncHTTPTestCase):
def get_app(self):
return rw.httpbase.Application(handler=HelloWorldHandler)

def test_hello_world(self):
rw.httpbase.PRE_REQUEST.add(self.raise_404)
response = self.fetch('/')
assert response.code == 404

def raise_404(self):
raise tornado.web.HTTPError(404)

@classmethod
def teardown_class(cls):
rw.httpbase.PRE_REQUEST.clear()

0 comments on commit 54c1462

Please sign in to comment.