refactor(downloader): judge every 206, 416 and 200 ranged answer in one range_answer verdict

This commit is contained in:
imSp4rky
2026-12-04 14:46:56 -07:00
parent 89b3e77d18
commit bec5354293
6 changed files with 526 additions and 242 deletions
+1 -46
View File
@@ -6,7 +6,7 @@ import pytest
import requests as rq
from unshackle.core.constants import DOWNLOAD_CANCELLED
from unshackle.core.downloaders.requests import download, has_range_header, parse_content_range, request_range
from unshackle.core.downloaders.requests import download
from unshackle.core.downloaders.requests import requests as requests_downloader
# the package re-exports the `requests` function, shadowing the module of the same name
@@ -299,48 +299,3 @@ def test_batch_larger_than_the_queue_window_downloads_every_segment(tmp_path, se
assert not consumer.is_alive(), "downloader never finished the batch"
assert advanced == len(urls)
assert sorted(p.name for p in tmp_path.iterdir() if p.suffix == ".mp4") == [f"{i:02}.mp4" for i in range(6)]
@pytest.mark.parametrize(
("item", "expected"),
[
({}, False),
({"headers": None}, False),
({"headers": {}}, False),
({"headers": {"Accept": "*/*"}}, False),
({"headers": {"Range": "bytes=0-127"}}, True),
({"headers": {"range": "bytes=0-127"}}, True),
],
)
def test_has_range_header(item, expected):
assert has_range_header(item) is expected
@pytest.mark.parametrize(
("value", "expected"),
[
(None, None),
("", None),
("garbage", None),
("bytes */400000", None),
("bytes 0-399999/400000", (0, 399999, 400000)),
("BYTES 150000-399999/*", (150000, 399999, None)),
(b"bytes 5-9/10", (5, 9, 10)),
],
)
def test_parse_content_range(value, expected):
assert parse_content_range(value) == expected
@pytest.mark.parametrize(
("headers", "expected"),
[
({}, None),
({"Accept": "*/*"}, None),
({"Range": "bytes=-500"}, None),
({"Range": "bytes=100-"}, (100, None)),
({"range": "bytes=100-227"}, (100, 227)),
],
)
def test_request_range(headers, expected):
assert request_range(headers) == expected