Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle connection_init and connection_terminate #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion graphene_subscriptions/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def websocket_receive(self, message):
id = request.get("id")

if request["type"] == "connection_init":
return
self._send_connection_ack()

elif request["type"] == "connection_terminate":
self.websocket_disconnect(message)

elif request["type"] == "start":
payload = request["payload"]
Expand Down Expand Up @@ -95,3 +98,15 @@ def _send_result(self, id, result):
),
}
)

def _send_connection_ack(self):
self.send(
{
"type": "websocket.send",
"text": json.dumps(
{
"type": "connection_ack",
}
),
}
)
31 changes: 31 additions & 0 deletions tests/test_consumers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from channels.testing import WebsocketCommunicator

from graphene_subscriptions.consumers import GraphqlSubscriptionConsumer


@pytest.mark.asyncio
async def test_consumer_connection_init():
communicator = WebsocketCommunicator(GraphqlSubscriptionConsumer, "/graphql/")
connected, subprotocol = await communicator.connect()
assert connected

await communicator.send_json_to({"type": "connection_init"})

response = await communicator.receive_json_from()

assert response["type"] == "connection_ack"


@pytest.mark.asyncio
async def test_consumer_connection_terminate():
communicator = WebsocketCommunicator(GraphqlSubscriptionConsumer, "/graphql/")
connected, subprotocol = await communicator.connect()
assert connected

await communicator.send_json_to({"type": "connection_terminate"})

response = await communicator.receive_output()

assert response["type"] == "websocket.close"
assert response["code"] == 1000