Skip to content
Draft
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
11 changes: 6 additions & 5 deletions requests_toolbelt/downloadutils/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ def stream_response_to_file(response, path=None, chunksize=_DEFAULT_CHUNKSIZE):
raise exc.StreamingError("File already exists: %s" % filename)
fd = open(filename, 'wb')

for chunk in response.iter_content(chunk_size=chunksize):
fd.write(chunk)

if not pre_opened:
fd.close()
try:
for chunk in response.iter_content(chunk_size=chunksize):
fd.write(chunk)
finally:
if not pre_opened:
fd.close()

return filename
71 changes: 71 additions & 0 deletions tests/test_downloadutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,74 @@ def test_tee_to_bytearray(streamed_response):
def test_tee_to_bytearray_only_accepts_bytearrays():
with pytest.raises(TypeError):
tee.tee_to_bytearray(None, object())


class DownloadResponse(object):
def __init__(self, failure):
self.error = requests.exceptions.ChunkedEncodingError('interrupted response')
self.failure = failure

def iter_content(self, chunk_size):
yield b'first'
if self.failure == 'read':
raise self.error
yield b'second'


class FailingDownloadFile(object):
def __init__(self, fileobj):
self.fileobj = fileobj
self.error = IOError('disk full')

def write(self, chunk):
raise self.error

def close(self):
self.fileobj.close()

@property
def closed(self):
return self.fileobj.closed


@pytest.mark.parametrize('owned', [True, False])
@pytest.mark.parametrize('failure', ['none', 'read', 'write'])
def test_download_destination_lifecycle(tmpdir, monkeypatch, owned, failure):
destination = str(tmpdir.join('download.bin'))
outputs = []
response = DownloadResponse(failure)

def record_open(path, mode):
fileobj = open(path, mode)
output = FailingDownloadFile(fileobj) if failure == 'write' else fileobj
outputs.append(output)
return output

monkeypatch.setattr(stream, 'open', record_open, raising=False)
if owned:
argument = destination
else:
output = FailingDownloadFile(io.BytesIO()) if failure == 'write' else io.BytesIO()
outputs.append(output)
argument = output
try:
if failure == 'none':
result = stream.stream_response_to_file(response, argument)
assert result == (destination if owned else None)
else:
error_type = requests.exceptions.ChunkedEncodingError if failure == 'read' else IOError
with pytest.raises(error_type) as caught:
stream.stream_response_to_file(response, argument)
error = response.error if failure == 'read' else outputs[0].error
assert caught.value is error
output = outputs[0]
assert output.closed is owned
if failure == 'none':
if owned:
with open(destination, 'rb') as downloaded:
assert downloaded.read() == b'firstsecond'
else:
assert output.getvalue() == b'firstsecond'
finally:
for output in outputs:
output.close()