def test_retry_is_int(): response = ServerSentEvent(0, retry=1) assert response.retry == 1 with pytest.raises(TypeError) as ctx: response = ServerSentEvent(0, retry="ten").encode() assert str(ctx.value) == "retry argument must be int"
async def stream_response(self, send) -> None: await send({ "type": "http.response.start", "status": self.status_code, "headers": self.raw_headers, }) self._ping_task = self._loop.create_task( self._ping(send)) # type: ignore async for data in self.body_iterator: if AppStatus.should_exit: logger.debug(f"Caught signal. Stopping stream_response loop.") break if isinstance(data, dict): chunk = ServerSentEvent(**data).encode() elif data is None: chunk = self.comment_encode("NONE", sep=self.sep) else: chunk = ServerSentEvent(str(data), sep=self.sep).encode() logger.debug(f"[EventSourceResponse] chunk: {chunk.decode()}") await send({ "type": "http.response.body", "body": chunk, "more_body": True }) await send({ "type": "http.response.body", "body": b"", "more_body": False })
def test_multiline_data(stream_sep, line_sep): lines = line_sep.join(["foo", "bar", "xyz"]) result = ServerSentEvent(lines, event="event", sep=stream_sep).encode() assert ( result == "event: event{0}data: foo{0}data: bar{0}data: xyz{0}{0}".format( stream_sep ).encode() )
def test_server_sent_event(input, expected): print(input, expected) if isinstance(input, str): assert ServerSentEvent(input).encode() == expected else: assert ServerSentEvent(**input).encode() == expected
def test_custom_sep(sep): result = ServerSentEvent("foo", event="event", sep=sep).encode() assert result == "event: event{0}data: foo{0}{0}".format(sep).encode()