From fd4733819f9775973f91048603ce08e959fbd375 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Sun, 23 Apr 2017 23:23:35 -0700 Subject: [PATCH] Dropping extra headers from transmit_next_chunk(). It appears that **only** the initiate() request requires them. --- google/resumable_media/upload.py | 14 ++++++++------ tests/unit/test_upload.py | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/google/resumable_media/upload.py b/google/resumable_media/upload.py index ff24de7a..5f1b73bc 100644 --- a/google/resumable_media/upload.py +++ b/google/resumable_media/upload.py @@ -257,9 +257,9 @@ class ResumableUpload(_UploadBase): upload_url (str): The URL where the resumable upload will be initiated. chunk_size (int): The size of each chunk used to upload the resource. headers (Optional[Mapping[str, str]]): Extra headers that should - be sent with the :meth:`initiate` request and each - :meth:`transmit_next_chunk` request, e.g. headers for encrypted - data. These **will not** be sent with a :meth:`recover` request. + be sent with the :meth:`initiate` request, e.g. headers for + encrypted data. These **will not** be sent with + :meth:`transmit_next_chunk` or :meth:`recover` requests. Raises: ValueError: If ``chunk_size`` is not a multiple of @@ -403,7 +403,9 @@ def _prepare_request(self): will (almost) certainly not be network I/O. Returns: - Tuple[bytes, dict]: The payload and headers for the request. + Tuple[bytes, dict]: The payload and headers for the request. The + headers **do not** incorporate the ``_headers`` on the + current instance. Raises: ValueError: If the current upload has finished. @@ -437,7 +439,6 @@ def _prepare_request(self): _CONTENT_TYPE_HEADER: self._content_type, _helpers.CONTENT_RANGE_HEADER: content_range, } - headers.update(self._headers) return payload, headers def _make_invalid(self): @@ -560,7 +561,8 @@ def _prepare_recover_request(self): the upload can end up :attr:`invalid` is if it has been initiated. Returns: - dict: The headers for the request. + dict: The headers for the request (they **do not** incorporate the + ``_headers`` on the current instance). Raises: ValueError: If the current upload is not in an invalid state. diff --git a/tests/unit/test_upload.py b/tests/unit/test_upload.py index 673efa7c..1338efa3 100644 --- a/tests/unit/test_upload.py +++ b/tests/unit/test_upload.py @@ -506,11 +506,12 @@ def test__prepare_request_success_with_headers(self): new_headers = self._prepare_request_helper(headers) assert new_headers is not headers expected_headers = { - u'cannot': u'touch this', u'content-range': u'bytes 0-32/33', u'content-type': BASIC_CONTENT, } assert new_headers == expected_headers + # Make sure the ``_headers`` are not incorporated. + assert u'cannot' not in new_headers def test__make_invalid(self): upload = upload_mod.ResumableUpload(RESUMABLE_URL, ONE_MB) @@ -649,6 +650,19 @@ def test__prepare_recover_request(self): # Make sure headers are untouched. assert upload._headers == {} + def test__prepare_recover_request_with_headers(self): + headers = {u'lake': u'ocean'} + upload = upload_mod.ResumableUpload( + RESUMABLE_URL, ONE_MB, headers=headers) + upload._invalid = True + + new_headers = upload._prepare_recover_request() + assert new_headers == {u'content-range': u'bytes */*'} + # Make sure the ``_headers`` are not incorporated. + assert u'lake' not in new_headers + # Make sure headers are untouched. + assert upload._headers == {u'lake': u'ocean'} + def test__process_recover_response_bad_status(self): upload = upload_mod.ResumableUpload(RESUMABLE_URL, ONE_MB) upload._invalid = True