Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not to remove entity headers for 412 responses #2951

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading