mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-06 07:55:10 +00:00
69 lines
1.9 KiB
Python
69 lines
1.9 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 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_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 == []
|