Skip to content

Commit

Permalink
error responses testing
Browse files Browse the repository at this point in the history
  • Loading branch information
coletdjnz committed Dec 2, 2023
1 parent f97ab81 commit 40fa635
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
71 changes: 70 additions & 1 deletion test/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from test.conftest import validate_and_send
from test.helper import FakeYDL, http_server_port
from yt_dlp.cookies import YoutubeDLCookieJar
from yt_dlp.dependencies import brotli, requests, urllib3
from yt_dlp.dependencies import brotli, requests, urllib3, curl_cffi
from yt_dlp.networking import (
HEADRequest,
PUTRequest,
Expand Down Expand Up @@ -946,6 +946,74 @@ def test_headers(self, handler):
assert std_headers['accept-language'].lower() in res
assert 'x-custom: test' in res

@pytest.mark.parametrize('raised,expected,match', [
(lambda: curl_cffi.requests.errors.RequestsError(
'', code=curl_cffi.const.CurlECode.PARTIAL_FILE), IncompleteRead, None),
(lambda: curl_cffi.requests.errors.RequestsError(
'', code=curl_cffi.const.CurlECode.OPERATION_TIMEDOUT), TransportError, None),
(lambda: curl_cffi.requests.errors.RequestsError(
'', code=curl_cffi.const.CurlECode.RECV_ERROR), TransportError, None),
])
@pytest.mark.parametrize('handler', ['CurlCFFI'], indirect=True)
def test_response_error_mapping(self, handler, monkeypatch, raised, expected, match):
import curl_cffi.requests
from yt_dlp.networking._curlcffi import CurlCFFIResponseAdapter
curl_res = curl_cffi.requests.Response()
res = CurlCFFIResponseAdapter(curl_res)

def mock_read(*args, **kwargs):
try:
raise raised()
except Exception as e:
e.response = curl_res
raise
monkeypatch.setattr(res.fp, 'read', mock_read)

with pytest.raises(expected, match=match) as exc_info:
res.read()

assert exc_info.type is expected

@pytest.mark.parametrize('raised,expected,match', [
(lambda: curl_cffi.requests.errors.RequestsError(
'', code=curl_cffi.const.CurlECode.OPERATION_TIMEDOUT), TransportError, None),
(lambda: curl_cffi.requests.errors.RequestsError(
'', code=curl_cffi.const.CurlECode.PEER_FAILED_VERIFICATION), CertificateVerifyError, None),
(lambda: curl_cffi.requests.errors.RequestsError(
'', code=curl_cffi.const.CurlECode.SSL_CONNECT_ERROR), SSLError, None),
(lambda: curl_cffi.requests.errors.RequestsError(
'', code=curl_cffi.const.CurlECode.TOO_MANY_REDIRECTS), HTTPError, None),
(lambda: curl_cffi.requests.errors.RequestsError(
'', code=curl_cffi.const.CurlECode.PROXY), ProxyError, None),
])
@pytest.mark.parametrize('handler', ['CurlCFFI'], indirect=True)
def test_request_error_mapping(self, handler, monkeypatch, raised, expected, match):
import curl_cffi.requests
curl_res = curl_cffi.requests.Response()
curl_res.status_code = 301

with handler() as rh:
original_get_instance = rh._get_instance

def mock_get_instance(*args, **kwargs):
instance = original_get_instance(*args, **kwargs)

def request(*_, **__):
try:
raise raised()
except Exception as e:
e.response = curl_res
raise
monkeypatch.setattr(instance, 'request', request)
return instance

monkeypatch.setattr(rh, '_get_instance', mock_get_instance)

with pytest.raises(expected) as exc_info:
rh.send(Request('http://fake'))

assert exc_info.type is expected


def run_validation(handler, error, req, **handler_kwargs):
with handler(**handler_kwargs) as rh:
Expand Down Expand Up @@ -1730,6 +1798,7 @@ def test_compat(self):
assert res.getheader('test') == res.get_header('test')


# TODO: move these to test_utils.py when that moves to pytest
class TestImpersonate:
@pytest.mark.parametrize('target,expected', [
('firefox', ('firefox', None, None, None)),
Expand Down
9 changes: 1 addition & 8 deletions yt_dlp/networking/_curlcffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def read(self, amt=None):
partial=self.fp.bytes_read,
expected=content_length - self.fp.bytes_read if content_length is not None else None,
cause=e) from e
raise
raise TransportError(cause=e) from e


@register_rh
Expand Down Expand Up @@ -198,13 +198,6 @@ def _send(self, request: Request):
max_redirects_exceeded = True
curl_response = e.response

elif e.code == CurlECode.PARTIAL_FILE:
partial = e.response.content
content_length = int_or_none(e.response.headers.get('Content-Length'))
raise IncompleteRead(
partial=len(partial),
expected=content_length - len(partial) if content_length is not None else None,
cause=e) from e
elif e.code == CurlECode.PROXY:
raise ProxyError(cause=e) from e
else:
Expand Down

0 comments on commit 40fa635

Please sign in to comment.