| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 609b3e0 commit 7e0412a
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,6 +43,12 @@ | |||
| 43 | 43 | Please restart the download. | |
| 44 | 44 | """ | |
| 45 | 45 | ||
| 46 | + _RESPONSE_HEADERS_INFO = """\ | ||
| 47 | + The X-Goog-Stored-Content-Length is {}. The X-Goog-Stored-Content-Encoding is {}. | ||
| 48 | + The download request read {} bytes of data. | ||
| 49 | + If the download was incomplete, please check the network connection and restart the download. | ||
| 50 | + """ | ||
| 51 | + | ||
| 46 | 52 | ||
| 47 | 53 | class Download(_request_helpers.RequestsMixin, _download.Download): | |
| 48 | 54 | """Helper to manage downloading a resource from a Google API. | |
@@ -141,13 +147,30 @@ def _write_to_stream(self, response): | |||
| 141 | 147 | ): | |
| 142 | 148 | actual_checksum = _helpers.prepare_checksum_digest(checksum_object.digest()) | |
| 143 | 149 | if actual_checksum != expected_checksum: | |
| 144 | - msg = _CHECKSUM_MISMATCH.format( | ||
| 145 | - self.media_url, | ||
| 146 | - expected_checksum, | ||
| 147 | - actual_checksum, | ||
| 148 | - checksum_type=self.checksum.upper(), | ||
| 150 | + headers = self._get_headers(response) | ||
| 151 | + x_goog_encoding = headers.get("x-goog-stored-content-encoding") | ||
| 152 | + x_goog_length = headers.get("x-goog-stored-content-length") | ||
| 153 | + content_length_msg = _RESPONSE_HEADERS_INFO.format( | ||
| 154 | + x_goog_length, x_goog_encoding, self._bytes_downloaded | ||
| 149 | 155 | ) | |
| 150 | - raise DataCorruption(response, msg) | ||
| 156 | + if ( | ||
| 157 | + x_goog_length | ||
| 158 | + and self._bytes_downloaded < int(x_goog_length) | ||
| 159 | + and x_goog_encoding != "gzip" | ||
| 160 | + ): | ||
| 161 | + # The library will attempt to trigger a retry by raising a ConnectionError, if | ||
| 162 | + # (a) bytes_downloaded is less than response header x-goog-stored-content-length, and | ||
| 163 | + # (b) the object is not gzip-compressed when stored in Cloud Storage. | ||
| 164 | + raise ConnectionError(content_length_msg) | ||
| 165 | + else: | ||
| 166 | + msg = _CHECKSUM_MISMATCH.format( | ||
| 167 | + self.media_url, | ||
| 168 | + expected_checksum, | ||
| 169 | + actual_checksum, | ||
| 170 | + checksum_type=self.checksum.upper(), | ||
| 171 | + ) | ||
| 172 | + msg += content_length_msg | ||
| 173 | + raise DataCorruption(response, msg) | ||
| 151 | 174 | ||
| 152 | 175 | def consume( | |
| 153 | 176 | self, | |
@@ -339,13 +362,31 @@ def _write_to_stream(self, response): | |||
| 339 | 362 | actual_checksum = _helpers.prepare_checksum_digest(checksum_object.digest()) | |
| 340 | 363 | ||
| 341 | 364 | if actual_checksum != expected_checksum: | |
| 342 | - msg = _CHECKSUM_MISMATCH.format( | ||
| 343 | - self.media_url, | ||
| 344 | - expected_checksum, | ||
| 345 | - actual_checksum, | ||
| 346 | - checksum_type=self.checksum.upper(), | ||
| 365 | + headers = self._get_headers(response) | ||
| 366 | + x_goog_encoding = headers.get("x-goog-stored-content-encoding") | ||
| 367 | + x_goog_length = headers.get("x-goog-stored-content-length") | ||
| 368 | + content_length_msg = _RESPONSE_HEADERS_INFO.format( | ||
| 369 | + x_goog_length, x_goog_encoding, self._bytes_downloaded | ||
| 347 | 370 | ) | |
| 348 | - raise DataCorruption(response, msg) | ||
| 371 | + if ( | ||
| 372 | + x_goog_length | ||
| 373 | + and self._bytes_downloaded < int(x_goog_length) | ||
| 374 | + and x_goog_encoding != "gzip" | ||
| 375 | + ): | ||
| 376 | + # The library will attempt to trigger a retry by raising a ConnectionError, if | ||
| 377 | + # (a) bytes_downloaded is less than response header x-goog-stored-content-length, and | ||
| 378 | + # (b) the object is not gzip-compressed when stored in Cloud Storage. | ||
| 379 | + raise ConnectionError(content_length_msg) | ||
| 380 | + else: | ||
| 381 | + msg = _CHECKSUM_MISMATCH.format( | ||
| 382 | + self.media_url, | ||
| 383 | + expected_checksum, | ||
| 384 | + actual_checksum, | ||
| 385 | + checksum_type=self.checksum.upper(), | ||
| 386 | + ) | ||
| 387 | + msg += content_length_msg | ||
| 388 | + raise DataCorruption(response, msg) | ||
| 389 | + | ||
| 349 | 390 | ||
| 350 | 391 | def consume( | |
| 351 | 392 | self, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -463,7 +463,7 @@ def test_corrupt_download(self, add_files, corrupting_transport, checksum): | |||
| 463 | 463 | info[checksum], | |
| 464 | 464 | checksum_type=checksum.upper(), | |
| 465 | 465 | ) | |
| 466 | - assert exc_info.value.args == (msg,) | ||
| 466 | + assert msg in exc_info.value.args[0] | ||
| 467 | 467 | ||
| 468 | 468 | def test_corrupt_download_no_check(self, add_files, corrupting_transport): | |
| 469 | 469 | for info in ALL_FILES: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -124,7 +124,11 @@ def test__write_to_stream_with_hash_check_fail(self, checksum): | |||
| 124 | 124 | msg = download_mod._CHECKSUM_MISMATCH.format( | |
| 125 | 125 | EXAMPLE_URL, bad_checksum, good_checksum, checksum_type=checksum.upper() | |
| 126 | 126 | ) | |
| 127 | - assert error.args[0] == msg | ||
| 127 | + assert msg in error.args[0] | ||
| 128 | + assert ( | ||
| 129 | + f"The download request read {download._bytes_downloaded} bytes of data." | ||
| 130 | + in error.args[0] | ||
| 131 | + ) | ||
| 128 | 132 | ||
| 129 | 133 | # Check mocks. | |
| 130 | 134 | response.__enter__.assert_called_once_with() | |
@@ -186,6 +190,29 @@ def test__write_to_stream_with_invalid_checksum_type(self): | |||
| 186 | 190 | error = exc_info.value | |
| 187 | 191 | assert error.args[0] == "checksum must be ``'md5'``, ``'crc32c'`` or ``None``" | |
| 188 | 192 | ||
| 193 | + @pytest.mark.parametrize("checksum", ["md5", "crc32c"]) | ||
| 194 | + def test__write_to_stream_incomplete_read(self, checksum): | ||
| 195 | + stream = io.BytesIO() | ||
| 196 | + download = download_mod.Download(EXAMPLE_URL, stream=stream, checksum=checksum) | ||
| 197 | + | ||
| 198 | + chunk1 = b"first chunk" | ||
| 199 | + mock_full_content_length = len(chunk1) + 123 | ||
| 200 | + headers = {"x-goog-stored-content-length": mock_full_content_length} | ||
| 201 | + bad_checksum = "d3JvbmcgbiBtYWRlIHVwIQ==" | ||
| 202 | + header_value = "crc32c={bad},md5={bad}".format(bad=bad_checksum) | ||
| 203 | + headers[_helpers._HASH_HEADER] = header_value | ||
| 204 | + response = _mock_response(chunks=[chunk1], headers=headers) | ||
| 205 | + | ||
| 206 | + with pytest.raises(ConnectionError) as exc_info: | ||
| 207 | + download._write_to_stream(response) | ||
| 208 | + | ||
| 209 | + assert not download.finished | ||
| 210 | + error = exc_info.value | ||
| 211 | + assert ( | ||
| 212 | + f"The download request read {download._bytes_downloaded} bytes of data." | ||
| 213 | + in error.args[0] | ||
| 214 | + ) | ||
| 215 | + | ||
| 189 | 216 | def _consume_helper( | |
| 190 | 217 | self, | |
| 191 | 218 | stream=None, | |
@@ -304,7 +331,11 @@ def test_consume_with_stream_hash_check_fail(self, checksum): | |||
| 304 | 331 | msg = download_mod._CHECKSUM_MISMATCH.format( | |
| 305 | 332 | EXAMPLE_URL, bad_checksum, good_checksum, checksum_type=checksum.upper() | |
| 306 | 333 | ) | |
| 307 | - assert error.args[0] == msg | ||
| 334 | + assert msg in error.args[0] | ||
| 335 | + assert ( | ||
| 336 | + f"The download request read {download._bytes_downloaded} bytes of data." | ||
| 337 | + in error.args[0] | ||
| 338 | + ) | ||
| 308 | 339 | ||
| 309 | 340 | # Check mocks. | |
| 310 | 341 | transport.request.assert_called_once_with( | |
@@ -599,7 +630,11 @@ def test__write_to_stream_with_hash_check_fail(self, checksum): | |||
| 599 | 630 | msg = download_mod._CHECKSUM_MISMATCH.format( | |
| 600 | 631 | EXAMPLE_URL, bad_checksum, good_checksum, checksum_type=checksum.upper() | |
| 601 | 632 | ) | |
| 602 | - assert error.args[0] == msg | ||
| 633 | + assert msg in error.args[0] | ||
| 634 | + assert ( | ||
| 635 | + f"The download request read {download._bytes_downloaded} bytes of data." | ||
| 636 | + in error.args[0] | ||
| 637 | + ) | ||
| 603 | 638 | ||
| 604 | 639 | # Check mocks. | |
| 605 | 640 | response.__enter__.assert_called_once_with() | |
@@ -632,6 +667,31 @@ def test__write_to_stream_with_invalid_checksum_type(self): | |||
| 632 | 667 | error = exc_info.value | |
| 633 | 668 | assert error.args[0] == "checksum must be ``'md5'``, ``'crc32c'`` or ``None``" | |
| 634 | 669 | ||
| 670 | + @pytest.mark.parametrize("checksum", ["md5", "crc32c"]) | ||
| 671 | + def test__write_to_stream_incomplete_read(self, checksum): | ||
| 672 | + stream = io.BytesIO() | ||
| 673 | + download = download_mod.RawDownload( | ||
| 674 | + EXAMPLE_URL, stream=stream, checksum=checksum | ||
| 675 | + ) | ||
| 676 | + | ||
| 677 | + chunk1 = b"first chunk" | ||
| 678 | + mock_full_content_length = len(chunk1) + 123 | ||
| 679 | + headers = {"x-goog-stored-content-length": mock_full_content_length} | ||
| 680 | + bad_checksum = "d3JvbmcgbiBtYWRlIHVwIQ==" | ||
| 681 | + header_value = "crc32c={bad},md5={bad}".format(bad=bad_checksum) | ||
| 682 | + headers[_helpers._HASH_HEADER] = header_value | ||
| 683 | + response = _mock_raw_response(chunks=[chunk1], headers=headers) | ||
| 684 | + | ||
| 685 | + with pytest.raises(ConnectionError) as exc_info: | ||
| 686 | + download._write_to_stream(response) | ||
| 687 | + | ||
| 688 | + assert not download.finished | ||
| 689 | + error = exc_info.value | ||
| 690 | + assert ( | ||
| 691 | + f"The download request read {download._bytes_downloaded} bytes of data." | ||
| 692 | + in error.args[0] | ||
| 693 | + ) | ||
| 694 | + | ||
| 635 | 695 | def _consume_helper( | |
| 636 | 696 | self, | |
| 637 | 697 | stream=None, | |
@@ -754,7 +814,11 @@ def test_consume_with_stream_hash_check_fail(self, checksum): | |||
| 754 | 814 | msg = download_mod._CHECKSUM_MISMATCH.format( | |
| 755 | 815 | EXAMPLE_URL, bad_checksum, good_checksum, checksum_type=checksum.upper() | |
| 756 | 816 | ) | |
| 757 | - assert error.args[0] == msg | ||
| 817 | + assert msg in error.args[0] | ||
| 818 | + assert ( | ||
| 819 | + f"The download request read {download._bytes_downloaded} bytes of data." | ||
| 820 | + in error.args[0] | ||
| 821 | + ) | ||
| 758 | 822 | ||
| 759 | 823 | # Check mocks. | |
| 760 | 824 | transport.request.assert_called_once_with( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments