From b1bd9bc86a802b7a636381afdb5c55125df537e9 Mon Sep 17 00:00:00 2001 From: jell-o-fishi Date: Sat, 21 Oct 2023 10:30:15 +0300 Subject: [PATCH] added cli unit tests --- tests/conftest.py | 2 +- tests/rsocket/test_cli_command.py | 43 +++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 68866800..e065ba63 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -119,7 +119,7 @@ async def pipe_tcp(unused_tcp_port): @pytest.fixture -async def lazy_pipe_tcp(aiohttp_raw_server, unused_tcp_port): # noqa: F811 +async def lazy_pipe_tcp(unused_tcp_port): # noqa: F811 logging.info('Testing transport tcp (explicitly) on port %s (lazy)', unused_tcp_port) yield functools.partial(pipe_factory_tcp, unused_tcp_port) diff --git a/tests/rsocket/test_cli_command.py b/tests/rsocket/test_cli_command.py index c948f109..82c49f62 100644 --- a/tests/rsocket/test_cli_command.py +++ b/tests/rsocket/test_cli_command.py @@ -1,20 +1,25 @@ import io import sys import tempfile +from asyncio import sleep import pytest +from asyncclick.testing import CliRunner from decoy import Decoy from rsocket.awaitable.awaitable_rsocket import AwaitableRSocket from rsocket.cli.command import parse_uri, build_composite_metadata, create_request_payload, get_metadata_value, \ create_setup_payload, normalize_data, normalize_limit_rate, RequestType, get_request_type, parse_headers, \ - normalize_metadata_mime_type, execute_request + normalize_metadata_mime_type, execute_request, command from rsocket.extensions.helpers import route, authenticate_simple, authenticate_bearer from rsocket.extensions.mimetypes import WellKnownMimeTypes from rsocket.frame import MAX_REQUEST_N -from rsocket.helpers import create_future +from rsocket.helpers import create_future, create_response from rsocket.payload import Payload +from rsocket.request_handler import BaseRequestHandler from tests.rsocket.helpers import create_data +from tests.tools.fixtures_quart import pipe_factory_quart_websocket +from tests.tools.fixtures_tcp import pipe_factory_tcp def test_parse_uri(): @@ -218,3 +223,37 @@ async def test_execute_request_fnf(decoy: Decoy): result = await execute_request(client, RequestType.fnf, 3, Payload()) assert result is None + + +async def test_execute_command_tcp_request(unused_tcp_port): + class Handler(BaseRequestHandler): + async def request_response(self, request: Payload): + await sleep(4) + return create_response(b'response from test') + + async with pipe_factory_tcp(unused_tcp_port, + client_arguments={}, + server_arguments={'handler_factory': Handler}): + runner = CliRunner() + uri = f'tcp://localhost:{unused_tcp_port}' + result = await runner.invoke(command, ['--request', uri]) + + assert result.stdout == 'response from test\n' + assert result.exit_code == 0 + + +async def test_execute_command_websocket_request(unused_tcp_port): + class Handler(BaseRequestHandler): + async def request_response(self, request: Payload): + await sleep(4) + return create_response(b'response from test') + + async with pipe_factory_quart_websocket(unused_tcp_port, + client_arguments={}, + server_arguments={'handler_factory': Handler}): + runner = CliRunner() + uri = f'ws://localhost:{unused_tcp_port}' + result = await runner.invoke(command, ['--request', uri]) + + assert result.stdout == 'response from test\n' + assert result.exit_code == 0