Skip to content

Commit

Permalink
Don't set a default mimetype for Test Responses
Browse files Browse the repository at this point in the history
The TestResponse should have a mimetype as defined by the response
itself. If the response is missing a mimetype then it shouldn't assume
one.

See #2450
  • Loading branch information
pgjones committed Jul 23, 2022
1 parent e1bfc33 commit b00ecce
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Version 2.2.0
to sansio/http.py. :issue:`2408`
- Extracted utility get_content_length, get_query_string, get_path_info
functions from wsgi.py. :pr:`2415`
- Don't assume a mimetype for test responses. :issue:`2450`


Version 2.1.3
Expand Down
2 changes: 1 addition & 1 deletion src/werkzeug/sansio/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Response:
default_status = 200

#: the default mimetype if none is provided.
default_mimetype = "text/plain"
default_mimetype: t.Optional[str] = "text/plain"

#: Warn if a cookie header exceeds this size. The default, 4093, should be
#: safely `supported by most browsers <cookie_>`_. A cookie larger than
Expand Down
7 changes: 7 additions & 0 deletions src/werkzeug/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,10 @@ class TestResponse(Response):
serving a file, call :meth:`close` to close any open files and
prevent Python showing a ``ResourceWarning``.
.. versionchanged:: 2.2
Set the ``default_mimetype`` to None to prevent a mimetype being
assumed if missing.
.. versionchanged:: 2.1
Removed deprecated behavior for treating the response instance
as a tuple.
Expand All @@ -1288,6 +1292,9 @@ class TestResponse(Response):
Test client methods always return instances of this class.
"""

default_mimetype = None
# Don't assume a mimetype, instead use whatever the response provides

request: Request
"""A request object with the environ used to make the request that
resulted in this response.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,16 @@ def app(request):

response = client.get("/%3f?") # escaped ? in path, and empty query string
assert response.text == "/?\n/%3f?"


def no_response_headers_app(environ, start_response):
"""A WSGI application which returns a resposne with no headers."""
response = Response("Response")
response.headers.clear()
return response(environ, start_response)


def test_no_content_type_header_addition():
c = Client(no_response_headers_app)
response = c.open()
assert response.headers == Headers([("Content-Length", "8")])

0 comments on commit b00ecce

Please sign in to comment.