Skip to content

Commit

Permalink
response return value runtime check (#3321)
Browse files Browse the repository at this point in the history
  • Loading branch information
benitogf authored and asvetlov committed Oct 5, 2018
1 parent 93395eb commit e00002c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
12 changes: 10 additions & 2 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .tcp_helpers import tcp_cork, tcp_keepalive, tcp_nodelay
from .web_exceptions import HTTPException
from .web_request import BaseRequest
from .web_response import Response
from .web_response import Response, StreamResponse


__all__ = ('RequestHandler', 'RequestPayloadError', 'PayloadAccessError')
Expand Down Expand Up @@ -346,6 +346,7 @@ async def start(self):
handler = self._task_handler
manager = self._manager
keepalive_timeout = self._keepalive_timeout
resp = None

while not self._force_close:
if not self._messages:
Expand Down Expand Up @@ -389,6 +390,13 @@ async def start(self):
"please raise the exception instead",
DeprecationWarning)

if self.debug:
if not isinstance(resp, StreamResponse):
self.log_debug("Possibly missing return "
"statement on request handler")
raise RuntimeError("Web-handler should return "
"a response instance, "
"got {!r}".format(resp))
await resp.prepare(request)
await resp.write_eof()

Expand Down Expand Up @@ -438,7 +446,7 @@ async def start(self):
self.log_exception('Unhandled exception', exc_info=exc)
self.force_close()
finally:
if self.transport is None:
if self.transport is None and resp is not None:
self.log_debug('Ignored premature client disconnection.')
elif not self._force_close:
if self._keepalive and not self._close:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,32 @@ async def cancel():
assert log.debug.called


async def test_handle_none_response(make_srv, transport, request_handler):
loop = asyncio.get_event_loop()
log = mock.Mock()

srv = make_srv(logger=log, debug=True)
srv.connection_made(transport)

handle = mock.Mock()
handle.return_value = loop.create_future()
handle.return_value.set_result(None)
request_handler.side_effect = handle

srv.data_received(
b'GET / HTTP/1.0\r\n'
b'Content-Length: 10\r\n'
b'Host: example.com\r\n\r\n')

assert srv._task_handler

await asyncio.sleep(0, loop=loop)
await srv._task_handler
assert request_handler.called
log.debug.assert_called_with("Possibly missing return "
"statement on request handler")


async def test_handle_cancelled(make_srv, transport) -> None:
log = mock.Mock()

Expand Down

0 comments on commit e00002c

Please sign in to comment.