Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions googleapiclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def progress(self):
the percentage complete as a float, returning 0.0 if the total size of
the upload is unknown.
"""
if self.total_size is not None:
if self.total_size is not None and self.total_size != 0:
return float(self.resumable_progress) / float(self.total_size)
else:
return 0.0
Expand All @@ -229,7 +229,7 @@ def progress(self):
the percentage complete as a float, returning 0.0 if the total size of
the download is unknown.
"""
if self.total_size is not None:
if self.total_size is not None and self.total_size != 0:
return float(self.resumable_progress) / float(self.total_size)
else:
return 0.0
Expand Down
24 changes: 24 additions & 0 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,30 @@ def test_pickle_with_credentials(self):
new_http = new_zoo._http
self.assertFalse(hasattr(new_http.request, 'credentials'))

def test_resumable_media_upload_no_content(self):
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', http=self.http)

media_upload = MediaFileUpload(datafile('empty'), resumable=True)
request = zoo.animals().insert(media_body=media_upload, body=None)

self.assertEquals(media_upload, request.resumable)
self.assertEquals(request.body, None)
self.assertEquals(request.resumable_uri, None)

http = HttpMockSequence([
({'status': '200',
'location': 'http://upload.example.com'}, ''),
({'status': '308',
'location': 'http://upload.example.com/2',
'range': '0-0'}, ''),
])

status, body = request.next_chunk(http=http)
self.assertEquals(None, body)
self.assertTrue(isinstance(status, MediaUploadProgress))
self.assertEquals(0, status.progress())


class Next(unittest.TestCase):

Expand Down
22 changes: 22 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,28 @@ def test_media_io_base_download_retries_5xx(self):
self.assertEqual(5, download._progress)
self.assertEqual(5, download._total_size)

def test_media_io_base_download_empty_file(self):
self.request.http = HttpMockSequence([
({'status': '200',
'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())

EXPECTED = """POST /someapi/v1/collection/?foo=bar HTTP/1.1
Content-Type: application/json
MIME-Version: 1.0
Expand Down