Skip to content

Commit

Permalink
Do not dump streamed response content
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Ratto committed Nov 3, 2020
1 parent 207ca64 commit 71ffcd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 4 additions & 1 deletion requests_toolbelt/utils/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 21 additions & 1 deletion tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class RequestResponseMixin(object):
]

httpresponse_spec = [
'closed',
'headers',
'reason',
'status',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit 71ffcd1

Please sign in to comment.