async def _stream() -> HTTPStream: stream = HTTPStream( AsyncMock(), Config(), WorkerContext(), AsyncMock(), False, None, None, AsyncMock(), 1 ) stream.app_put = AsyncMock() stream.config._log = AsyncMock(spec=Logger) return stream
async def test_send_invalid_message(stream: HTTPStream, status: Any, headers: Any, body: Any) -> None: stream.scope = {"method": "GET"} stream.state = ASGIHTTPState.REQUEST with pytest.raises((TypeError, ValueError)): await stream.app_send({ "type": "http.response.start", "headers": headers, "status": status }) await stream.app_send({"type": "http.response.body", "body": body})
async def test_send_push(stream: HTTPStream, http_scope: HTTPScope) -> None: stream.scope = http_scope stream.stream_id = 1 await stream.app_send({"type": "http.response.push", "path": "/push", "headers": []}) assert stream.send.call_args_list == [ # type: ignore call( Request( stream_id=1, headers=[(b":scheme", b"https")], http_version="2", method="GET", raw_path=b"/push", ) ) ]
async def test_handle_end_body(stream: HTTPStream) -> None: stream.app_put = AsyncMock() await stream.handle(EndBody(stream_id=1)) stream.app_put.assert_called() assert stream.app_put.call_args_list == [ call({"type": "http.request", "body": b"", "more_body": False}) ]
async def test_closed_app_send_noop(stream: HTTPStream) -> None: stream.closed = True await stream.app_send({ "type": "http.response.start", "status": 200, "headers": [] }) stream.send.assert_not_called()
async def test_send_invalid_message( stream: HTTPStream, http_scope: HTTPScope, status: Any, headers: Any, body: Any, ) -> None: stream.scope = http_scope stream.state = ASGIHTTPState.REQUEST with pytest.raises((TypeError, ValueError)): await stream.app_send( cast( HTTPResponseStartEvent, {"type": "http.response.start", "headers": headers, "status": status}, ) ) await stream.app_send( cast(HTTPResponseBodyEvent, {"type": "http.response.body", "body": body}) )
async def test_send_push(stream: HTTPStream) -> None: stream.scope = { "scheme": "https", "headers": [(b"host", b"hypercorn")], "http_version": "2" } stream.stream_id = 1 await stream.app_send({ "type": "http.response.push", "path": "/push", "headers": [] }) assert stream.send.call_args_list == [ call( Request( stream_id=1, headers=[(b":scheme", b"https"), (b":authority", b"hypercorn")], http_version="2", method="GET", raw_path=b"/push", )) ]
async def test_send_invalid_message_given_state( stream: HTTPStream, state: ASGIHTTPState, message_type: str ) -> None: stream.state = state with pytest.raises(UnexpectedMessageError): await stream.app_send({"type": message_type}) # type: ignore
async def _stream() -> HTTPStream: stream = HTTPStream(Config(), False, None, None, CoroutineMock(), CoroutineMock(), 1) stream.app_put = CoroutineMock() stream.config._log = AsyncMock(spec=Logger) return stream