aio-libs/aiohttp-sse

Invalid event stream format for multiline data

decatur opened this issue · 3 comments

If data is multiline with \n as line separator (like in the provided examples/simple.py), the generated stream does not conform to the Server-Sent Events specification:

data: {
    "time": "Server Time : 2021-01-11 23:35:07.354653"
}

Expected is

data: {
data:    "time": "Server Time : 2021-01-11 23:35:07.354653"
data: }

Fix:
In aiohttp-sse.__init__, replace for chunk in data.split('\r\n'): with for chunk in data.splitlines():

Just checked

async def test_multiline_example(aiohttp_client):
    sep = "\n"
    lines = ["first", "second", "third"]
    data = sep.join(lines)

    async def handler(request: web.Request) -> web.StreamResponse:
        async with sse_response(request, sep=sep) as sse:
            await sse.send(data)
        return sse

    app = web.Application()
    app.router.add_route("GET", "/", handler)
    client = await aiohttp_client(app)

    async with client.get("/") as response:
        assert 200 == response.status
        text = await response.text()
        expected = sep.join([f"data: {line}" for line in lines]) + sep * 2
        assert text == expected

Test passed.
So, described behaviour seems to be already fixed and issue may be closed

If that's a new test you just wrote, how about we add that in a PR and close the issue with that?

If that's a new test you just wrote, how about we add that in a PR and close the issue with that?

Test proofing the issue is not actual - already exists in master

expected = (
"data: foo{0}data: bar{0}data: xyz{0}{0}"
"event: bar{0}data: foo{0}data: bar{0}data: xyz{0}{0}"
"id: xyz{0}event: bar{0}data: foo{0}data: bar{0}data: xyz{0}{0}"
"id: xyz{0}event: bar{0}data: foo{0}data: bar{0}data: xyz{0}"
"retry: 1{0}{0}"
)
assert streamed_data == expected.format(stream_sep)

But in the future I would like to simplify existing tests and divide them into separate test-cases (after typing PR will be done)