From f897be58bf3b9eb589617b98bfbca3c0e188602d Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 24 Nov 2016 12:53:49 +0000 Subject: [PATCH] Make Response.content return None if raw is None Add test. --- requests/models.py | 2 +- tests/test_requests.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/requests/models.py b/requests/models.py index cce629bb..4e2dd78b 100644 --- a/requests/models.py +++ b/requests/models.py @@ -769,7 +769,7 @@ class Response(object): raise RuntimeError( 'The content for this response was already consumed') - if not self.status_code: + if self.status_code == 0 or self.raw is None: self._content = None else: self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes() diff --git a/tests/test_requests.py b/tests/test_requests.py index a0c47eea..b6667b51 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1094,6 +1094,10 @@ class TestRequests: total_seconds = ((td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6) assert total_seconds > 0.0 + def test_empty_response_has_content_none(self): + r = requests.Response() + assert r.content is None + def test_response_is_iterable(self): r = requests.Response() io = StringIO.StringIO('abc')