From 71ffcd1dcd09881afcda264382fea51cef0b00e8 Mon Sep 17 00:00:00 2001 From: Andrea Ratto Date: Mon, 2 Nov 2020 17:10:55 +0100 Subject: [PATCH] Do not dump streamed response content --- requests_toolbelt/utils/dump.py | 5 ++++- tests/test_dump.py | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/requests_toolbelt/utils/dump.py b/requests_toolbelt/utils/dump.py index 23b35e75..5c19ae47 100644 --- a/requests_toolbelt/utils/dump.py +++ b/requests_toolbelt/utils/dump.py @@ -104,7 +104,10 @@ def _dump_response_data(response, prefixes, bytearr): bytearr.extend(prefix + b'\r\n') - bytearr.extend(response.content) + if not response.raw.closed: + bytearr.extend(b'<< Response body is being streamed >>') + else: + bytearr.extend(response.content) def _coerce_to_bytes(data): diff --git a/tests/test_dump.py b/tests/test_dump.py index da51f453..e4f2ce4a 100644 --- a/tests/test_dump.py +++ b/tests/test_dump.py @@ -98,6 +98,7 @@ class RequestResponseMixin(object): ] httpresponse_spec = [ + 'closed', 'headers', 'reason', 'status', @@ -137,8 +138,9 @@ def configure_request(self, body=b'', headers=None, method=None, self.request.url = url def configure_httpresponse(self, headers=None, reason=b'', status=200, - version=HTTP_1_1): + version=HTTP_1_1, closed=True): """Helper function to configure a mocked urllib3 response.""" + self.httpresponse.closed = closed self.httpresponse.headers = HTTPHeaderDict(headers or {}) self.httpresponse.reason = reason self.httpresponse.status = status @@ -327,6 +329,24 @@ def test_dump_response_data_with_unknown_http_version(self): assert b'response:HTTP/? 201 OK\r\n' in array assert b'response:Content-Type: application/json\r\n' in array + def test_dump_response_skips_body_when_streaming(self): + self.configure_response( + url='https://example.com/bigfile', + content=None, + reason=b'OK', + ) + + array = bytearray() + self.configure_httpresponse(closed=False) + prefixes = dump.PrefixSettings('request:', 'response:') + dump._dump_response_data( + response=self.response, + prefixes=prefixes, + bytearr=array, + ) + + assert array.endswith(b'<< Response body is being streamed >>') + class TestResponsePublicFunctions(RequestResponseMixin):