async def test_send_reject(stream: WSStream) -> None: await stream.handle( Request( stream_id=1, http_version="2", headers=[(b"sec-websocket-version", b"13")], raw_path=b"/", method="GET", )) await stream.app_send( cast( WebsocketResponseStartEvent, { "type": "websocket.http.response.start", "status": 200, "headers": [] }, ), ) assert stream.state == ASGIWebsocketState.HANDSHAKE # Must wait for response before sending anything stream.send.assert_not_called() # type: ignore await stream.app_send( cast(WebsocketResponseBodyEvent, { "type": "websocket.http.response.body", "body": b"Body" })) assert stream.state == ASGIWebsocketState.HTTPCLOSED stream.send.assert_called() # type: ignore assert stream.send.call_args_list == [ # type: ignore call(Response(stream_id=1, headers=[], status_code=200)), call(Body(stream_id=1, data=b"Body")), call(EndBody(stream_id=1)), ] stream.config._log.access.assert_called() # type: ignore
async def test_send_response(stream: HTTPStream) -> None: await stream.handle( Request(stream_id=1, http_version="2", headers=[], raw_path=b"/?a=b", method="GET")) await stream.app_send( cast(HTTPResponseStartEvent, { "type": "http.response.start", "status": 200, "headers": [] })) assert stream.state == ASGIHTTPState.REQUEST # Must wait for response before sending anything stream.send.assert_not_called() # type: ignore await stream.app_send( cast(HTTPResponseBodyEvent, { "type": "http.response.body", "body": b"Body" })) assert stream.state == ASGIHTTPState.CLOSED stream.send.assert_called() # type: ignore assert stream.send.call_args_list == [ # type: ignore call(Response(stream_id=1, headers=[], status_code=200)), call(Body(stream_id=1, data=b"Body")), call(EndBody(stream_id=1)), call(StreamClosed(stream_id=1)), ] stream.config._log.access.assert_called() # type: ignore
async def test_handle_body(stream: HTTPStream) -> None: await stream.handle(Body(stream_id=1, data=b"data")) stream.app_put.assert_called() # type: ignore assert stream.app_put.call_args_list == [ # type: ignore call({ "type": "http.request", "body": b"data", "more_body": True }) ]
async def test_protocol_send_body(protocol: H11Protocol) -> None: await protocol.handle( RawData(data=b"GET / HTTP/1.1\r\nHost: hypercorn\r\nConnection: close\r\n\r\n") ) await protocol.stream_send( Response(stream_id=1, status_code=200, headers=[(b"content-length", b"5")]) ) await protocol.stream_send(Body(stream_id=1, data=b"hello")) protocol.send.assert_called() assert protocol.send.call_args_list == [ call( RawData( data=b"HTTP/1.1 200 \r\ncontent-length: 5\r\ndate: Thu, 01 Jan 1970 01:23:20 GMT\r\nserver: hypercorn-h11\r\nconnection: close\r\n\r\n" # noqa: E501 ) ), call(RawData(data=b"hello")), ]
async def test_invalid_server_name(stream: HTTPStream) -> None: stream.config.server_names = ["hypercorn"] await stream.handle( Request( stream_id=1, http_version="2", headers=[(b"host", b"example.com")], raw_path=b"/", method="GET", )) assert stream.send.call_args_list == [ # type: ignore call( Response( stream_id=1, headers=[(b"content-length", b"0"), (b"connection", b"close")], status_code=404, )), call(EndBody(stream_id=1)), ] # This shouldn't error await stream.handle(Body(stream_id=1, data=b"Body"))