Skip to content

[PR #10880/3c9d7abf backport][3.11] Add invalid content type test docs #10885

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

Merged
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
7 changes: 6 additions & 1 deletion docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,12 @@ Response object

Returns value is ``'application/octet-stream'`` if no
Content-Type header present in HTTP headers according to
:rfc:`2616`. To make sure Content-Type header is not present in
:rfc:`9110`. If the *Content-Type* header is invalid (e.g., ``jpg``
instead of ``image/jpeg``), the value is ``text/plain`` by default
according to :rfc:`2045`. To see the original header check
``resp.headers['CONTENT-TYPE']``.

To make sure Content-Type header is not present in
the server reply, use :attr:`headers` or :attr:`raw_headers`, e.g.
``'CONTENT-TYPE' not in resp.headers``.

Expand Down
7 changes: 7 additions & 0 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,13 @@ def test_ctor_content_type_with_extra() -> None:
assert resp.headers["content-type"] == "text/plain; version=0.0.4; charset=utf-8"


def test_invalid_content_type_parses_to_text_plain() -> None:
resp = Response(text="test test", content_type="jpeg")

assert resp.content_type == "text/plain"
assert resp.headers["content-type"] == "jpeg; charset=utf-8"


def test_ctor_both_content_type_param_and_header_with_text() -> None:
with pytest.raises(ValueError):
Response(
Expand Down
Loading