From 146e7d71560f6256a0a42e15007e1ddb4a6c6848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Pito=C5=84?= Date: Mon, 5 Feb 2024 13:44:10 +0100 Subject: [PATCH] Fix websockets tests for new Starlette (#1151) --- tests/asgi/test_configuration.py | 4 +++- .../asgi/test_websockets_graphql_transport_ws.py | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/asgi/test_configuration.py b/tests/asgi/test_configuration.py index bdf6e069b..eb7e1a8ed 100644 --- a/tests/asgi/test_configuration.py +++ b/tests/asgi/test_configuration.py @@ -684,11 +684,13 @@ def test_init_wait_timeout_graphql_transport_ws( app = GraphQL(schema, websocket_handler=websocket_handler) client = TestClient(app) - with pytest.raises(WebSocketDisconnect, match="4408"): + with pytest.raises(WebSocketDisconnect) as exc_info: with client.websocket_connect("/", ["graphql-transport-ws"]) as ws: time.sleep(0.1) ws.receive_json() + assert exc_info.value.code == 4408 + @pytest.mark.xfail(reason="sometimes fails due to a race condition") def test_handle_connection_init_timeout_handler_executed_graphql_transport_ws( diff --git a/tests/asgi/test_websockets_graphql_transport_ws.py b/tests/asgi/test_websockets_graphql_transport_ws.py index 7aff2501e..5d8f1ad44 100644 --- a/tests/asgi/test_websockets_graphql_transport_ws.py +++ b/tests/asgi/test_websockets_graphql_transport_ws.py @@ -767,9 +767,12 @@ def test_too_many_connection_init_messages_graphql_transport_ws( response = ws.receive_json() assert response["type"] == GraphQLTransportWSHandler.GQL_CONNECTION_ACK ws.send_json({"type": GraphQLTransportWSHandler.GQL_CONNECTION_INIT}) - with pytest.raises(WebSocketDisconnect, match="4429"): + + with pytest.raises(WebSocketDisconnect) as exc_info: ws.receive_json() + assert exc_info.value.code == 4429 + def test_get_operation_type(): graphql_document = parse("subscription ping {ping} query other { dummy }") @@ -796,9 +799,12 @@ def test_connection_not_acknowledged_graphql_transport_ws( "payload": {"query": "subscription { ping }"}, } ) - with pytest.raises(WebSocketDisconnect, match="4401"): + + with pytest.raises(WebSocketDisconnect) as exc_info: ws.receive_json() + assert exc_info.value.code == 4401 + def test_duplicate_operation_id_graphql_transport_ws( client_graphql_transport_ws, @@ -822,9 +828,12 @@ def test_duplicate_operation_id_graphql_transport_ws( } ) ws.receive_json() - with pytest.raises(WebSocketDisconnect, match="4409"): + + with pytest.raises(WebSocketDisconnect) as exc_info: ws.receive_json() + assert exc_info.value.code == 4409 + def test_invalid_operation_id_is_handled_graphql_transport_ws( client_graphql_transport_ws,