]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fix automatic .read() when Response instances are created with content=<str> (#1324)
authorTom Christie <tom@tomchristie.com>
Fri, 25 Sep 2020 10:29:17 +0000 (11:29 +0100)
committerGitHub <noreply@github.com>
Fri, 25 Sep 2020 10:29:17 +0000 (11:29 +0100)
httpx/_models.py
tests/models/test_responses.py

index 03635f3e38e22e9878619037773b3129048ffd78..4fe59d4c5030c70dd11f95940365867b333f8bc2 100644 (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()
 
index 2e38381185d532e4d436b59c835c92c96f190c05..6241fd3e22ef898804cdb9ea4f6648d4894b8576 100644 (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!")