Skip to content

Commit

Permalink
remove handle_request
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi committed Sep 17, 2019
1 parent d1e8091 commit d09d203
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
2 changes: 1 addition & 1 deletion aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class RequestHandler(BaseProtocol):
'_waiter', '_error_handler', '_task_handler',
'_upgrade', '_payload_parser', '_request_parser',
'_reading_paused', 'logger', 'access_log',
'access_logger', '_close', '_force_close', 'handle_request')
'access_logger', '_close', '_force_close')

def __init__(self, manager: 'Server', *,
loop: asyncio.AbstractEventLoop,
Expand Down
77 changes: 42 additions & 35 deletions tests/test_web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ async def test_lingering(srv, transport) -> None:
async def handle(message, request, writer):
pass

with mock.patch.object(web.RequestHandler, 'handle_request', new=handle):
with mock.patch.object(
web.RequestHandler, 'handle_request', create=True, new=handle
):
srv.data_received(
b'GET / HTTP/1.0\r\n'
b'Host: example.com\r\n'
Expand Down Expand Up @@ -494,18 +496,19 @@ async def test_handle_cancel(make_srv, transport) -> None:
async def handle_request(message, payload, writer):
await asyncio.sleep(10)

srv.handle_request = handle_request

async def cancel():
srv._task_handler.cancel()

srv.data_received(
b'GET / HTTP/1.0\r\n'
b'Content-Length: 10\r\n'
b'Host: example.com\r\n\r\n')
with mock.patch.object(
web.RequestHandler, 'handle_request', create=True, new=handle_request
):
srv.data_received(
b'GET / HTTP/1.0\r\n'
b'Content-Length: 10\r\n'
b'Host: example.com\r\n\r\n')

await asyncio.gather(srv._task_handler, cancel())
assert log.debug.called
await asyncio.gather(srv._task_handler, cancel())
assert log.debug.called


async def test_handle_cancelled(make_srv, transport) -> None:
Expand All @@ -516,7 +519,6 @@ async def test_handle_cancelled(make_srv, transport) -> None:
srv = make_srv(logger=log)
srv.connection_made(transport)

srv.handle_request = mock.Mock()
# start request_handler task
await asyncio.sleep(0)

Expand All @@ -538,16 +540,16 @@ async def test_handle_400(srv, buf, transport) -> None:
async def test_keep_alive(make_srv, transport, ceil) -> None:
loop = asyncio.get_event_loop()
srv = make_srv(keepalive_timeout=0.05)
future = loop.create_future()
future.set_result(1)

with mock.patch.object(
web.RequestHandler, 'KEEPALIVE_RESCHEDULE_DELAY', new=0.1
), mock.patch.object(
web.RequestHandler, 'handle_request', create=True, return_value=future
):
srv.connection_made(transport)

srv.keep_alive(True)
srv.handle_request = mock.Mock()
srv.handle_request.return_value = loop.create_future()
srv.handle_request.return_value.set_result(1)

srv.data_received(
b'GET / HTTP/1.1\r\n'
b'Host: example.com\r\n'
Expand Down Expand Up @@ -639,29 +641,34 @@ async def test_close(srv, transport) -> None:
srv.connection_made(transport)
await asyncio.sleep(0)

srv.handle_request = mock.Mock()
srv.handle_request.side_effect = helpers.noop

assert transport is srv.transport
handle_request = mock.Mock()
handle_request.side_effect = helpers.noop
with mock.patch.object(
web.RequestHandler,
'handle_request',
create=True,
new=handle_request
):
assert transport is srv.transport

srv._keepalive = True
srv.data_received(
b'GET / HTTP/1.1\r\n'
b'Host: example.com\r\n'
b'Content-Length: 0\r\n\r\n'
b'GET / HTTP/1.1\r\n'
b'Host: example.com\r\n'
b'Content-Length: 0\r\n\r\n')
srv._keepalive = True
srv.data_received(
b'GET / HTTP/1.1\r\n'
b'Host: example.com\r\n'
b'Content-Length: 0\r\n\r\n'
b'GET / HTTP/1.1\r\n'
b'Host: example.com\r\n'
b'Content-Length: 0\r\n\r\n')

await asyncio.sleep(0.05)
assert srv._task_handler
assert srv._waiter
await asyncio.sleep(0.05)
assert srv._task_handler
assert srv._waiter

srv.close()
await asyncio.sleep(0)
assert srv._task_handler is None
assert srv.transport is None
assert transport.close.called
srv.close()
await asyncio.sleep(0)
assert srv._task_handler is None
assert srv.transport is None
assert transport.close.called


async def test_pipeline_multiple_messages(
Expand Down

0 comments on commit d09d203

Please sign in to comment.