1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-07-09 23:08:32 +00:00

add testcase

This commit is contained in:
c-basalt 2025-01-01 13:33:16 -05:00
parent 02001159b8
commit e8eb983583
4 changed files with 61 additions and 41 deletions

View File

@ -55,19 +55,36 @@ class TestExternalJSI(unittest.TestCase):
maxDiff = 2000
def setUp(self):
print()
self.ydl = FakeYDL()
self.url = ''
self.url_param = ''
if not self._JSI_CLASS.exe_version:
print(f'{self._JSI_CLASS.__name__} is not installed, skipping')
self.skipTest('Not available')
@property
def jsi(self):
return self._JSI_CLASS(self.ydl, self.url, 10, {})
return self._JSI_CLASS(self.ydl, self.url_param, 10, {})
def test_execute(self):
self.assertEqual(self.jsi.execute('console.log("Hello, world!");'), 'Hello, world!')
def test_user_agent(self):
ua = self.ydl.params['http_headers']['User-Agent']
self.assertEqual(self.jsi.execute('console.log(navigator.userAgent);'), ua)
self.assertNotEqual(self.jsi.execute('console.log(JSON.stringify(navigator.webdriver));'), 'true')
jsi = self._JSI_CLASS(self.ydl, self.url_param, 10, {}, user_agent='test/ua')
self.assertEqual(jsi.execute('console.log(navigator.userAgent);'), 'test/ua')
def test_location(self):
if 'location' not in self._JSI_CLASS._SUPPORTED_FEATURES:
print(f'{self._JSI_CLASS.__name__} does not support location, skipping')
self.skipTest('Location not supported')
self.url_param = 'https://example.com/123/456'
self.assertEqual(self.jsi.execute('console.log(JSON.stringify([location.href, location.hostname]));'),
'["https://example.com/123/456","example.com"]')
def test_execute_dom_parse(self):
if 'dom' not in self.jsi._SUPPORTED_FEATURES:
print(f'{self._JSI_CLASS.__name__} does not support DOM, skipping')
@ -81,29 +98,32 @@ def test_execute_dom_script(self):
if 'dom' not in self.jsi._SUPPORTED_FEATURES:
print(f'{self._JSI_CLASS.__name__} does not support DOM, skipping')
self.skipTest('DOM not supported')
self.assertEqual(self.jsi.execute(
'console.log(document.getElementById("test-div").innerHTML);',
html='''<html><body>
html='''<html><head><title>Hello, world!</title><body>
<div id="test-div"></div>
<script>
document.getElementById("test-div").innerHTML = "Hello, world!"
<script src="https://example.com/script.js"></script>
<script type="text/javascript">
document.getElementById("test-div").innerHTML = document.title;
console.log('this should not show up');
a = b; // Errors should be ignored
</script>
</body></html>'''),
'Hello, world!')
def test_dom_location(self):
if not self._JSI_CLASS._SUPPORTED_FEATURES.issuperset({'dom', 'location'}):
print(f'{self._JSI_CLASS.__name__} does not support both DOM and location, skipping')
self.skipTest('DOM or location not supported')
self.url_param = 'https://example.com/123/456'
self.assertEqual(self.jsi.execute(
'console.log(document.getElementById("test-div").innerHTML);',
html='''<html><body>
<div id="test-div"></div>
<script src="https://example.com/script.js"></script>
<script type="text/javascript">
document.getElementById("test-div").innerHTML = "Hello, world!"
console.log('this should not show up');
a = b; // Undefined variable assignment
</script>
</body></html>'''),
'Hello, world!')
html='''<html><head><script>
document.querySelector("#test-div").innerHTML = document.domain</script></head>
<body><div id="test-div">Hello, world!</div></body></html>'''),
'example.com')
def test_execute_cookiejar(self):
if 'cookies' not in self.jsi._SUPPORTED_FEATURES:
@ -134,7 +154,7 @@ def _assert_expected_execute(cookie_str, ref_cookie_str):
ref_cookiejar.set_cookie(test_cookie.to_cookie())
# test identity without modification from js
self.url = 'http://example.com/123/456'
self.url_param = 'http://example.com/123/456'
_assert_expected_execute(self.jsi.execute(
'console.log(document.cookie);', cookiejar=cookiejar),
'test1=test1; test3=test3')
@ -144,7 +164,7 @@ def _assert_expected_execute(cookie_str, ref_cookie_str):
new_cookie_2 = NetscapeFields('test2', 'new2', '.example.com', '/', True, int(time.time()) + 900)
ref_cookiejar.set_cookie(new_cookie_1.to_cookie())
ref_cookiejar.set_cookie(new_cookie_2.to_cookie())
self.url = 'https://example.com/123/456'
self.url_param = 'https://example.com/123/456'
_assert_expected_execute(self.jsi.execute(
f'''document.cookie = "test1=new1; secure; expires={new_cookie_1.expire_str()}; domain=.example.com; path=/";
console.log(document.cookie);''',

View File

@ -142,7 +142,6 @@ def execute(self, jscode, video_id=None, note='Executing JS in Deno with jsdom',
callback_varname = f'__callback_{random_string()}'
script = f'''{self._init_script};
{self._override_navigator_js};
import jsdom from "{self._JSDOM_URL}";
let {callback_varname} = (() => {{
const jar = jsdom.CookieJar.deserializeSync({json.dumps(self.serialize_cookie(cookiejar, self._url))});
@ -151,9 +150,12 @@ def execute(self, jscode, video_id=None, note='Executing JS in Deno with jsdom',
cookieJar: jar,
pretendToBeVisual: true,
}});
Object.keys(dom.window).filter(key => !['atob', 'btoa'].includes(key)).forEach((key) => {{
try {{window[key] = dom.window[key]}} catch (e) {{}}
Object.keys(dom.window).filter(key => !['atob', 'btoa', 'crypto', 'location'].includes(key))
.filter(key => !(window.location? [] : ['sessionStorage', 'localStorage']).includes(key))
.forEach((key) => {{
try {{window[key] = dom.window[key]}} catch (e) {{ console.error(e) }}
}});
{self._override_navigator_js};
window.screen = {{
availWidth: 1920,
@ -168,8 +170,8 @@ def execute(self, jscode, video_id=None, note='Executing JS in Deno with jsdom',
width: 1920,
}}
Object.defineProperty(document.body, 'clientWidth', {{value: 1903}});
Object.defineProperty(document.body, 'clientHeight', {{value: 1035}});
document.domain = location.hostname;
Object.defineProperty(document.body, 'clientHeight', {{value: 2000}});
document.domain = location?.hostname;
delete window.jsdom;
const origLog = console.log;

View File

@ -164,26 +164,24 @@ def _execute_html(self, jscode: str, url: str, html: str, cookiejar, video_id=No
return new_html, stdout
def execute(self, jscode, video_id=None, note='Executing JS in PhantomJS', html='', cookiejar=None):
if self._url or html or cookiejar:
jscode = '''console.log(page.evaluate(function() {
var %(std_var)s = [];
console.log = function() {
var values = '';
for (var i = 0; i < arguments.length; i++) {
values += arguments[i] + ' ';
}
%(std_var)s.push(values);
jscode = '''console.log(page.evaluate(function() {
var %(std_var)s = [];
console.log = function() {
var values = '';
for (var i = 0; i < arguments.length; i++) {
values += arguments[i] + ' ';
}
%(jscode)s;
return %(std_var)s.join('\\n');
}));
saveAndExit();''' % {
'std_var': f'__stdout__values_{random_string()}',
'jscode': jscode,
%(std_var)s.push(values);
}
return self._execute_html(jscode, self._url, html, cookiejar, video_id=video_id, note=note)[1].strip()
return self._execute(jscode, video_id, note=note).strip()
%(jscode)s;
return %(std_var)s.join('\\n');
}));
saveAndExit();''' % {
'std_var': f'__stdout__values_{random_string()}',
'jscode': jscode,
}
return self._execute_html(jscode, self._url, html, cookiejar, video_id=video_id, note=note)[1].strip()
class PhantomJSwrapper:

View File

@ -117,7 +117,7 @@ def __init__(
self.write_debug(f'Allowed JSI keys: {jsi_keys}')
handler_classes = [_JSI_HANDLERS[key] for key in jsi_keys
if _JSI_HANDLERS[key]._SUPPORTED_FEATURES.issuperset(self._features)]
self.write_debug(f'Selected JSI classes for given features: {get_jsi_keys(handler_classes)}, '
self.write_debug(f'Select JSI for features={self._features}: {get_jsi_keys(handler_classes)}, '
f'included: {get_jsi_keys(only_include) or "all"}, excluded: {get_jsi_keys(exclude)}')
self._handler_dict = {