Skip to content

Commit

Permalink
fix: append existing headers in prepare_initiate_request (#314)
Browse files Browse the repository at this point in the history
* fix: append existing headers in prepare_initiate_request

* add test

* lint

* clarifying comments
  • Loading branch information
WildSunLove committed Mar 8, 2022
1 parent 87207bb commit dfaa317
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions google/resumable_media/_upload.py
Expand Up @@ -468,9 +468,12 @@ def _prepare_initiate_request(
parse_result = urllib.parse.urlparse(self.upload_url)
parsed_query = urllib.parse.parse_qs(parse_result.query)
if "x-goog-signature" in parsed_query or "X-Goog-Signature" in parsed_query:
headers = {_CONTENT_TYPE_HEADER: content_type}
# Deconstruct **self._headers first so that content type defined here takes priority
headers = {**self._headers, _CONTENT_TYPE_HEADER: content_type}
else:
# Deconstruct **self._headers first so that content type defined here takes priority
headers = {
**self._headers,
_CONTENT_TYPE_HEADER: "application/json; charset=UTF-8",
"x-upload-content-type": content_type,
}
Expand All @@ -484,7 +487,6 @@ def _prepare_initiate_request(
content_length = "{:d}".format(self._total_bytes)
headers["x-upload-content-length"] = content_length

headers.update(self._headers)
payload = json.dumps(metadata).encode("utf-8")
return _POST, self.upload_url, payload, headers

Expand Down
7 changes: 6 additions & 1 deletion tests/unit/test__upload.py
Expand Up @@ -453,7 +453,12 @@ def test_prepare_initiate_request_with_signed_url(self):
assert headers == expected_headers

def test__prepare_initiate_request_with_headers(self):
headers = {"caviar": "beluga", "top": "quark"}
# content-type header should be overwritten, the rest should stay
headers = {
"caviar": "beluga",
"top": "quark",
"content-type": "application/xhtml",
}
data, new_headers = self._prepare_initiate_request_helper(
upload_headers=headers
)
Expand Down

0 comments on commit dfaa317

Please sign in to comment.