Fix automatic .read() when Response instances are created with content=<str> (#1324)

This commit is contained in:
Tom Christie 2020-09-25 11:29:17 +01:00 committed by GitHub
parent 320bfe1d0e
commit 666cbbdfe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -900,7 +900,7 @@ class Response:
headers, stream = encode_response(content, text, html, json)
self._prepare(headers)
self.stream = stream
if content is None or isinstance(content, bytes):
if content is None or isinstance(content, (bytes, str)):
# Load the response body, except for streaming content.
self.read()

View File

@ -38,6 +38,19 @@ def test_response():
assert not response.is_error
def test_response_content():
response = httpx.Response(200, content="Hello, world!")
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.text == "Hello, world!"
assert response.headers == httpx.Headers(
{
"Content-Length": "13",
}
)
def test_response_text():
response = httpx.Response(200, text="Hello, world!")