Skip to content

Commit

Permalink
Not to remove entity headers for 412 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihweiLHBird committed Jun 4, 2024
1 parent f8e0a72 commit bc1c0e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 1 addition & 2 deletions sanic/response/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ def processed_headers(self) -> Iterator[Tuple[bytes, bytes]]:
Returns:
Iterator[Tuple[bytes, bytes]]: A list of header tuples encoded in bytes for sending
""" # noqa: E501
# TODO: Make a blacklist set of header names and then filter with that
if self.status in (304, 412): # Not Modified, Precondition Failed
if self.status == 304: # Not Modified
self.headers = remove_entity_headers(self.headers)
if has_message_body(self.status):
self.headers.setdefault("content-type", self.content_type)
Expand Down
15 changes: 14 additions & 1 deletion tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from aiofiles import os as async_os
from pytest import LogCaptureFixture

from sanic import Request, Sanic
from sanic import Request, Sanic, exceptions
from sanic.compat import Header
from sanic.constants import DEFAULT_HTTP_CONTENT_TYPE
from sanic.cookies import CookieJar
Expand Down Expand Up @@ -218,6 +218,19 @@ def test_no_content(json_app):
assert "Content-Length" not in response.headers


def test_412_should_has_content_length(app: Sanic):
@app.get("/failed-precondition")
async def failed_precondition(request: Request):
raise exceptions.SanicException("Failed Precondition", status_code=412)

request, response = app.test_client.get("/failed-precondition")
assert response.status == 412
assert "Content-Length" in response.headers
assert response.headers["Content-Length"] == str(
len(response.text.encode())
)


@pytest.fixture
def streaming_app(app):
@app.route("/")
Expand Down

0 comments on commit bc1c0e5

Please sign in to comment.