|
| 1 | +import asyncio |
1 | 2 | import json |
| 3 | +from threading import Timer |
2 | 4 |
|
3 | 5 | import httpx |
4 | 6 | import pytest |
| 7 | +from ariadne.asgi import GQL_CONNECTION_INIT, GQL_START |
| 8 | +from websockets import connect |
5 | 9 |
|
6 | 10 | my_storage = {} |
7 | 11 |
|
@@ -81,8 +85,45 @@ async def create_blog(host, storage): |
81 | 85 | ) |
82 | 86 | json_response = json.loads(response.text) |
83 | 87 | assert ("errors" in json_response) == True |
| 88 | + assert json_response["data"]["createblog"]["id"] is not None |
84 | 89 |
|
85 | 90 |
|
86 | 91 | @pytest.mark.asyncio |
87 | 92 | async def test_create_blog(server, host, storage): |
88 | 93 | await create_blog(host, storage) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_subscription(server, host, storage): |
| 98 | + query = """ |
| 99 | + subscription reviewblog($token: String!) { |
| 100 | + reviewblog(token: $token) { |
| 101 | + errors |
| 102 | + id |
| 103 | + } |
| 104 | + } |
| 105 | + """ |
| 106 | + variables = {"token": f'Bearer {storage["token"]}'} |
| 107 | + ws = await connect(f"ws://{host}/", subprotocols=["graphql-ws"]) |
| 108 | + await ws.send(json.dumps({"type": GQL_CONNECTION_INIT})) |
| 109 | + await ws.send( |
| 110 | + json.dumps( |
| 111 | + {"type": GQL_START, "payload": {"query": query, "variables": variables},} |
| 112 | + ) |
| 113 | + ) |
| 114 | + received = await ws.recv() |
| 115 | + assert received == '{"type": "connection_ack"}' |
| 116 | + |
| 117 | + def delay_create_blog(server, host): |
| 118 | + loop = asyncio.new_event_loop() |
| 119 | + asyncio.set_event_loop(loop) |
| 120 | + loop.run_until_complete(create_blog(server, host)) |
| 121 | + |
| 122 | + timer = Timer(1.0, delay_create_blog, (server, host, storage)) |
| 123 | + timer.start() |
| 124 | + |
| 125 | + received = await ws.recv() |
| 126 | + await ws.close() |
| 127 | + json_response = json.loads(received) |
| 128 | + assert ("errors" in json_response) == False |
| 129 | + assert json_response["payload"]["data"]["reviewblog"]["id"] is not None |
0 commit comments