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

fix: don't raise when downloading zero byte files #1074

Merged
merged 1 commit into from Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions googleapiclient/http.py
Expand Up @@ -745,8 +745,16 @@ def next_chunk(self, num_retries=0):
if self._total_size is None or self._progress == self._total_size:
self._done = True
return MediaDownloadProgress(self._progress, self._total_size), self._done
else:
raise HttpError(resp, content, uri=self._uri)
elif resp.status == 416:
# 416 is Range Not Satisfiable
# This typically occurs with a zero byte file
content_range = resp["content-range"]
length = content_range.rsplit("/", 1)[1]
self._total_size = int(length)
if self._total_size == 0:
self._done = True
return MediaDownloadProgress(self._progress, self._total_size), self._done
raise HttpError(resp, content, uri=self._uri)


class _StreamSlice(object):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_http.py
Expand Up @@ -672,6 +672,26 @@ def test_media_io_base_download_empty_file(self):
self.assertEqual(0, download._total_size)
self.assertEqual(0, status.progress())

def test_media_io_base_download_empty_file_416_response(self):
self.request.http = HttpMockSequence(
[({"status": "416", "content-range": "0-0/0"}, b"")]
)

download = MediaIoBaseDownload(fd=self.fd, request=self.request, chunksize=3)

self.assertEqual(self.fd, download._fd)
self.assertEqual(0, download._progress)
self.assertEqual(None, download._total_size)
self.assertEqual(False, download._done)
self.assertEqual(self.request.uri, download._uri)

status, done = download.next_chunk()

self.assertEqual(True, done)
self.assertEqual(0, download._progress)
self.assertEqual(0, download._total_size)
self.assertEqual(0, status.progress())

def test_media_io_base_download_unknown_media_size(self):
self.request.http = HttpMockSequence([({"status": "200"}, b"123")])

Expand Down