Skip to content

Commit

Permalink
fix: don't raise when downloading zero byte files (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Oct 22, 2020
1 parent 857eaf3 commit 86d8788
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
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

0 comments on commit 86d8788

Please sign in to comment.