From 4d3faad55fdb00f4111cda57c3c0f6995b74cbdb Mon Sep 17 00:00:00 2001 From: remmy Date: Wed, 2 Sep 2020 18:55:35 +0000 Subject: [PATCH] fix(blob.py): returns blob hashes (md5, crc32c) when 'X-Goog-Hash header' is not provided --- google/cloud/storage/blob.py | 4 ++-- tests/unit/test_blob.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/google/cloud/storage/blob.py b/google/cloud/storage/blob.py index 31aeb05ef..d61081c44 100644 --- a/google/cloud/storage/blob.py +++ b/google/cloud/storage/blob.py @@ -818,8 +818,8 @@ def _extract_headers_from_download(self, response): method, digest = match.groups() digests[method] = digest - self.crc32c = digests.get("crc32c", None) - self.md5_hash = digests.get("md5", None) + self.crc32c = digests.get("crc32c", self.crc32c) + self.md5_hash = digests.get("md5", self.md5_hash) def _do_download( self, diff --git a/tests/unit/test_blob.py b/tests/unit/test_blob.py index 9bf60d42d..c4fd7fb8b 100644 --- a/tests/unit/test_blob.py +++ b/tests/unit/test_blob.py @@ -1524,10 +1524,16 @@ def test_download_as_string_w_response_headers(self): def test_download_as_string_w_hash_response_header_none(self): blob_name = "blob-name" + md5_hash = "CS9tHYTtyFntzj7B9nkkJQ==" + crc32c = "4gcgLQ==" client = mock.Mock(spec=["_http"]) bucket = _Bucket(client) media_link = "http://example.com/media/" - properties = {"mediaLink": media_link} + properties = { + "mediaLink": media_link, + "md5Hash": md5_hash, + "crc32c": crc32c, + } blob = self._make_one(blob_name, bucket=bucket, properties=properties) response = self._mock_requests_response( @@ -1538,8 +1544,8 @@ def test_download_as_string_w_hash_response_header_none(self): ) blob._extract_headers_from_download(response) - self.assertIsNone(blob.md5_hash) - self.assertIsNone(blob.crc32c) + self.assertEqual(blob.md5_hash, md5_hash) + self.assertEqual(blob.crc32c, crc32c) def test_download_as_bytes_w_generation_match(self): GENERATION_NUMBER = 6