Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: don't set content-range on empty uploads #1070

Merged
merged 2 commits into from
Oct 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions googleapiclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,13 +1026,17 @@ def next_chunk(self, http=None, num_retries=0):
chunk_end = self.resumable_progress + len(data) - 1

headers = {
"Content-Range": "bytes %d-%d/%s"
% (self.resumable_progress, chunk_end, size),
# Must set the content-length header here because httplib can't
# calculate the size when working with _StreamSlice.
"Content-Length": str(chunk_end - self.resumable_progress + 1),
}

# An empty file results in chunk_end = -1 and size = 0
# sending "bytes 0--1/0" results in an invalid request
# Only add header "Content-Range" if chunk_end != -1
if chunk_end != -1:
headers["Content-Range"] = "bytes %d-%d/%s" % (self.resumable_progress, chunk_end, size)

for retry_num in range(num_retries + 1):
if retry_num > 0:
self._sleep(self._rand() * 2 ** retry_num)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,33 @@ def test_media_io_base_next_chunk_no_retry_403_not_configured(self):
request._sleep.assert_not_called()


def test_media_io_base_empty_file(self):
fd = BytesIO()
upload = MediaIoBaseUpload(
fd=fd, mimetype="image/png", chunksize=500, resumable=True
)

http = HttpMockSequence(
[
({"status": "200", "location": "https://www.googleapis.com/someapi/v1/upload?foo=bar"}, "{}"),
({"status": "200", "location": "https://www.googleapis.com/someapi/v1/upload?foo=bar"}, "{}")
]
)

model = JsonModel()
uri = u"https://www.googleapis.com/someapi/v1/upload/?foo=bar"
method = u"POST"
request = HttpRequest(
http, model.response, uri, method=method, headers={}, resumable=upload
)

request.execute()

# Check that "Content-Range" header is not set in the PUT request
self.assertTrue("Content-Range" not in http.request_sequence[-1][-1])
self.assertEqual("0", http.request_sequence[-1][-1]["Content-Length"])


class TestMediaIoBaseDownload(unittest.TestCase):
def setUp(self):
http = HttpMock(datafile("zoo.json"), {"status": "200"})
Expand Down