| 1 |
import re |
| 2 |
|
| 3 |
import pytest |
| 4 |
import requests |
| 5 |
import responses |
| 6 |
from requests import HTTPError |
| 7 |
from requests.models import PreparedRequest |
| 8 |
from responses import matchers |
| 9 |
|
| 10 |
from conda_package_streaming.lazy_wheel import LazyZipOverHTTP |
| 11 |
|
| 12 |
HTTP_FULL_RANGE_PATTERN = re.compile(r"bytes=(\d+)-(\d+)$") |
| 13 |
HTTP_END_RANGE_PATTERN = re.compile(r"bytes=-(\d+)$") |
| 14 |
|
| 15 |
|
| 16 |
class TestLazyZipOverHTTP: |
| 17 |
@staticmethod |
| 18 |
def generate_zero_bytes(length_bytes: int) -> bytes: |
| 19 |
return bytes(length_bytes) |
| 20 |
|
| 21 |
@staticmethod |
| 22 |
def successful_http_stream_callback_wrapper(file: bytes): |
| 23 |
# https://datatracker.ietf.org/doc/html/rfc7233 |
| 24 |
def _callback(request: PreparedRequest): |
| 25 |
full_pattern_match = HTTP_FULL_RANGE_PATTERN.match(request.headers["Range"]) |
| 26 |
end_pattern_match = HTTP_END_RANGE_PATTERN.match(request.headers["Range"]) |
| 27 |
|
| 28 |
if full_pattern_match: |
| 29 |
start_range = int(full_pattern_match.group(1)) |
| 30 |
end_range = int(full_pattern_match.group(2)) |
| 31 |
|
| 32 |
assert start_range >= 0 |
| 33 |
assert end_range >= 0 |
| 34 |
assert start_range < end_range |
| 35 |
|
| 36 |
if start_range >= len(file): |
| 37 |
# Range Not Satisfiable |
| 38 |
return 416, {}, b"" |
| 39 |
|
| 40 |
# truncate to file length |
| 41 |
end_range = min(end_range, len(file) - 1) |
| 42 |
content_length = end_range - start_range + 1 # the bounds are inclusive |
| 43 |
else: |
| 44 |
assert end_pattern_match |
| 45 |
|
| 46 |
content_length_requested = int(end_pattern_match.group(1)) |
| 47 |
assert content_length_requested >= 0 |
| 48 |
content_length = min(content_length_requested, len(file)) |
| 49 |
|
| 50 |
start_range = len(file) - content_length |
| 51 |
end_range = len(file) - 1 |
| 52 |
|
| 53 |
headers = { |
| 54 |
"Content-Length": str(content_length), |
| 55 |
"Content-Range": f"bytes {start_range}-{end_range}/{len(file)}", |
| 56 |
} |
| 57 |
|
| 58 |
# this is not inlined because black and flake8 disagree on how to format it |
| 59 |
end_file_range = end_range + 1 |
| 60 |
|
| 61 |
return 206, headers, file[start_range:end_file_range] |
| 62 |
|
| 63 |
return _callback |
| 64 |
|
| 65 |
@pytest.mark.parametrize("fall_back_to_full_download", [True, False]) |
| 66 |
@responses.activate |
| 67 |
def test_init_stream_successful(self, fall_back_to_full_download: bool): |
| 68 |
responses.add_callback( |
| 69 |
responses.GET, |
| 70 |
"https://example.com/test.zip", |
| 71 |
callback=self.successful_http_stream_callback_wrapper( |
| 72 |
self.generate_zero_bytes(10000) |
| 73 |
), |
| 74 |
content_type="application/zip", |
| 75 |
) |
| 76 |
|
| 77 |
session = requests.Session() |
| 78 |
lazy_zip = LazyZipOverHTTP( |
| 79 |
"https://example.com/test.zip", |
| 80 |
session, |
| 81 |
fall_back_to_full_download=fall_back_to_full_download, |
| 82 |
) |
| 83 |
lazy_zip.read() |
| 84 |
|
| 85 |
@pytest.mark.parametrize("fall_back_to_full_download", [True, False]) |
| 86 |
@responses.activate |
| 87 |
def test_init_stream_retry_without_range_with_fallback( |
| 88 |
self, fall_back_to_full_download: bool |
| 89 |
): |
| 90 |
""" |
| 91 |
Some package servers respond with 416 (Range Not Satisfiable) when the |
| 92 |
file is smaller than the range requested. This violates RFC 7233, but we |
| 93 |
cope with it by retrying without Range, requesting the full file if |
| 94 |
fall_back_to_full_download is set. |
| 95 |
""" |
| 96 |
responses.add( |
| 97 |
responses.GET, |
| 98 |
url="https://example.com/test.zip", |
| 99 |
status=416, |
| 100 |
match=[matchers.header_matcher({"Range": re.compile(r".*")})], |
| 101 |
) |
| 102 |
|
| 103 |
responses.add( |
| 104 |
responses.GET, |
| 105 |
url="https://example.com/test.zip", |
| 106 |
body=self.generate_zero_bytes(10000), |
| 107 |
content_type="application/zip", |
| 108 |
) |
| 109 |
|
| 110 |
session = requests.Session() |
| 111 |
|
| 112 |
if fall_back_to_full_download: |
| 113 |
# this should work |
| 114 |
lazy_zip = LazyZipOverHTTP( |
| 115 |
"https://example.com/test.zip", session, fall_back_to_full_download=True |
| 116 |
) |
| 117 |
lazy_zip.read() |
| 118 |
return |
| 119 |
|
| 120 |
# otherwise, the constructor should raise an exception |
| 121 |
with pytest.raises( |
| 122 |
HTTPError, |
| 123 |
match="Set the fall_back_to_full_download flag to work around this issue.", |
| 124 |
): |
| 125 |
LazyZipOverHTTP( |
| 126 |
"https://example.com/test.zip", |
| 127 |
session, |
| 128 |
fall_back_to_full_download=False, |
| 129 |
) |