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

add eof method to close stream #2094

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions sanic/response.py
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/test_response.py
Expand Up @@ -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