Repository: googleapis/google-resumable-media-python

Issue:
Allow space in-between checksums in response headers
**Summary:** I encountered this bug when downloading files using the Google Cloud Storage Python libraries, but the bug is coming from the Resumable Media library.  The current code (and [official docs](https://cloud.google.com/storage/docs/xml-api/reference-headers#xgooghash)) assumes the checksum headers will be contiguous without whitespace:
```
'x-goog-hash': 'crc32c=t6zgHw==,md5=zH3E9XwJPGGra4vnT+aamQ=='
```
However, I'm actually getting responses with whitespaces between each type of checksum:
```
'x-goog-hash': 'crc32c=t6zgHw==, md5=zH3E9XwJPGGra4vnT+aamQ=='
```

Because of this, the checksum of downloaded files aren't being verified.

Solution is to patch `_helpers.py` as follows:
```
def _parse_checksum_header(header_value, response, checksum_label):
    if header_value is None:
        return None

    matches = []
    for checksum in header_value.split(u","):
        name, value = checksum.split(u"=", 1)
        # Use lstrip to support both "," and ", " as delimiters
        if name.lstrip() == checksum_label:
            matches.append(value)

    if len(matches) == 0:
        return None
    elif len(matches) == 1:
        return matches[0]
    else:
        raise common.InvalidResponse(
            response,
            u"X-Goog-Hash header had multiple ``{}`` values.".format(checksum_label),
            header_value,
            matches,
        )
```

Relevant: #22, #31, #32 

Discussion:
google-cla[bot]: 
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed (or fixed any issues), please reply here with `@googlebot I signed it!` and we'll verify it.

----

#### What to do if you already signed the CLA

##### Individual signers

*   It's possible we don't have your GitHub username or you're using a different email address on your commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).

##### Corporate signers

*   Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to [go/cla#troubleshoot](http://go/cla#troubleshoot) ([Public version](https://opensource.google/docs/cla/#troubleshoot)).
*   The email used to register you as an authorized contributor must be the email used for the Git commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
*   The email used to register you as an authorized contributor must also be [attached to your GitHub account](https://github.com/settings/emails).
		

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fgoogle-resumable-media-python%2Fpull%2F170) for more info**.

<!-- need_sender_cla -->
google-cla[bot]: 
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed (or fixed any issues), please reply here with `@googlebot I signed it!` and we'll verify it.

----

#### What to do if you already signed the CLA

##### Individual signers

*   It's possible we don't have your GitHub username or you're using a different email address on your commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).

##### Corporate signers

*   Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to [go/cla#troubleshoot](http://go/cla#troubleshoot) ([Public version](https://opensource.google/docs/cla/#troubleshoot)).
*   The email used to register you as an authorized contributor must be the email used for the Git commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
*   The email used to register you as an authorized contributor must also be [attached to your GitHub account](https://github.com/settings/emails).
		

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fgoogle-resumable-media-python%2Fpull%2F170) for more info**.

<!-- need_sender_cla -->
crwilcox: Hi @mcsimps2 thank you for the contribution. Could I bother you to make a corresponding issue for this?
mcsimps2: @crwilcox It's mentioned in the summary at the bottom - it resolves #169.  I should've made it more clear that was the issue of concern, sorry about that!
crwilcox: @mcsimps2 Cool, I just missed it :)
crwilcox: Tests are reporting failed for linting, which is not your pr. I can create a separate PR and clean that up.

Patch:
diff --git a/google/resumable_media/_helpers.py b/google/resumable_media/_helpers.py
index f57827e..3e4acf2 100644
--- a/google/resumable_media/_helpers.py
+++ b/google/resumable_media/_helpers.py
@@ -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:


Test patch:
diff --git a/tests/unit/test__helpers.py b/tests/unit/test__helpers.py
index 7e1bb53..1297408 100644
--- a/tests/unit/test__helpers.py
+++ b/tests/unit/test__helpers.py
@@ -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)