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

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

Merged
merged 5 commits into from
Jul 9, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sanic/response/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ def __init__(
headers: Optional[Union[Header, Dict[str, 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)
self.streaming_fn = streaming_fn
self.status = status
Expand Down
15 changes: 15 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from sanic.cookies import CookieJar
from sanic.response import (
HTTPResponse,
ResponseStream,
empty,
file,
file_stream,
Expand Down Expand Up @@ -943,3 +944,17 @@ def test_file_validating_304_response(
)
assert response.status == 304
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"