Skip to content

Commit

Permalink
Handle case when headers argument of ResponseStream constructor i…
Browse files Browse the repository at this point in the history
…s `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>
  • Loading branch information
liamcoatman and ahopkins committed Jul 9, 2023
1 parent c21999a commit 11a0b15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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"

0 comments on commit 11a0b15

Please sign in to comment.