Handle case when headers argument of ResponseStream constructor is None (#2729)

* Handle case when headers is None

* Add test for response stream with default headers

* Move test

---------

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
This commit is contained in:
Liam Coatman 2023-07-09 08:34:40 +01:00 committed by GitHub
parent c21999a248
commit 11a0b15194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -522,7 +522,9 @@ class ResponseStream:
headers: Optional[Union[Header, Dict[str, str]]] = None, headers: Optional[Union[Header, Dict[str, str]]] = None,
content_type: Optional[str] = None, content_type: Optional[str] = None,
): ):
if not isinstance(headers, Header): if headers is None:
headers = Header()
elif not isinstance(headers, Header):
headers = Header(headers) headers = Header(headers)
self.streaming_fn = streaming_fn self.streaming_fn = streaming_fn
self.status = status self.status = status

View File

@ -23,6 +23,7 @@ from sanic.compat import Header
from sanic.cookies import CookieJar from sanic.cookies import CookieJar
from sanic.response import ( from sanic.response import (
HTTPResponse, HTTPResponse,
ResponseStream,
empty, empty,
file, file,
file_stream, file_stream,
@ -943,3 +944,17 @@ def test_file_validating_304_response(
) )
assert response.status == 304 assert response.status == 304
assert response.body == b"" assert response.body == b""
def test_stream_response_with_default_headers(app: Sanic):
async def sample_streaming_fn(response_):
await response_.write("foo")
@app.route("/")
async def test(request: Request):
return ResponseStream(sample_streaming_fn, content_type="text/csv")
_, response = app.test_client.get("/")
assert response.text == "foo"
assert response.headers["Transfer-Encoding"] == "chunked"
assert response.headers["Content-Type"] == "text/csv"