Compare commits

..

2 Commits

Author SHA1 Message Date
coletdjnz
e418906bfd
Simplify macos target os version 2024-01-01 22:41:15 +13:00
coletdjnz
ed017a30a4
Test supported impersonate targets 2024-01-01 20:38:15 +13:00
2 changed files with 20 additions and 7 deletions

View File

@ -52,6 +52,7 @@ from yt_dlp.networking.exceptions import (
) )
from yt_dlp.networking.impersonate import ImpersonateRequestHandler, ImpersonateTarget from yt_dlp.networking.impersonate import ImpersonateRequestHandler, ImpersonateTarget
from yt_dlp.utils._utils import _YDLLogger as FakeLogger from yt_dlp.utils._utils import _YDLLogger as FakeLogger
from yt_dlp.utils import YoutubeDLError
from yt_dlp.utils.networking import ( from yt_dlp.utils.networking import (
HTTPHeaderDict, HTTPHeaderDict,
std_headers, std_headers,
@ -762,6 +763,18 @@ class TestClientCertificate:
}) })
@pytest.mark.parametrize('handler', ['CurlCFFI'], indirect=True)
class TestHTTPImpersonateRequestHandler(TestRequestHandlerBase):
def test_supported_impersonate_targets(self, handler):
with handler(headers=std_headers) as rh:
# note: this assumes the impersonate request handler supports the impersonate extension
for target in rh.supported_targets:
res = validate_and_send(rh, Request(
f'http://127.0.0.1:{self.http_port}/headers', extensions={'impersonate': target}))
assert res.status == 200
assert std_headers['user-agent'].lower() not in res.read().decode().lower()
class TestUrllibRequestHandler(TestRequestHandlerBase): class TestUrllibRequestHandler(TestRequestHandlerBase):
@pytest.mark.parametrize('handler', ['Urllib'], indirect=True) @pytest.mark.parametrize('handler', ['Urllib'], indirect=True)
def test_file_urls(self, handler): def test_file_urls(self, handler):
@ -1444,7 +1457,7 @@ class TestYoutubeDLNetworking:
with FakeImpersonationRHYDL() as ydl: with FakeImpersonationRHYDL() as ydl:
with pytest.raises( with pytest.raises(
RequestError, RequestError,
match=r'Impersonate target "test" is not available. This request requires browser impersonation' match=r'Impersonate target "test" is not available'
): ):
ydl.urlopen(Request('http://', extensions={'impersonate': ImpersonateTarget('test', None, None, None)})) ydl.urlopen(Request('http://', extensions={'impersonate': ImpersonateTarget('test', None, None, None)}))
@ -1465,14 +1478,14 @@ class TestYoutubeDLNetworking:
with FakeHTTPRHYDL() as ydl: with FakeHTTPRHYDL() as ydl:
with pytest.raises( with pytest.raises(
RequestError, RequestError,
match=r'Impersonate target "test" is not available. This request requires browser impersonation' match=r'Impersonate target "test" is not available'
): ):
ydl.urlopen(Request('http://', extensions={'impersonate': ImpersonateTarget('test', None, None, None)})) ydl.urlopen(Request('http://', extensions={'impersonate': ImpersonateTarget('test', None, None, None)}))
def test_raise_impersonate_error(self): def test_raise_impersonate_error(self):
with pytest.raises( with pytest.raises(
ValueError, YoutubeDLError,
match=r'Impersonate target "test" is not available. Use --list-impersonate-targets to see available targets.' match=r'Impersonate target "test" is not available'
): ):
FakeYDL({'impersonate': ImpersonateTarget('test', None, None, None)}) FakeYDL({'impersonate': ImpersonateTarget('test', None, None, None)})

View File

@ -112,11 +112,11 @@ class CurlCFFIRH(ImpersonateRequestHandler, InstanceStoreMixin):
ImpersonateTarget('chrome', '104', 'windows', '10'): curl_cffi.requests.BrowserType.chrome104, ImpersonateTarget('chrome', '104', 'windows', '10'): curl_cffi.requests.BrowserType.chrome104,
ImpersonateTarget('chrome', '101', 'windows', '10'): curl_cffi.requests.BrowserType.chrome101, ImpersonateTarget('chrome', '101', 'windows', '10'): curl_cffi.requests.BrowserType.chrome101,
ImpersonateTarget('chrome', '99', 'windows', '10'): curl_cffi.requests.BrowserType.chrome99, ImpersonateTarget('chrome', '99', 'windows', '10'): curl_cffi.requests.BrowserType.chrome99,
ImpersonateTarget('chrome', '99', 'android', '12'): curl_cffi.requests.BrowserType.chrome99_android,
ImpersonateTarget('edge', '101', 'windows', '10'): curl_cffi.requests.BrowserType.edge101, ImpersonateTarget('edge', '101', 'windows', '10'): curl_cffi.requests.BrowserType.edge101,
ImpersonateTarget('edge', '99', 'windows', '10'): curl_cffi.requests.BrowserType.edge99, ImpersonateTarget('edge', '99', 'windows', '10'): curl_cffi.requests.BrowserType.edge99,
ImpersonateTarget('safari', '15.5', 'macos', '12.4'): curl_cffi.requests.BrowserType.safari15_5, ImpersonateTarget('safari', '15.5', 'macos', '12'): curl_cffi.requests.BrowserType.safari15_5,
ImpersonateTarget('safari', '15.3', 'macos', '11.6.4'): curl_cffi.requests.BrowserType.safari15_3, ImpersonateTarget('safari', '15.3', 'macos', '11'): curl_cffi.requests.BrowserType.safari15_3,
ImpersonateTarget('chrome', '99', 'android', '12'): curl_cffi.requests.BrowserType.chrome99_android,
} }
def _create_instance(self, cookiejar=None): def _create_instance(self, cookiejar=None):