From: Tom Christie Date: Fri, 25 Sep 2020 10:29:17 +0000 (+0100) Subject: Fix automatic .read() when Response instances are created with content= (#1324) X-Git-Tag: 0.15.4~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=666cbbdfe8259e7b6363a2eec5df8ab60e7b6003;p=thirdparty%2Fhttpx.git Fix automatic .read() when Response instances are created with content= (#1324) --- diff --git a/httpx/_models.py b/httpx/_models.py index 03635f3e..4fe59d4c 100644 --- a/httpx/_models.py +++ b/httpx/_models.py @@ -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() diff --git a/tests/models/test_responses.py b/tests/models/test_responses.py index 2e383811..6241fd3e 100644 --- a/tests/models/test_responses.py +++ b/tests/models/test_responses.py @@ -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!")