async def _protocol(monkeypatch: MonkeyPatch) -> H11Protocol: MockHTTPStream = AsyncMock() # noqa: N806 MockHTTPStream.return_value = AsyncMock(spec=HTTPStream) monkeypatch.setattr(hypercorn.protocol.h11, "HTTPStream", MockHTTPStream) MockEvent = AsyncMock() # noqa: N806 MockEvent.return_value = AsyncMock(spec=IOEvent) return H11Protocol(Config(), False, None, None, CoroutineMock(), CoroutineMock(), MockEvent)
async def test_protocol_instant_recycle(protocol: H11Protocol) -> None: data = b"GET / HTTP/1.1\r\nHost: hypercorn\r\n\r\n" # This test requires a real event as the handling should pause on # the instant receipt protocol.can_read = EventWrapper() await protocol.handle(RawData(data=data)) assert protocol.stream is not None await protocol.stream_send( Response(stream_id=1, status_code=200, headers=[])) await protocol.stream_send(EndBody(stream_id=1)) task = asyncio.ensure_future(protocol.handle(RawData(data=data))) await asyncio.sleep(0) # Switch to task await protocol.stream_send(StreamClosed(stream_id=1)) # Should have recycled, i.e. a stream should exist assert protocol.stream is not None await asyncio.sleep(0) # Switch to task assert task.done()
async def _protocol(monkeypatch: MonkeyPatch) -> H11Protocol: MockHTTPStream = Mock() # noqa: N806 MockHTTPStream.return_value = AsyncMock(spec=HTTPStream) monkeypatch.setattr(hypercorn.protocol.h11, "HTTPStream", MockHTTPStream) context = Mock() context.event_class.return_value = AsyncMock(spec=IOEvent) return H11Protocol(AsyncMock(), Config(), context, False, None, None, AsyncMock())
async def test_protocol_instant_recycle( protocol: H11Protocol, event_loop: asyncio.AbstractEventLoop) -> None: # This test task acts as the asgi app, spawned tasks act as the # server. data = b"GET / HTTP/1.1\r\nHost: hypercorn\r\n\r\n" # This test requires a real event as the handling should pause on # the instant receipt protocol.can_read = EventWrapper() task = event_loop.create_task(protocol.handle(RawData(data=data))) await asyncio.sleep(0) # Switch to task assert protocol.stream is not None assert task.done() await protocol.stream_send( Response(stream_id=1, status_code=200, headers=[])) await protocol.stream_send(EndBody(stream_id=1)) task = event_loop.create_task(protocol.handle(RawData(data=data))) await asyncio.sleep(0) # Switch to task await protocol.stream_send(StreamClosed(stream_id=1)) await asyncio.sleep(0) # Switch to task # Should have recycled, i.e. a stream should exist assert protocol.stream is not None assert task.done()
async def test_protocol_handle_max_incomplete(monkeypatch: MonkeyPatch) -> None: config = Config() config.h11_max_incomplete_size = 5 MockHTTPStream = AsyncMock() # noqa: N806 MockHTTPStream.return_value = AsyncMock(spec=HTTPStream) monkeypatch.setattr(hypercorn.protocol.h11, "HTTPStream", MockHTTPStream) MockEvent = AsyncMock() # noqa: N806 MockEvent.return_value = AsyncMock(spec=IOEvent) protocol = H11Protocol(config, False, None, None, CoroutineMock(), CoroutineMock(), MockEvent) await protocol.handle(RawData(data=b"GET / HTTP/1.1\r\nHost: hypercorn\r\n")) protocol.send.assert_called() assert protocol.send.call_args_list == [ call( RawData( data=b"HTTP/1.1 400 \r\ncontent-length: 0\r\nconnection: close\r\n" b"date: Thu, 01 Jan 1970 01:23:20 GMT\r\nserver: hypercorn-h11\r\n\r\n" ) ), call(RawData(data=b"")), call(Closed()), ]
async def test_protocol_send_end_data(protocol: H11Protocol) -> None: protocol.stream = AsyncMock() await protocol.stream_send(EndData(stream_id=1)) assert protocol.stream is not None