From 07caa2ca9dcb83d5b3f9640499b9bb3ca1301f00 Mon Sep 17 00:00:00 2001 From: Liam Coatman Date: Thu, 30 Mar 2023 09:41:28 +0100 Subject: [PATCH 1/3] Handle case when headers is None --- sanic/response/types.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sanic/response/types.py b/sanic/response/types.py index 3f93855de0..f0e765c09c 100644 --- a/sanic/response/types.py +++ b/sanic/response/types.py @@ -520,7 +520,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 From 08777f90dd969d668772e6a454b03659dfa1725a Mon Sep 17 00:00:00 2001 From: Liam Coatman Date: Thu, 30 Mar 2023 16:00:04 +0100 Subject: [PATCH 2/3] Add test for response stream with default headers --- tests/test_request_stream.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/test_request_stream.py b/tests/test_request_stream.py index 1513f87876..1b4961b39b 100644 --- a/tests/test_request_stream.py +++ b/tests/test_request_stream.py @@ -2,9 +2,9 @@ import pytest -from sanic import Sanic +from sanic import Sanic, Request from sanic.blueprints import Blueprint -from sanic.response import json, text +from sanic.response import json, text, ResponseStream from sanic.views import HTTPMethodView from sanic.views import stream as stream_decorator @@ -621,3 +621,17 @@ async def read_chunk(): assert res == None app.run(access_log=False, single_process=True) + + +def test_response_stream_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" From 8c73261cf2121d179645a546a88a261cc10ef5d5 Mon Sep 17 00:00:00 2001 From: Liam Coatman Date: Mon, 3 Apr 2023 15:18:30 +0100 Subject: [PATCH 3/3] Move test --- tests/test_request_stream.py | 18 ++---------------- tests/test_response.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/tests/test_request_stream.py b/tests/test_request_stream.py index 1b4961b39b..1513f87876 100644 --- a/tests/test_request_stream.py +++ b/tests/test_request_stream.py @@ -2,9 +2,9 @@ import pytest -from sanic import Sanic, Request +from sanic import Sanic from sanic.blueprints import Blueprint -from sanic.response import json, text, ResponseStream +from sanic.response import json, text from sanic.views import HTTPMethodView from sanic.views import stream as stream_decorator @@ -621,17 +621,3 @@ async def read_chunk(): assert res == None app.run(access_log=False, single_process=True) - - -def test_response_stream_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" diff --git a/tests/test_response.py b/tests/test_response.py index 8d2d9e0879..65a875cd6e 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -23,6 +23,7 @@ from sanic.cookies import CookieJar from sanic.response import ( HTTPResponse, + ResponseStream, empty, file, file_stream, @@ -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"