Skip to content

Commit

Permalink
Make it easier to debug close reason assertions (#3659)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorJohn authored Oct 6, 2024
1 parent d7e8a9b commit 3484480
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 35 deletions.
5 changes: 3 additions & 2 deletions tests/http/clients/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,6 @@ def close_code(self) -> int:
assert self.ws.close_code is not None
return self.ws.close_code

def assert_reason(self, reason: str) -> None:
assert self._reason == reason
@property
def close_reason(self) -> Optional[str]:
return self._reason
5 changes: 3 additions & 2 deletions tests/http/clients/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,6 @@ def close_code(self) -> int:
assert self._close_code is not None
return self._close_code

def assert_reason(self, reason: str) -> None:
assert self._close_reason == reason
@property
def close_reason(self) -> Optional[str]:
return self._close_reason
3 changes: 2 additions & 1 deletion tests/http/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ def closed(self) -> bool: ...
@abc.abstractmethod
def close_code(self) -> int: ...

@property
@abc.abstractmethod
def assert_reason(self, reason: str) -> None: ...
def close_reason(self) -> Optional[str]: ...

async def __aiter__(self) -> AsyncGenerator[Message, None]:
while not self.closed:
Expand Down
5 changes: 3 additions & 2 deletions tests/http/clients/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,6 @@ def close_code(self) -> int:
assert self._close_code is not None
return self._close_code

def assert_reason(self, reason: str) -> None:
assert self._close_reason == reason
@property
def close_reason(self) -> Optional[str]:
return self._close_reason
5 changes: 3 additions & 2 deletions tests/http/clients/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,6 @@ def close_code(self) -> int:
assert self._close_code is not None
return self._close_code

def assert_reason(self, reason: str) -> None:
assert self._close_reason == reason
@property
def close_reason(self) -> Optional[str]:
return self._close_reason
36 changes: 18 additions & 18 deletions tests/websockets/test_graphql_transport_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def test_unknown_message_type(ws_raw: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Unknown message type: NOT_A_MESSAGE_TYPE")
assert ws.close_reason == "Unknown message type: NOT_A_MESSAGE_TYPE"


async def test_missing_message_type(ws_raw: WebSocketClient):
Expand All @@ -87,7 +87,7 @@ async def test_missing_message_type(ws_raw: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Failed to parse message")
assert ws.close_reason == "Failed to parse message"


async def test_parsing_an_invalid_message(ws_raw: WebSocketClient):
Expand All @@ -98,7 +98,7 @@ async def test_parsing_an_invalid_message(ws_raw: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Failed to parse message")
assert ws.close_reason == "Failed to parse message"


async def test_parsing_an_invalid_payload(ws_raw: WebSocketClient):
Expand All @@ -109,7 +109,7 @@ async def test_parsing_an_invalid_payload(ws_raw: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Failed to parse message")
assert ws.close_reason == "Failed to parse message"


async def test_ws_messages_must_be_text(ws_raw: WebSocketClient):
Expand All @@ -120,7 +120,7 @@ async def test_ws_messages_must_be_text(ws_raw: WebSocketClient):
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("WebSocket message type must be text")
assert ws.close_reason == "WebSocket message type must be text"


async def test_ws_messages_must_be_json(ws_raw: WebSocketClient):
Expand All @@ -131,7 +131,7 @@ async def test_ws_messages_must_be_json(ws_raw: WebSocketClient):
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("WebSocket message type must be text")
assert ws.close_reason == "WebSocket message type must be text"


async def test_ws_message_frame_types_cannot_be_mixed(ws_raw: WebSocketClient):
Expand All @@ -156,7 +156,7 @@ async def test_ws_message_frame_types_cannot_be_mixed(ws_raw: WebSocketClient):
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("WebSocket message type must be text")
assert ws.close_reason == "WebSocket message type must be text"


async def test_connection_init_timeout(
Expand All @@ -180,7 +180,7 @@ async def test_connection_init_timeout(
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4408
ws.assert_reason("Connection initialisation timeout")
assert ws.close_reason == "Connection initialisation timeout"


@pytest.mark.flaky
Expand Down Expand Up @@ -240,7 +240,7 @@ async def test_close_twice(
await ws.receive(timeout=0.5)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Invalid connection init payload")
assert ws.close_reason == "Invalid connection init payload"
transport_close.assert_not_called()


Expand All @@ -249,7 +249,7 @@ async def test_too_many_initialisation_requests(ws: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4429
ws.assert_reason("Too many initialisation requests")
assert ws.close_reason == "Too many initialisation requests"


async def test_ping_pong(ws: WebSocketClient):
Expand Down Expand Up @@ -320,7 +320,7 @@ async def test_unauthorized_subscriptions(ws_raw: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4401
ws.assert_reason("Unauthorized")
assert ws.close_reason == "Unauthorized"


async def test_duplicated_operation_ids(ws: WebSocketClient):
Expand All @@ -345,7 +345,7 @@ async def test_duplicated_operation_ids(ws: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4409
ws.assert_reason("Subscriber for sub1 already exists")
assert ws.close_reason == "Subscriber for sub1 already exists"


async def test_reused_operation_ids(ws: WebSocketClient):
Expand Down Expand Up @@ -409,7 +409,7 @@ async def test_subscription_syntax_error(ws: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Syntax Error: Expected Name, found <EOF>.")
assert ws.close_reason == "Syntax Error: Expected Name, found <EOF>."


async def test_subscription_field_errors(ws: WebSocketClient):
Expand Down Expand Up @@ -663,7 +663,7 @@ async def test_single_result_invalid_operation_selection(ws: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Can't get GraphQL operation type")
assert ws.close_reason == "Can't get GraphQL operation type"


async def test_single_result_execution_error(ws: WebSocketClient):
Expand Down Expand Up @@ -743,7 +743,7 @@ async def test_single_result_duplicate_ids_sub(ws: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4409
ws.assert_reason("Subscriber for sub1 already exists")
assert ws.close_reason == "Subscriber for sub1 already exists"


async def test_single_result_duplicate_ids_query(ws: WebSocketClient):
Expand Down Expand Up @@ -774,7 +774,7 @@ async def test_single_result_duplicate_ids_query(ws: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4409
ws.assert_reason("Subscriber for sub1 already exists")
assert ws.close_reason == "Subscriber for sub1 already exists"


async def test_injects_connection_params(ws_raw: WebSocketClient):
Expand Down Expand Up @@ -804,7 +804,7 @@ async def test_rejects_connection_params_not_dict(ws_raw: WebSocketClient):
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Invalid connection init payload")
assert ws.close_reason == "Invalid connection init payload"


@pytest.mark.parametrize(
Expand All @@ -820,7 +820,7 @@ async def test_rejects_connection_params_with_wrong_type(
data = await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4400
ws.assert_reason("Invalid connection init payload")
assert ws.close_reason == "Invalid connection init payload"


# timings can sometimes fail currently. Until this test is rewritten when
Expand Down
6 changes: 3 additions & 3 deletions tests/websockets/test_graphql_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ async def test_ws_messages_must_be_text(ws_raw: WebSocketClient):
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 1002
ws.assert_reason("WebSocket message type must be text")
assert ws.close_reason == "WebSocket message type must be text"


async def test_ws_messages_must_be_json(ws_raw: WebSocketClient):
Expand All @@ -300,7 +300,7 @@ async def test_ws_messages_must_be_json(ws_raw: WebSocketClient):
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 1002
ws.assert_reason("WebSocket message type must be text")
assert ws.close_reason == "WebSocket message type must be text"


async def test_ws_message_frame_types_cannot_be_mixed(ws_raw: WebSocketClient):
Expand All @@ -326,7 +326,7 @@ async def test_ws_message_frame_types_cannot_be_mixed(ws_raw: WebSocketClient):
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 1002
ws.assert_reason("WebSocket message type must be text")
assert ws.close_reason == "WebSocket message type must be text"


async def test_unknown_protocol_messages_are_ignored(ws_raw: WebSocketClient):
Expand Down
10 changes: 5 additions & 5 deletions tests/websockets/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def test_turning_off_graphql_ws(http_client_class: Type[HttpClient]):
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4406
ws.assert_reason("Subprotocol not acceptable")
assert ws.close_reason == "Subprotocol not acceptable"


async def test_turning_off_graphql_transport_ws(http_client_class: Type[HttpClient]):
Expand All @@ -27,7 +27,7 @@ async def test_turning_off_graphql_transport_ws(http_client_class: Type[HttpClie
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4406
ws.assert_reason("Subprotocol not acceptable")
assert ws.close_reason == "Subprotocol not acceptable"


async def test_turning_off_all_subprotocols(http_client_class: Type[HttpClient]):
Expand All @@ -40,15 +40,15 @@ async def test_turning_off_all_subprotocols(http_client_class: Type[HttpClient])
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4406
ws.assert_reason("Subprotocol not acceptable")
assert ws.close_reason == "Subprotocol not acceptable"

async with http_client.ws_connect(
"/graphql", protocols=[GRAPHQL_WS_PROTOCOL]
) as ws:
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4406
ws.assert_reason("Subprotocol not acceptable")
assert ws.close_reason == "Subprotocol not acceptable"


async def test_generally_unsupported_subprotocols_are_rejected(http_client: HttpClient):
Expand All @@ -58,7 +58,7 @@ async def test_generally_unsupported_subprotocols_are_rejected(http_client: Http
await ws.receive(timeout=2)
assert ws.closed
assert ws.close_code == 4406
ws.assert_reason("Subprotocol not acceptable")
assert ws.close_reason == "Subprotocol not acceptable"


async def test_clients_can_prefer_subprotocols(http_client_class: Type[HttpClient]):
Expand Down

0 comments on commit 3484480

Please sign in to comment.