1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
import asyncio
from typing import List
import pytest
from gql import Client, gql
from gql.transport.exceptions import (
TransportConnectionFailed,
TransportProtocolError,
TransportQueryError,
)
from .conftest import WebSocketServerHelper
# Marking all tests in this file with the websockets marker
pytestmark = pytest.mark.websockets
invalid_query_str = """
query getContinents {
continents {
code
bloh
}
}
"""
invalid_query1_server_answer = (
'{{"type":"next","id":"{query_id}",'
'"payload":{{"errors":['
'{{"message":"Cannot query field \\"bloh\\" on type \\"Continent\\".",'
'"locations":[{{"line":4,"column":5}}],'
'"extensions":{{"code":"INTERNAL_SERVER_ERROR"}}}}]}}}}'
)
invalid_query1_server = [invalid_query1_server_answer]
@pytest.mark.asyncio
@pytest.mark.parametrize("graphqlws_server", [invalid_query1_server], indirect=True)
@pytest.mark.parametrize("query_str", [invalid_query_str])
async def test_graphqlws_invalid_query(client_and_graphqlws_server, query_str):
session, server = client_and_graphqlws_server
query = gql(query_str)
with pytest.raises(TransportQueryError) as exc_info:
await session.execute(query)
exception = exc_info.value
assert isinstance(exception.errors, List)
error = exception.errors[0]
assert error["extensions"]["code"] == "INTERNAL_SERVER_ERROR"
invalid_subscription_str = """
subscription getContinents {
continents {
code
bloh
}
}
"""
async def server_invalid_subscription(ws):
await WebSocketServerHelper.send_connection_ack(ws)
await ws.recv()
await ws.send(invalid_query1_server_answer.format(query_id=1))
await WebSocketServerHelper.send_complete(ws, 1)
await ws.wait_closed()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"graphqlws_server", [server_invalid_subscription], indirect=True
)
@pytest.mark.parametrize("query_str", [invalid_subscription_str])
async def test_graphqlws_invalid_subscription(client_and_graphqlws_server, query_str):
session, server = client_and_graphqlws_server
query = gql(query_str)
with pytest.raises(TransportQueryError) as exc_info:
async for result in session.subscribe(query):
pass
exception = exc_info.value
assert isinstance(exception.errors, List)
error = exception.errors[0]
assert error["extensions"]["code"] == "INTERNAL_SERVER_ERROR"
async def server_no_ack(ws):
await ws.wait_closed()
@pytest.mark.asyncio
@pytest.mark.parametrize("graphqlws_server", [server_no_ack], indirect=True)
@pytest.mark.parametrize("query_str", [invalid_query_str])
async def test_graphqlws_server_does_not_send_ack(graphqlws_server, query_str):
from gql.transport.websockets import WebsocketsTransport
url = f"ws://{graphqlws_server.hostname}:{graphqlws_server.port}/graphql"
transport = WebsocketsTransport(url=url, ack_timeout=0.1)
with pytest.raises(asyncio.TimeoutError):
async with Client(transport=transport):
pass
invalid_query_server_answer = (
'{"id":"1","type":"error","payload":[{"message":"Cannot query field '
'\\"helo\\" on type \\"Query\\". Did you mean \\"hello\\"?",'
'"locations":[{"line":2,"column":3}]}]}'
)
async def server_invalid_query(ws):
await WebSocketServerHelper.send_connection_ack(ws)
result = await ws.recv()
print(f"Server received: {result}")
await ws.send(invalid_query_server_answer)
await WebSocketServerHelper.wait_connection_terminate(ws)
await ws.wait_closed()
@pytest.mark.asyncio
@pytest.mark.parametrize("graphqlws_server", [server_invalid_query], indirect=True)
async def test_graphqlws_sending_invalid_query(client_and_graphqlws_server):
session, server = client_and_graphqlws_server
query = gql("{helo}")
with pytest.raises(TransportQueryError) as exc_info:
await session.execute(query)
exception = exc_info.value
assert isinstance(exception.errors, List)
error = exception.errors[0]
assert (
error["message"]
== 'Cannot query field "helo" on type "Query". Did you mean "hello"?'
)
not_json_answer = ["BLAHBLAH"]
missing_type_answer = ["{}"]
missing_id_answer_1 = ['{"type": "next"}']
missing_id_answer_2 = ['{"type": "error"}']
missing_id_answer_3 = ['{"type": "complete"}']
data_without_payload = ['{"type": "next", "id":"1"}']
error_without_payload = ['{"type": "error", "id":"1"}']
error_with_payload_not_a_list = ['{"type": "error", "id":"1", "payload": "NOT A LIST"}']
payload_is_not_a_dict = ['{"type": "next", "id":"1", "payload": "BLAH"}']
empty_payload = ['{"type": "next", "id":"1", "payload": {}}']
sending_bytes = [b"\x01\x02\x03"]
@pytest.mark.asyncio
@pytest.mark.parametrize(
"graphqlws_server",
[
not_json_answer,
missing_type_answer,
missing_id_answer_1,
missing_id_answer_2,
missing_id_answer_3,
data_without_payload,
error_without_payload,
payload_is_not_a_dict,
error_with_payload_not_a_list,
empty_payload,
sending_bytes,
],
indirect=True,
)
async def test_graphqlws_transport_protocol_errors(client_and_graphqlws_server):
session, server = client_and_graphqlws_server
query = gql("query { hello }")
with pytest.raises(TransportProtocolError):
await session.execute(query)
async def server_without_ack(ws):
# Sending something else than an ack
await WebSocketServerHelper.send_complete(ws, 1)
await ws.wait_closed()
@pytest.mark.asyncio
@pytest.mark.parametrize("graphqlws_server", [server_without_ack], indirect=True)
async def test_graphqlws_server_does_not_ack(graphqlws_server):
from gql.transport.websockets import WebsocketsTransport
url = f"ws://{graphqlws_server.hostname}:{graphqlws_server.port}/graphql"
print(f"url = {url}")
transport = WebsocketsTransport(url=url)
with pytest.raises(TransportProtocolError):
async with Client(transport=transport):
pass
async def server_closing_directly(ws):
await ws.close()
@pytest.mark.asyncio
@pytest.mark.parametrize("graphqlws_server", [server_closing_directly], indirect=True)
async def test_graphqlws_server_closing_directly(graphqlws_server):
from gql.transport.websockets import WebsocketsTransport
url = f"ws://{graphqlws_server.hostname}:{graphqlws_server.port}/graphql"
print(f"url = {url}")
transport = WebsocketsTransport(url=url)
with pytest.raises(TransportConnectionFailed):
async with Client(transport=transport):
pass
async def server_closing_after_ack(ws):
await WebSocketServerHelper.send_connection_ack(ws)
await ws.close()
@pytest.mark.asyncio
@pytest.mark.parametrize("graphqlws_server", [server_closing_after_ack], indirect=True)
async def test_graphqlws_server_closing_after_ack(client_and_graphqlws_server):
session, server = client_and_graphqlws_server
query = gql("query { hello }")
print("\n Trying to execute first query.\n")
with pytest.raises(TransportConnectionFailed) as exc1:
await session.execute(query)
exc1_cause = exc1.value.__cause__
exc1_cause_str = f"{type(exc1_cause).__name__}:{exc1_cause!s}"
print(f"\n First query Exception cause: {exc1_cause_str}\n")
assert (
exc1_cause_str == "ConnectionClosedOK:received 1000 (OK); then sent 1000 (OK)"
)
await session.transport.wait_closed()
print("\n Trying to execute second query.\n")
with pytest.raises(TransportConnectionFailed) as exc2:
await session.execute(query)
exc2_cause = exc2.value.__cause__
exc2_cause_str = f"{type(exc2_cause).__name__}:{exc2_cause!s}"
print(f" Second query Exception cause: {exc2_cause_str}\n")
assert (
exc2_cause_str == "ConnectionClosedOK:received 1000 (OK); then sent 1000 (OK)"
)
|