add eof method to close stream (#2094)
* add eof method to close stream * Add eof test Co-authored-by: Ajay Gupta <ajay.gupta@1mg.com> Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
This commit is contained in:
parent
e21521f45c
commit
9b26358e63
@ -203,6 +203,9 @@ class StreamingHTTPResponse(BaseHTTPResponse):
|
|||||||
self.streaming_fn = None
|
self.streaming_fn = None
|
||||||
await super().send(*args, **kwargs)
|
await super().send(*args, **kwargs)
|
||||||
|
|
||||||
|
async def eof(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class HTTPResponse(BaseHTTPResponse):
|
class HTTPResponse(BaseHTTPResponse):
|
||||||
"""
|
"""
|
||||||
@ -235,6 +238,9 @@ class HTTPResponse(BaseHTTPResponse):
|
|||||||
self.headers = Header(headers or {})
|
self.headers = Header(headers or {})
|
||||||
self._cookies = None
|
self._cookies = None
|
||||||
|
|
||||||
|
async def eof(self):
|
||||||
|
await self.send("", True)
|
||||||
|
|
||||||
|
|
||||||
def empty(
|
def empty(
|
||||||
status=204, headers: Optional[Dict[str, str]] = None
|
status=204, headers: Optional[Dict[str, str]] = None
|
||||||
|
@ -529,3 +529,19 @@ def test_empty_response(app):
|
|||||||
request, response = app.test_client.get("/test")
|
request, response = app.test_client.get("/test")
|
||||||
assert response.content_type is None
|
assert response.content_type is None
|
||||||
assert response.body == b""
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user