Skip to content

Commit

Permalink
fix: allow space in checksum header (#170)
Browse files Browse the repository at this point in the history
The current checksum logic only allowed checksum
 headers in the format "crc32c=hash1,md5=hash2",
 but GCP may actually return them with a space
 in-between as "crc32c=hash1, md5=hash2".

Resolves #169

Co-authored-by: Christopher Wilcox <crwilcox@google.com>
  • Loading branch information
mcsimps2 and crwilcox committed Sep 10, 2020
1 parent cdea3ee commit 224fc98
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion google/resumable_media/_helpers.py
Expand Up @@ -317,7 +317,8 @@ def _parse_checksum_header(header_value, response, checksum_label):
matches = []
for checksum in header_value.split(u","):
name, value = checksum.split(u"=", 1)
if name == checksum_label:
# Official docs say "," is the separator, but real-world responses have encountered ", "
if name.lstrip() == checksum_label:
matches.append(value)

if len(matches) == 0:
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test__helpers.py
Expand Up @@ -311,11 +311,12 @@ def test__DoNothingHash():


class Test__get_expected_checksum(object):
@pytest.mark.parametrize("template", [u"crc32c={},md5={}", u"crc32c={}, md5={}"])
@pytest.mark.parametrize("checksum", ["md5", "crc32c"])
@mock.patch("google.resumable_media._helpers._LOGGER")
def test__w_header_present(self, _LOGGER, checksum):
def test__w_header_present(self, _LOGGER, template, checksum):
checksums = {"md5": u"b2twdXNodGhpc2J1dHRvbg==", "crc32c": u"3q2+7w=="}
header_value = u"crc32c={},md5={}".format(checksums["crc32c"], checksums["md5"])
header_value = template.format(checksums["crc32c"], checksums["md5"])
headers = {_helpers._HASH_HEADER: header_value}
response = _mock_response(headers=headers)

Expand Down

0 comments on commit 224fc98

Please sign in to comment.