Beispiel #1
0
async def test_websocket_completion() -> None:
    # Ensure that the connecion callable returns on completion
    app = Quart(__name__)
    scope = {
        'headers': [(b'host', b'quart')],
        'http_version': '1.1',
        'method': 'GET',
        "root_path": "",
        'scheme': 'wss',
        'path': '/',
        'query_string': b'',
        'subprotocols': [],
        'extensions': {'websocket.http.response': {}},
    }
    connection = ASGIWebsocketConnection(app, scope)

    queue: asyncio.Queue = asyncio.Queue()
    queue.put_nowait({'type': 'websocket.connect'})

    async def receive() -> dict:
        # This will block after returning the first and only entry
        return await queue.get()

    async def send(message: dict) -> None:
        pass

    # This test fails if a timeout error is raised here
    await asyncio.wait_for(connection(receive, send), timeout=1)
Beispiel #2
0
async def test_websocket_completion() -> None:
    # Ensure that the connecion callable returns on completion
    app = Quart(__name__)
    scope: WebsocketScope = {
        "type": "websocket",
        "asgi": {},
        "http_version": "1.1",
        "scheme": "wss",
        "path": "/",
        "raw_path": b"/",
        "query_string": b"",
        "root_path": "",
        "headers": [(b"host", b"quart")],
        "client": ("127.0.0.1", 80),
        "server": None,
        "subprotocols": [],
        "extensions": {"websocket.http.response": {}},
    }
    connection = ASGIWebsocketConnection(app, scope)

    queue: asyncio.Queue = asyncio.Queue()
    queue.put_nowait({"type": "websocket.connect"})

    async def receive() -> ASGIReceiveEvent:
        # This will block after returning the first and only entry
        return await queue.get()

    async def send(message: ASGISendEvent) -> None:
        pass

    # This test fails if a timeout error is raised here
    await asyncio.wait_for(connection(receive, send), timeout=1)
Beispiel #3
0
def test_websocket_path_from_absolute_target() -> None:
    app = Quart(__name__)
    scope = {
        'headers': [(b'host', b'quart')],
        'http_version': '1.1',
        'method': 'GET',
        "root_path": "",
        'scheme': 'wss',
        'path': 'ws://quart/path',
        'query_string': b'',
        'subprotocols': [],
        'extensions': {'websocket.http.response': {}},
    }
    connection = ASGIWebsocketConnection(app, scope)
    websocket = connection._create_websocket_from_scope(lambda: None)
    assert websocket.path == '/path'
Beispiel #4
0
async def test_websocket_accept_connection_warns(websocket_scope: WebsocketScope) -> None:
    connection = ASGIWebsocketConnection(Quart(__name__), websocket_scope)

    async def mock_send(message: ASGISendEvent) -> None:
        pass

    with pytest.warns(None):
        await connection.accept_connection(mock_send, Headers({"a": "b"}), None)
Beispiel #5
0
async def test_websocket_accept_connection_warns() -> None:
    connection = ASGIWebsocketConnection(Quart(__name__), {})

    async def mock_send(message: dict) -> None:
        pass

    with pytest.warns(None):
        await connection.accept_connection(mock_send, Headers({"a": "b"}), None)
Beispiel #6
0
def test_websocket_path_from_absolute_target() -> None:
    app = Quart(__name__)
    scope = {
        "headers": [(b"host", b"quart")],
        "http_version": "1.1",
        "method": "GET",
        "scheme": "wss",
        "path": "ws://quart/path",
        "query_string": b"",
        "subprotocols": [],
        "extensions": {
            "websocket.http.response": {}
        },
    }
    connection = ASGIWebsocketConnection(app, scope)
    websocket = connection._create_websocket_from_scope(lambda: None)
    assert websocket.path == "/path"
Beispiel #7
0
def test_websocket_path_from_absolute_target() -> None:
    app = Quart(__name__)
    scope: WebsocketScope = {
        "type": "websocket",
        "asgi": {},
        "http_version": "1.1",
        "scheme": "wss",
        "path": "ws://quart/path",
        "raw_path": b"/",
        "query_string": b"",
        "root_path": "",
        "headers": [(b"host", b"quart")],
        "client": ("127.0.0.1", 80),
        "server": None,
        "subprotocols": [],
        "extensions": {"websocket.http.response": {}},
    }
    connection = ASGIWebsocketConnection(app, scope)
    websocket = connection._create_websocket_from_scope(lambda: None)  # type: ignore
    assert websocket.path == "/path"
Beispiel #8
0
async def test_websocket_accept_connection(
        scope: dict, headers: Headers, subprotocol: Optional[str], has_headers: bool,
) -> None:
    connection = ASGIWebsocketConnection(Quart(__name__), scope)
    mock_send = CoroutineMock()
    await connection.accept_connection(mock_send, headers, subprotocol)

    if has_headers:
        mock_send.assert_called_with({
            "subprotocol": subprotocol,
            "type": "websocket.accept",
            "headers": _encode_headers(headers),
        })
    else:
        mock_send.assert_called_with({
            "subprotocol": subprotocol,
            "type": "websocket.accept",
        })
Beispiel #9
0
async def test_websocket_accept_connection(
    scope: dict,
    headers: Headers,
    subprotocol: Optional[str],
    has_headers: bool,
) -> None:
    connection = ASGIWebsocketConnection(Quart(__name__), scope)
    sent_message = None

    async def mock_send(message: dict) -> None:
        nonlocal sent_message
        sent_message = message

    await connection.accept_connection(mock_send, headers, subprotocol)

    if has_headers:
        assert sent_message["headers"]
    if subprotocol is not None:
        assert sent_message["subprotocol"] == subprotocol