From e22e43acb451e97fd8693d69965887ef4b6906b1 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Wed, 2 Sep 2020 15:26:02 -0400 Subject: [PATCH] fix(blob): base64 includes additional characters (#258) Hashes were not being parsed correctly. I forgot that base64 includes the "+" and "/" characters. https://en.wikipedia.org/wiki/Base64 image --- google/cloud/storage/blob.py | 2 +- tests/unit/test_blob.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/google/cloud/storage/blob.py b/google/cloud/storage/blob.py index 31aeb05ef..2c28f69fc 100644 --- a/google/cloud/storage/blob.py +++ b/google/cloud/storage/blob.py @@ -813,7 +813,7 @@ def _extract_headers_from_download(self, response): digests = {} for encoded_digest in x_goog_hash.split(","): - match = re.match(r"(crc32c|md5)=([\w\d/]+={0,3})", encoded_digest) + match = re.match(r"(crc32c|md5)=([\w\d/\+/]+={0,3})", encoded_digest) if match: method, digest = match.groups() digests[method] = digest diff --git a/tests/unit/test_blob.py b/tests/unit/test_blob.py index 9bf60d42d..0c9e5928d 100644 --- a/tests/unit/test_blob.py +++ b/tests/unit/test_blob.py @@ -1522,6 +1522,24 @@ def test_download_as_string_w_response_headers(self): self.assertEqual(blob.md5_hash, "CS9tHYTtyFntzj7B9nkkJQ==") self.assertEqual(blob.crc32c, "4gcgLQ==") + response = self._mock_requests_response( + http_client.OK, + headers={ + "Content-Type": "application/octet-stream", + "Content-Language": "en-US", + "Cache-Control": "max-age=1337;public", + "Content-Encoding": "gzip", + "X-Goog-Storage-Class": "STANDARD", + "X-Goog-Hash": "crc32c=4/c+LQ==,md5=CS9tHYTt/+ntzj7B9nkkJQ==", + }, + content=b"", + ) + blob._extract_headers_from_download(response) + self.assertEqual(blob.content_type, "application/octet-stream") + self.assertEqual(blob.content_language, "en-US") + self.assertEqual(blob.md5_hash, "CS9tHYTt/+ntzj7B9nkkJQ==") + self.assertEqual(blob.crc32c, "4/c+LQ==") + def test_download_as_string_w_hash_response_header_none(self): blob_name = "blob-name" client = mock.Mock(spec=["_http"])