From cf3c99890d73f4bc8605c1cd7fbfa4ff1a28237e Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Mon, 27 Jun 2016 18:47:34 -0400 Subject: [PATCH] added in type check for chunk_size --- requests/models.py | 2 ++ tests/test_requests.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/requests/models.py b/requests/models.py index 67747405..fbb3c7e6 100644 --- a/requests/models.py +++ b/requests/models.py @@ -685,6 +685,8 @@ class Response(object): if self._content_consumed and isinstance(self._content, bool): raise StreamConsumedError() + elif not isinstance(chunk_size, int): + raise TypeError("chunk_size must be an int, it is instead a %s." % type(chunk_size)) # simulate reading small chunks of the content reused_chunks = iter_slices(self._content, chunk_size) diff --git a/tests/test_requests.py b/tests/test_requests.py index 9b614300..d2a2714a 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -980,6 +980,20 @@ class TestRequests: chunks = r.iter_content(decode_unicode=True) assert all(isinstance(chunk, str) for chunk in chunks) + def test_response_chunk_size_int(self): + """Ensure that chunk_size is passed as an integer, otherwise + raise a TypeError. + """ + r = requests.Response() + r.raw = io.BytesIO(b'the content') + chunks = r.iter_content(1) + assert all(len(chunk) == 1 for chunk in chunks) + + r = requests.Response() + r.raw = io.BytesIO(b'the content') + with pytest.raises(TypeError): + chunks = r.iter_content("1024") + def test_request_and_response_are_pickleable(self, httpbin): r = requests.get(httpbin('get'))