mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
129 lines
4.0 KiB
Python
129 lines
4.0 KiB
Python
"""Each session kind's transport switches session retries and the request record off, and reads the body its own way."""
|
|
|
|
import importlib
|
|
import io
|
|
|
|
import pytest
|
|
import requests as rq
|
|
|
|
from unshackle.core.session import RnetSession
|
|
|
|
dl = importlib.import_module("unshackle.core.downloaders.requests")
|
|
|
|
|
|
class _Response:
|
|
def __init__(self, headers=None, body=b"abcdef"):
|
|
self.status_code = 200
|
|
self.headers = rq.structures.CaseInsensitiveDict(headers or {})
|
|
self.raw = _Raw(body)
|
|
self.body = body
|
|
self.content_length = len(body)
|
|
|
|
def raise_for_status(self):
|
|
pass
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
def iter_content(self, chunk_size):
|
|
yield self.body
|
|
|
|
|
|
class _Raw(io.BytesIO):
|
|
def __init__(self, body):
|
|
super().__init__(body)
|
|
self.calls: list[str] = []
|
|
|
|
def read(self, size=-1):
|
|
self.calls.append("read")
|
|
return super().read(size)
|
|
|
|
def read1(self, size=-1):
|
|
self.calls.append("read1")
|
|
return super().read1(size)
|
|
|
|
|
|
class _Session:
|
|
def __init__(self, response=None):
|
|
self.calls: list[dict] = []
|
|
self.response = response or _Response()
|
|
|
|
def get(self, url, **kwargs):
|
|
self.calls.append(kwargs)
|
|
return self.response
|
|
|
|
|
|
def test_each_session_kind_gets_its_transport():
|
|
assert isinstance(dl.transport_for(RnetSession()), dl.RnetTransport)
|
|
assert isinstance(dl.transport_for(rq.Session()), dl.RequestsTransport)
|
|
transport = dl.RnetTransport(_Session())
|
|
assert dl.transport_for(transport) is transport
|
|
|
|
|
|
def test_requests_transport_has_no_retries_and_no_record():
|
|
session = rq.Session()
|
|
view = dl.transport_for(session).session
|
|
assert view is not session
|
|
assert view.record_request is False
|
|
assert all(adapter.max_retries.total == 0 for adapter in view.adapters.values())
|
|
|
|
|
|
class _RnetSession(RnetSession):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.calls: list[dict] = []
|
|
|
|
def get(self, url, **kwargs):
|
|
self.calls.append(kwargs)
|
|
return _Response()
|
|
|
|
|
|
def test_rnet_transport_turns_retries_and_record_off():
|
|
session = _RnetSession()
|
|
dl.transport_for(session).get("http://127.0.0.1:1/a")
|
|
assert session.calls[0]["max_retries"] == 0
|
|
assert session.calls[0]["record_request"] is False
|
|
assert session.calls[0]["stream"] is True
|
|
|
|
|
|
def test_requests_body_reads_raw_and_read1_only_while_racing():
|
|
response = _Response({"Content-Length": "6"})
|
|
body = dl.RequestsBody(response, raw=True)
|
|
racing = iter([True, False, False])
|
|
assert b"".join(body.chunks(2, lambda: next(racing))) == b"abcdef"
|
|
assert response.raw.calls == ["read1", "read", "read"]
|
|
|
|
|
|
class _Transport(dl.Transport):
|
|
def __init__(self):
|
|
self.response = _Response({"Content-Length": "6"})
|
|
|
|
def get(self, url, **kwargs):
|
|
return dl.RequestsBody(self.response, raw=True)
|
|
|
|
|
|
@pytest.mark.parametrize("claimed, expected", [(None, {"read"}), (lambda: False, {"read1"})])
|
|
def test_download_reads_read1_only_for_a_claimable_segment(tmp_path, claimed, expected):
|
|
transport = _Transport()
|
|
events = dl.download("http://127.0.0.1:1/a", tmp_path / "a.bin", transport, claimed=claimed, racing=lambda: True)
|
|
list(events)
|
|
assert (tmp_path / "a.bin").read_bytes() == b"abcdef"
|
|
assert set(transport.response.raw.calls) == expected
|
|
|
|
|
|
def test_an_encoded_body_goes_through_iter_content():
|
|
response = _Response({"content-encoding": "gzip", "Content-Length": "6"})
|
|
body = dl.RequestsBody(response, raw=True)
|
|
assert body.content_encoded
|
|
assert body.content_length == 0
|
|
assert list(body.chunks(2, None)) == [b"abcdef"]
|
|
assert response.raw.calls == []
|
|
|
|
|
|
def test_another_session_kind_is_read_with_iter_content_and_a_timeout():
|
|
session = _Session()
|
|
transport = dl.transport_for(session)
|
|
assert transport.session is session
|
|
assert list(transport.get("http://127.0.0.1:1/a").chunks(2, None)) == [b"abcdef"]
|
|
assert session.calls[0]["timeout"] == (dl.CONNECT_TIMEOUT, dl.READ_TIMEOUT)
|