Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle streamed content #300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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