From 7342d036fa025cbc9bd0df913fd06cf2b42b5fe3 Mon Sep 17 00:00:00 2001 From: Ajay Gupta Date: Mon, 29 Mar 2021 22:00:56 +0530 Subject: [PATCH 1/2] add eof method to close stream --- sanic/response.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sanic/response.py b/sanic/response.py index a67d24fe72..9ab110f68c 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -203,6 +203,9 @@ async def send(self, *args, **kwargs): self.streaming_fn = None await super().send(*args, **kwargs) + async def eof(self): + raise NotImplementedError + class HTTPResponse(BaseHTTPResponse): """ @@ -235,6 +238,9 @@ def __init__( self.headers = Header(headers or {}) self._cookies = None + async def eof(self): + await self.send("", True) + def empty( status=204, headers: Optional[Dict[str, str]] = None From 534a6df79df88120d3b0d10cd4837226756b2f7f Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 15 Apr 2021 09:54:53 +0300 Subject: [PATCH 2/2] Add eof test --- tests/test_response.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_response.py b/tests/test_response.py index b03da9963b..4c107eaec3 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -529,3 +529,19 @@ def handler(request): request, response = app.test_client.get("/test") assert response.content_type is None assert response.body == b"" + + +def test_direct_response_stream(app): + @app.route("/") + async def test(request): + response = await request.respond(content_type="text/csv") + await response.send("foo,") + await response.send("bar") + await response.eof() + return response + + _, response = app.test_client.get("/") + assert response.text == "foo,bar" + assert response.headers["Transfer-Encoding"] == "chunked" + assert response.headers["Content-Type"] == "text/csv" + assert "Content-Length" not in response.headers