* Don't set headers for responses with 1xx, 204 and 304 status code
* Fix test
Co-authored-by: Tom Christie <tom@tomchristie.com>
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")))
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)