From 47ca68be3eef4ef60c5b8b5398dbd0775dacd3c2 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 7 Jan 2022 13:25:44 +0100 Subject: [PATCH 1/2] Don't set headers for responses with 1xx, 204 and 304 status code --- starlette/responses.py | 6 +++++- tests/test_responses.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/starlette/responses.py b/starlette/responses.py index da765cfa9..26d730540 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -71,7 +71,11 @@ def init_headers(self, headers: typing.Mapping[str, str] = None) -> None: populate_content_type = b"content-type" not in keys body = getattr(self, "body", None) - if body is not None and populate_content_length: + if ( + body is not None + and populate_content_length + and not (self.status_code < 200 or self.status_code in (204, 304)) + ): content_length = str(len(body)) raw_headers.append((b"content-length", content_length.encode("latin-1"))) diff --git a/tests/test_responses.py b/tests/test_responses.py index 150fe4795..23f1f02ab 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -1,4 +1,5 @@ import os +from typing import Callable import anyio import pytest @@ -366,3 +367,20 @@ def test_streaming_response_known_size(test_client_factory): client: TestClient = test_client_factory(app) response = client.get("/") assert response.headers["content-length"] == "10" + + +@pytest.mark.parametrize("status_code", (100, 204, 304)) +def test_no_content_length( + status_code: int, test_client_factory: Callable[..., TestClient] +): + app = Response(content=b"non_empty", status_code=status_code) + client = test_client_factory(app) + response = client.get("/") + assert "content-length" not in response.headers + + +def test_json_response_no_content_length(test_client_factory): + app = JSONResponse("null", status_code=204) + client = test_client_factory(app) + response = client.get("/") + assert "content-length" not in response.headers From 33eb15a6278a8d894c75a6d8f61e900a0d7112c6 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 7 Jan 2022 18:24:20 +0100 Subject: [PATCH 2/2] Fix test --- tests/test_responses.py | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/tests/test_responses.py b/tests/test_responses.py index 23f1f02ab..e2337bdca 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -1,5 +1,4 @@ import os -from typing import Callable import anyio import pytest @@ -334,6 +333,13 @@ def test_empty_response(test_client_factory): assert response.headers["content-length"] == "0" +def test_empty_204_response(test_client_factory): + app = Response(status_code=204) + client: TestClient = test_client_factory(app) + response = client.get("/") + assert "content-length" not in response.headers + + def test_non_empty_response(test_client_factory): app = Response(content="hi") client: TestClient = test_client_factory(app) @@ -367,20 +373,3 @@ def test_streaming_response_known_size(test_client_factory): client: TestClient = test_client_factory(app) response = client.get("/") assert response.headers["content-length"] == "10" - - -@pytest.mark.parametrize("status_code", (100, 204, 304)) -def test_no_content_length( - status_code: int, test_client_factory: Callable[..., TestClient] -): - app = Response(content=b"non_empty", status_code=status_code) - client = test_client_factory(app) - response = client.get("/") - assert "content-length" not in response.headers - - -def test_json_response_no_content_length(test_client_factory): - app = JSONResponse("null", status_code=204) - client = test_client_factory(app) - response = client.get("/") - assert "content-length" not in response.headers