Skip to content

Commit

Permalink
align ping message with graphql-sse implementation (https://github.co…
Browse files Browse the repository at this point in the history
  • Loading branch information
danplischke committed Aug 29, 2024
1 parent cd3680a commit 7ebd431
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ariadne/asgi/handlers/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def __init__(
self.send_timeout = send_timeout
self.ping_interval = ping_interval or self.DEFAULT_PING_INTERVAL
self.encoding = encoding or "utf-8"
self.body = None
self.body = None # type: ignore

_headers: Dict[str, str] = {}
if headers is not None:
Expand Down Expand Up @@ -231,7 +231,7 @@ async def _ping(self, send: Send) -> None:
await send(
{
"type": "http.response.body",
"body": self.encode_event(GraphQLServerSentEvent(event="next")),
"body": ":\r\n\r\n".encode(self.encoding),
"more_body": True,
}
)
Expand Down
16 changes: 10 additions & 6 deletions tests/asgi/test_sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ def get_sse_events(response: Response) -> List[Dict[str, Any]]:
for event in response.text.split("\r\n\r\n"):
if len(event.strip()) == 0:
continue
event, data = event.split("\r\n", 1)
event = event.replace("event: ", "")
data = data.replace("data: ", "")
data = json.loads(data) if len(data) > 0 else None
events.append({"event": event, "data": data})
if "\r\n" not in event:
# ping message
events.append({"event": "", "data": None})
else:
event, data = event.split("\r\n", 1)
event = event.replace("event: ", "")
data = data.replace("data: ", "")
data = json.loads(data) if len(data) > 0 else None
events.append({"event": event, "data": data})
return events


Expand Down Expand Up @@ -154,7 +158,7 @@ def test_ping_is_send_sse(sse_client):
assert len(events) == 4
assert events[0]["event"] == "next"
assert events[0]["data"]["data"] == {"testSlow": "slow"}
assert events[1]["event"] == "next"
assert events[1]["event"] == ""
assert events[1]["data"] is None
assert events[2]["event"] == "next"
assert events[2]["data"]["data"] == {"testSlow": "slow"}
Expand Down

0 comments on commit 7ebd431

Please sign in to comment.