]>
Commit | Line | Data |
---|---|---|
e387eb5a | 1 | #!/usr/bin/env python |
9d4660ca | 2 | # coding: utf-8 |
e387eb5a | 3 | |
4e408e47 PH |
4 | from __future__ import unicode_literals |
5 | ||
44a5f171 PH |
6 | # Allow direct execution |
7 | import os | |
dae7c920 | 8 | import sys |
44fb3454 | 9 | import unittest |
44a5f171 | 10 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
44fb3454 | 11 | |
44a5f171 PH |
12 | |
13 | # Various small unit tests | |
62e609ab | 14 | import io |
fac55558 | 15 | import json |
44a5f171 | 16 | import xml.etree.ElementTree |
dae7c920 | 17 | |
a921f407 | 18 | from youtube_dl.utils import ( |
05900629 | 19 | age_restricted, |
cae97f65 | 20 | args_to_str, |
5eb6bdce | 21 | encode_base_n, |
e4bdb37e | 22 | clean_html, |
eb9c3edd | 23 | date_from_str, |
a921f407 | 24 | DateRange, |
cae97f65 | 25 | detect_exe_version, |
5035536e | 26 | determine_ext, |
cbecc9b9 | 27 | dict_get, |
6b77d52b | 28 | encode_compat_str, |
29eb5174 | 29 | encodeFilename, |
cae97f65 PH |
30 | escape_rfc3986, |
31 | escape_url, | |
8bb56eee | 32 | extract_attributes, |
5379a2d4 | 33 | ExtractorError, |
a921f407 | 34 | find_xpath_attr, |
5aafe895 | 35 | fix_xml_ampersands, |
84c237fb | 36 | get_element_by_class, |
9c44d242 | 37 | InAdvancePagedList, |
cae97f65 | 38 | intlist_to_bytes, |
61ca9a80 | 39 | is_html, |
cae97f65 PH |
40 | js_to_json, |
41 | limit_length, | |
5bc880b9 | 42 | ohdave_rsa_encrypt, |
cae97f65 PH |
43 | OnDemandPagedList, |
44 | orderedSet, | |
608d11f5 | 45 | parse_duration, |
cae97f65 | 46 | parse_filesize, |
fb47597b | 47 | parse_count, |
cae97f65 | 48 | parse_iso8601, |
62e609ab | 49 | read_batch_urls, |
29eb5174 | 50 | sanitize_filename, |
a2aaf4db | 51 | sanitize_path, |
a4bcaad7 | 52 | prepend_extension, |
b3ed15b7 | 53 | replace_extension, |
46bc9b7d S |
54 | remove_start, |
55 | remove_end, | |
31b2051e | 56 | remove_quotes, |
a6a173c2 | 57 | shell_quote, |
29eb5174 | 58 | smuggle_url, |
f53c966a | 59 | str_to_int, |
cae97f65 | 60 | strip_jsonp, |
29eb5174 PH |
61 | timeconvert, |
62 | unescapeHTML, | |
63 | unified_strdate, | |
46f59e89 | 64 | unified_timestamp, |
29eb5174 | 65 | unsmuggle_url, |
cae97f65 | 66 | uppercase_escape, |
0fe2ff78 | 67 | lowercase_escape, |
29eb5174 | 68 | url_basename, |
b74fa8cd | 69 | urlencode_postdata, |
1143535d | 70 | urshift, |
fb640d0a | 71 | update_url_query, |
5f9b8394 | 72 | version_tuple, |
cae97f65 | 73 | xpath_with_ns, |
87f70ab3 | 74 | xpath_element, |
5379a2d4 | 75 | xpath_text, |
87f70ab3 | 76 | xpath_attr, |
cfb56d1a | 77 | render_table, |
347de493 | 78 | match_str, |
bf6427d2 YCH |
79 | parse_dfxp_time_expr, |
80 | dfxp2srt, | |
f7126449 S |
81 | cli_option, |
82 | cli_valueless_option, | |
83 | cli_bool_option, | |
4f3c5e06 | 84 | parse_codecs, |
a921f407 | 85 | ) |
36e6f62c | 86 | from youtube_dl.compat import ( |
8bb56eee | 87 | compat_chr, |
36e6f62c | 88 | compat_etree_fromstring, |
fb640d0a | 89 | compat_urlparse, |
90 | compat_parse_qs, | |
36e6f62c | 91 | ) |
44fb3454 | 92 | |
627dcfff | 93 | |
44fb3454 | 94 | class TestUtil(unittest.TestCase): |
59ae15a5 PH |
95 | def test_timeconvert(self): |
96 | self.assertTrue(timeconvert('') is None) | |
97 | self.assertTrue(timeconvert('bougrg') is None) | |
98 | ||
99 | def test_sanitize_filename(self): | |
100 | self.assertEqual(sanitize_filename('abc'), 'abc') | |
101 | self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e') | |
102 | ||
103 | self.assertEqual(sanitize_filename('123'), '123') | |
104 | ||
105 | self.assertEqual('abc_de', sanitize_filename('abc/de')) | |
106 | self.assertFalse('/' in sanitize_filename('abc/de///')) | |
107 | ||
108 | self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de')) | |
109 | self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|')) | |
110 | self.assertEqual('yes no', sanitize_filename('yes? no')) | |
111 | self.assertEqual('this - that', sanitize_filename('this: that')) | |
112 | ||
113 | self.assertEqual(sanitize_filename('AT&T'), 'AT&T') | |
4e408e47 | 114 | aumlaut = 'ä' |
59ae15a5 | 115 | self.assertEqual(sanitize_filename(aumlaut), aumlaut) |
4e408e47 | 116 | tests = '\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430' |
59ae15a5 PH |
117 | self.assertEqual(sanitize_filename(tests), tests) |
118 | ||
2aeb06d6 PH |
119 | self.assertEqual( |
120 | sanitize_filename('New World record at 0:12:34'), | |
121 | 'New World record at 0_12_34') | |
a7440261 | 122 | |
5a42414b PH |
123 | self.assertEqual(sanitize_filename('--gasdgf'), '_-gasdgf') |
124 | self.assertEqual(sanitize_filename('--gasdgf', is_id=True), '--gasdgf') | |
a7440261 PH |
125 | self.assertEqual(sanitize_filename('.gasdgf'), 'gasdgf') |
126 | self.assertEqual(sanitize_filename('.gasdgf', is_id=True), '.gasdgf') | |
2aeb06d6 | 127 | |
59ae15a5 PH |
128 | forbidden = '"\0\\/' |
129 | for fc in forbidden: | |
130 | for fbc in forbidden: | |
131 | self.assertTrue(fbc not in sanitize_filename(fc)) | |
132 | ||
133 | def test_sanitize_filename_restricted(self): | |
134 | self.assertEqual(sanitize_filename('abc', restricted=True), 'abc') | |
135 | self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e') | |
136 | ||
137 | self.assertEqual(sanitize_filename('123', restricted=True), '123') | |
138 | ||
139 | self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True)) | |
140 | self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True)) | |
141 | ||
142 | self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True)) | |
143 | self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True)) | |
144 | self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True)) | |
145 | self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True)) | |
146 | ||
79a2e94e AT |
147 | tests = 'aäb\u4e2d\u56fd\u7684c' |
148 | self.assertEqual(sanitize_filename(tests, restricted=True), 'aab_c') | |
4e408e47 | 149 | self.assertTrue(sanitize_filename('\xf6', restricted=True) != '') # No empty filename |
59ae15a5 | 150 | |
627dcfff | 151 | forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#' |
59ae15a5 PH |
152 | for fc in forbidden: |
153 | for fbc in forbidden: | |
154 | self.assertTrue(fbc not in sanitize_filename(fc, restricted=True)) | |
155 | ||
156 | # Handle a common case more neatly | |
4e408e47 PH |
157 | self.assertEqual(sanitize_filename('\u5927\u58f0\u5e26 - Song', restricted=True), 'Song') |
158 | self.assertEqual(sanitize_filename('\u603b\u7edf: Speech', restricted=True), 'Speech') | |
59ae15a5 PH |
159 | # .. but make sure the file name is never empty |
160 | self.assertTrue(sanitize_filename('-', restricted=True) != '') | |
161 | self.assertTrue(sanitize_filename(':', restricted=True) != '') | |
162 | ||
79a2e94e | 163 | self.assertEqual(sanitize_filename( |
b96f007e | 164 | 'ÂÃÄÀÁÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖŐØŒÙÚÛÜŰÝÞßàáâãäåæçèéêëìíîïðñòóôõöőøœùúûüűýþÿ', restricted=True), |
165 | 'AAAAAAAECEEEEIIIIDNOOOOOOOOEUUUUUYPssaaaaaaaeceeeeiiiionooooooooeuuuuuypy') | |
79a2e94e | 166 | |
796173d0 | 167 | def test_sanitize_ids(self): |
314d506b PH |
168 | self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw') |
169 | self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw') | |
170 | self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI') | |
796173d0 | 171 | |
a2aaf4db S |
172 | def test_sanitize_path(self): |
173 | if sys.platform != 'win32': | |
174 | return | |
175 | ||
176 | self.assertEqual(sanitize_path('abc'), 'abc') | |
177 | self.assertEqual(sanitize_path('abc/def'), 'abc\\def') | |
178 | self.assertEqual(sanitize_path('abc\\def'), 'abc\\def') | |
179 | self.assertEqual(sanitize_path('abc|def'), 'abc#def') | |
180 | self.assertEqual(sanitize_path('<>:"|?*'), '#######') | |
181 | self.assertEqual(sanitize_path('C:/abc/def'), 'C:\\abc\\def') | |
182 | self.assertEqual(sanitize_path('C?:/abc/def'), 'C##\\abc\\def') | |
183 | ||
184 | self.assertEqual(sanitize_path('\\\\?\\UNC\\ComputerName\\abc'), '\\\\?\\UNC\\ComputerName\\abc') | |
185 | self.assertEqual(sanitize_path('\\\\?\\UNC/ComputerName/abc'), '\\\\?\\UNC\\ComputerName\\abc') | |
186 | ||
187 | self.assertEqual(sanitize_path('\\\\?\\C:\\abc'), '\\\\?\\C:\\abc') | |
188 | self.assertEqual(sanitize_path('\\\\?\\C:/abc'), '\\\\?\\C:\\abc') | |
189 | self.assertEqual(sanitize_path('\\\\?\\C:\\ab?c\\de:f'), '\\\\?\\C:\\ab#c\\de#f') | |
190 | self.assertEqual(sanitize_path('\\\\?\\C:\\abc'), '\\\\?\\C:\\abc') | |
191 | ||
f18ef2d1 S |
192 | self.assertEqual( |
193 | sanitize_path('youtube/%(uploader)s/%(autonumber)s-%(title)s-%(upload_date)s.%(ext)s'), | |
194 | 'youtube\\%(uploader)s\\%(autonumber)s-%(title)s-%(upload_date)s.%(ext)s') | |
195 | ||
196 | self.assertEqual( | |
197 | sanitize_path('youtube/TheWreckingYard ./00001-Not bad, Especially for Free! (1987 Yamaha 700)-20141116.mp4.part'), | |
198 | 'youtube\\TheWreckingYard #\\00001-Not bad, Especially for Free! (1987 Yamaha 700)-20141116.mp4.part') | |
199 | self.assertEqual(sanitize_path('abc/def...'), 'abc\\def..#') | |
200 | self.assertEqual(sanitize_path('abc.../def'), 'abc..#\\def') | |
201 | self.assertEqual(sanitize_path('abc.../def...'), 'abc..#\\def..#') | |
202 | ||
2ebfeaca S |
203 | self.assertEqual(sanitize_path('../abc'), '..\\abc') |
204 | self.assertEqual(sanitize_path('../../abc'), '..\\..\\abc') | |
205 | self.assertEqual(sanitize_path('./abc'), 'abc') | |
206 | self.assertEqual(sanitize_path('./../abc'), '..\\abc') | |
207 | ||
a4bcaad7 S |
208 | def test_prepend_extension(self): |
209 | self.assertEqual(prepend_extension('abc.ext', 'temp'), 'abc.temp.ext') | |
210 | self.assertEqual(prepend_extension('abc.ext', 'temp', 'ext'), 'abc.temp.ext') | |
211 | self.assertEqual(prepend_extension('abc.unexpected_ext', 'temp', 'ext'), 'abc.unexpected_ext.temp') | |
212 | self.assertEqual(prepend_extension('abc', 'temp'), 'abc.temp') | |
213 | self.assertEqual(prepend_extension('.abc', 'temp'), '.abc.temp') | |
214 | self.assertEqual(prepend_extension('.abc.ext', 'temp'), '.abc.temp.ext') | |
215 | ||
b3ed15b7 S |
216 | def test_replace_extension(self): |
217 | self.assertEqual(replace_extension('abc.ext', 'temp'), 'abc.temp') | |
218 | self.assertEqual(replace_extension('abc.ext', 'temp', 'ext'), 'abc.temp') | |
219 | self.assertEqual(replace_extension('abc.unexpected_ext', 'temp', 'ext'), 'abc.unexpected_ext.temp') | |
220 | self.assertEqual(replace_extension('abc', 'temp'), 'abc.temp') | |
221 | self.assertEqual(replace_extension('.abc', 'temp'), '.abc.temp') | |
222 | self.assertEqual(replace_extension('.abc.ext', 'temp'), '.abc.temp') | |
223 | ||
46bc9b7d S |
224 | def test_remove_start(self): |
225 | self.assertEqual(remove_start(None, 'A - '), None) | |
226 | self.assertEqual(remove_start('A - B', 'A - '), 'B') | |
227 | self.assertEqual(remove_start('B - A', 'A - '), 'B - A') | |
228 | ||
229 | def test_remove_end(self): | |
230 | self.assertEqual(remove_end(None, ' - B'), None) | |
231 | self.assertEqual(remove_end('A - B', ' - B'), 'A') | |
232 | self.assertEqual(remove_end('B - A', ' - B'), 'B - A') | |
233 | ||
31b2051e S |
234 | def test_remove_quotes(self): |
235 | self.assertEqual(remove_quotes(None), None) | |
236 | self.assertEqual(remove_quotes('"'), '"') | |
237 | self.assertEqual(remove_quotes("'"), "'") | |
238 | self.assertEqual(remove_quotes(';'), ';') | |
239 | self.assertEqual(remove_quotes('";'), '";') | |
240 | self.assertEqual(remove_quotes('""'), '') | |
241 | self.assertEqual(remove_quotes('";"'), ';') | |
242 | ||
59ae15a5 | 243 | def test_ordered_set(self): |
627dcfff | 244 | self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7]) |
59ae15a5 PH |
245 | self.assertEqual(orderedSet([]), []) |
246 | self.assertEqual(orderedSet([1]), [1]) | |
5f6a1245 | 247 | # keep the list ordered |
627dcfff | 248 | self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1]) |
59ae15a5 PH |
249 | |
250 | def test_unescape_html(self): | |
4e408e47 | 251 | self.assertEqual(unescapeHTML('%20;'), '%20;') |
91757b0f NJ |
252 | self.assertEqual(unescapeHTML('/'), '/') |
253 | self.assertEqual(unescapeHTML('/'), '/') | |
7aefc49c S |
254 | self.assertEqual(unescapeHTML('é'), 'é') |
255 | self.assertEqual(unescapeHTML('�'), '�') | |
55b2f099 YCH |
256 | # HTML5 entities |
257 | self.assertEqual(unescapeHTML('.''), '.\'') | |
5f6a1245 | 258 | |
eb9c3edd JMF |
259 | def test_date_from_str(self): |
260 | self.assertEqual(date_from_str('yesterday'), date_from_str('now-1day')) | |
261 | self.assertEqual(date_from_str('now+7day'), date_from_str('now+1week')) | |
262 | self.assertEqual(date_from_str('now+14day'), date_from_str('now+2week')) | |
263 | self.assertEqual(date_from_str('now+365day'), date_from_str('now+1year')) | |
264 | self.assertEqual(date_from_str('now+30day'), date_from_str('now+1month')) | |
265 | ||
bd558525 | 266 | def test_daterange(self): |
5f6a1245 | 267 | _20century = DateRange("19000101", "20000101") |
bd558525 JMF |
268 | self.assertFalse("17890714" in _20century) |
269 | _ac = DateRange("00010101") | |
270 | self.assertTrue("19690721" in _ac) | |
271 | _firstmilenium = DateRange(end="10000101") | |
272 | self.assertTrue("07110427" in _firstmilenium) | |
37254abc | 273 | |
bf50b038 JMF |
274 | def test_unified_dates(self): |
275 | self.assertEqual(unified_strdate('December 21, 2010'), '20101221') | |
276 | self.assertEqual(unified_strdate('8/7/2009'), '20090708') | |
277 | self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214') | |
278 | self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011') | |
a69801e2 | 279 | self.assertEqual(unified_strdate('1968 12 10'), '19681210') |
026fcc04 | 280 | self.assertEqual(unified_strdate('1968-12-10'), '19681210') |
99b67fec | 281 | self.assertEqual(unified_strdate('28/01/2014 21:00:00 +0100'), '20140128') |
42bdd9d0 PH |
282 | self.assertEqual( |
283 | unified_strdate('11/26/2014 11:30:00 AM PST', day_first=False), | |
284 | '20141126') | |
9bb8e0a3 PH |
285 | self.assertEqual( |
286 | unified_strdate('2/2/2015 6:47:40 PM', day_first=False), | |
287 | '20150202') | |
f160785c | 288 | self.assertEqual(unified_strdate('Feb 14th 2016 5:45PM'), '20160214') |
8cf70de4 | 289 | self.assertEqual(unified_strdate('25-09-2014'), '20140925') |
46f59e89 | 290 | self.assertEqual(unified_strdate('27.02.2016 17:30'), '20160227') |
6a750402 | 291 | self.assertEqual(unified_strdate('UNKNOWN DATE FORMAT'), None) |
dae7c920 | 292 | |
46f59e89 S |
293 | def test_unified_timestamps(self): |
294 | self.assertEqual(unified_timestamp('December 21, 2010'), 1292889600) | |
295 | self.assertEqual(unified_timestamp('8/7/2009'), 1247011200) | |
296 | self.assertEqual(unified_timestamp('Dec 14, 2012'), 1355443200) | |
297 | self.assertEqual(unified_timestamp('2012/10/11 01:56:38 +0000'), 1349920598) | |
298 | self.assertEqual(unified_timestamp('1968 12 10'), -33436800) | |
299 | self.assertEqual(unified_timestamp('1968-12-10'), -33436800) | |
300 | self.assertEqual(unified_timestamp('28/01/2014 21:00:00 +0100'), 1390939200) | |
301 | self.assertEqual( | |
302 | unified_timestamp('11/26/2014 11:30:00 AM PST', day_first=False), | |
303 | 1417001400) | |
304 | self.assertEqual( | |
305 | unified_timestamp('2/2/2015 6:47:40 PM', day_first=False), | |
306 | 1422902860) | |
307 | self.assertEqual(unified_timestamp('Feb 14th 2016 5:45PM'), 1455471900) | |
308 | self.assertEqual(unified_timestamp('25-09-2014'), 1411603200) | |
309 | self.assertEqual(unified_timestamp('27.02.2016 17:30'), 1456594200) | |
310 | self.assertEqual(unified_timestamp('UNKNOWN DATE FORMAT'), None) | |
311 | ||
5035536e S |
312 | def test_determine_ext(self): |
313 | self.assertEqual(determine_ext('http://example.com/foo/bar.mp4/?download'), 'mp4') | |
314 | self.assertEqual(determine_ext('http://example.com/foo/bar/?download', None), None) | |
9cb9a5df S |
315 | self.assertEqual(determine_ext('http://example.com/foo/bar.nonext/?download', None), None) |
316 | self.assertEqual(determine_ext('http://example.com/foo/bar/mp4?download', None), None) | |
317 | self.assertEqual(determine_ext('http://example.com/foo/bar.m3u8//?download'), 'm3u8') | |
5035536e | 318 | |
59ae56fa | 319 | def test_find_xpath_attr(self): |
4e408e47 | 320 | testxml = '''<root> |
59ae56fa PH |
321 | <node/> |
322 | <node x="a"/> | |
323 | <node x="a" y="c" /> | |
324 | <node x="b" y="d" /> | |
ee114368 | 325 | <node x="" /> |
59ae56fa | 326 | </root>''' |
36e6f62c | 327 | doc = compat_etree_fromstring(testxml) |
59ae56fa | 328 | |
ee114368 | 329 | self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n'), None) |
59ae56fa | 330 | self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None) |
ee114368 S |
331 | self.assertEqual(find_xpath_attr(doc, './/node', 'n'), None) |
332 | self.assertEqual(find_xpath_attr(doc, './/node', 'n', 'v'), None) | |
333 | self.assertEqual(find_xpath_attr(doc, './/node', 'x'), doc[1]) | |
59ae56fa | 334 | self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1]) |
ee114368 S |
335 | self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'b'), doc[3]) |
336 | self.assertEqual(find_xpath_attr(doc, './/node', 'y'), doc[2]) | |
59ae56fa | 337 | self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2]) |
ee114368 S |
338 | self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'd'), doc[3]) |
339 | self.assertEqual(find_xpath_attr(doc, './/node', 'x', ''), doc[4]) | |
59ae56fa | 340 | |
d7e66d39 | 341 | def test_xpath_with_ns(self): |
4e408e47 | 342 | testxml = '''<root xmlns:media="http://example.com/"> |
d7e66d39 JMF |
343 | <media:song> |
344 | <media:author>The Author</media:author> | |
345 | <url>http://server.com/download.mp3</url> | |
346 | </media:song> | |
347 | </root>''' | |
36e6f62c | 348 | doc = compat_etree_fromstring(testxml) |
d7e66d39 JMF |
349 | find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'})) |
350 | self.assertTrue(find('media:song') is not None) | |
4e408e47 PH |
351 | self.assertEqual(find('media:song/media:author').text, 'The Author') |
352 | self.assertEqual(find('media:song/url').text, 'http://server.com/download.mp3') | |
d7e66d39 | 353 | |
87f70ab3 S |
354 | def test_xpath_element(self): |
355 | doc = xml.etree.ElementTree.Element('root') | |
356 | div = xml.etree.ElementTree.SubElement(doc, 'div') | |
357 | p = xml.etree.ElementTree.SubElement(div, 'p') | |
358 | p.text = 'Foo' | |
359 | self.assertEqual(xpath_element(doc, 'div/p'), p) | |
578c0745 S |
360 | self.assertEqual(xpath_element(doc, ['div/p']), p) |
361 | self.assertEqual(xpath_element(doc, ['div/bar', 'div/p']), p) | |
87f70ab3 | 362 | self.assertEqual(xpath_element(doc, 'div/bar', default='default'), 'default') |
578c0745 | 363 | self.assertEqual(xpath_element(doc, ['div/bar'], default='default'), 'default') |
87f70ab3 | 364 | self.assertTrue(xpath_element(doc, 'div/bar') is None) |
578c0745 S |
365 | self.assertTrue(xpath_element(doc, ['div/bar']) is None) |
366 | self.assertTrue(xpath_element(doc, ['div/bar'], 'div/baz') is None) | |
87f70ab3 | 367 | self.assertRaises(ExtractorError, xpath_element, doc, 'div/bar', fatal=True) |
578c0745 S |
368 | self.assertRaises(ExtractorError, xpath_element, doc, ['div/bar'], fatal=True) |
369 | self.assertRaises(ExtractorError, xpath_element, doc, ['div/bar', 'div/baz'], fatal=True) | |
87f70ab3 | 370 | |
5379a2d4 JMF |
371 | def test_xpath_text(self): |
372 | testxml = '''<root> | |
373 | <div> | |
374 | <p>Foo</p> | |
375 | </div> | |
376 | </root>''' | |
36e6f62c | 377 | doc = compat_etree_fromstring(testxml) |
5379a2d4 | 378 | self.assertEqual(xpath_text(doc, 'div/p'), 'Foo') |
87f70ab3 | 379 | self.assertEqual(xpath_text(doc, 'div/bar', default='default'), 'default') |
5379a2d4 JMF |
380 | self.assertTrue(xpath_text(doc, 'div/bar') is None) |
381 | self.assertRaises(ExtractorError, xpath_text, doc, 'div/bar', fatal=True) | |
382 | ||
87f70ab3 S |
383 | def test_xpath_attr(self): |
384 | testxml = '''<root> | |
385 | <div> | |
386 | <p x="a">Foo</p> | |
387 | </div> | |
388 | </root>''' | |
36e6f62c | 389 | doc = compat_etree_fromstring(testxml) |
87f70ab3 S |
390 | self.assertEqual(xpath_attr(doc, 'div/p', 'x'), 'a') |
391 | self.assertEqual(xpath_attr(doc, 'div/bar', 'x'), None) | |
392 | self.assertEqual(xpath_attr(doc, 'div/p', 'y'), None) | |
393 | self.assertEqual(xpath_attr(doc, 'div/bar', 'x', default='default'), 'default') | |
394 | self.assertEqual(xpath_attr(doc, 'div/p', 'y', default='default'), 'default') | |
395 | self.assertRaises(ExtractorError, xpath_attr, doc, 'div/bar', 'x', fatal=True) | |
396 | self.assertRaises(ExtractorError, xpath_attr, doc, 'div/p', 'y', fatal=True) | |
397 | ||
9d4660ca | 398 | def test_smuggle_url(self): |
e075a44a | 399 | data = {"ö": "ö", "abc": [3]} |
9d4660ca PH |
400 | url = 'https://foo.bar/baz?x=y#a' |
401 | smug_url = smuggle_url(url, data) | |
402 | unsmug_url, unsmug_data = unsmuggle_url(smug_url) | |
403 | self.assertEqual(url, unsmug_url) | |
404 | self.assertEqual(data, unsmug_data) | |
405 | ||
406 | res_url, res_data = unsmuggle_url(url) | |
407 | self.assertEqual(res_url, url) | |
408 | self.assertEqual(res_data, None) | |
409 | ||
dfaa86b7 RA |
410 | smug_url = smuggle_url(url, {'a': 'b'}) |
411 | smug_smug_url = smuggle_url(smug_url, {'c': 'd'}) | |
412 | res_url, res_data = unsmuggle_url(smug_smug_url) | |
413 | self.assertEqual(res_url, url) | |
414 | self.assertEqual(res_data, {'a': 'b', 'c': 'd'}) | |
415 | ||
a6a173c2 | 416 | def test_shell_quote(self): |
4e408e47 PH |
417 | args = ['ffmpeg', '-i', encodeFilename('ñ€ß\'.mp4')] |
418 | self.assertEqual(shell_quote(args), """ffmpeg -i 'ñ€ß'"'"'.mp4'""") | |
a6a173c2 | 419 | |
f53c966a JMF |
420 | def test_str_to_int(self): |
421 | self.assertEqual(str_to_int('123,456'), 123456) | |
422 | self.assertEqual(str_to_int('123.456'), 123456) | |
423 | ||
29eb5174 | 424 | def test_url_basename(self): |
4e408e47 PH |
425 | self.assertEqual(url_basename('http://foo.de/'), '') |
426 | self.assertEqual(url_basename('http://foo.de/bar/baz'), 'baz') | |
427 | self.assertEqual(url_basename('http://foo.de/bar/baz?x=y'), 'baz') | |
428 | self.assertEqual(url_basename('http://foo.de/bar/baz#x=y'), 'baz') | |
429 | self.assertEqual(url_basename('http://foo.de/bar/baz/'), 'baz') | |
d6c7a367 | 430 | self.assertEqual( |
4e408e47 PH |
431 | url_basename('http://media.w3.org/2010/05/sintel/trailer.mp4'), |
432 | 'trailer.mp4') | |
9d4660ca | 433 | |
608d11f5 PH |
434 | def test_parse_duration(self): |
435 | self.assertEqual(parse_duration(None), None) | |
a5fb718c S |
436 | self.assertEqual(parse_duration(False), None) |
437 | self.assertEqual(parse_duration('invalid'), None) | |
608d11f5 PH |
438 | self.assertEqual(parse_duration('1'), 1) |
439 | self.assertEqual(parse_duration('1337:12'), 80232) | |
440 | self.assertEqual(parse_duration('9:12:43'), 33163) | |
2db806b4 S |
441 | self.assertEqual(parse_duration('12:00'), 720) |
442 | self.assertEqual(parse_duration('00:01:01'), 61) | |
608d11f5 | 443 | self.assertEqual(parse_duration('x:y'), None) |
2db806b4 | 444 | self.assertEqual(parse_duration('3h11m53s'), 11513) |
ca7b3246 S |
445 | self.assertEqual(parse_duration('3h 11m 53s'), 11513) |
446 | self.assertEqual(parse_duration('3 hours 11 minutes 53 seconds'), 11513) | |
447 | self.assertEqual(parse_duration('3 hours 11 mins 53 secs'), 11513) | |
2db806b4 S |
448 | self.assertEqual(parse_duration('62m45s'), 3765) |
449 | self.assertEqual(parse_duration('6m59s'), 419) | |
450 | self.assertEqual(parse_duration('49s'), 49) | |
451 | self.assertEqual(parse_duration('0h0m0s'), 0) | |
452 | self.assertEqual(parse_duration('0m0s'), 0) | |
453 | self.assertEqual(parse_duration('0s'), 0) | |
7adcbe75 | 454 | self.assertEqual(parse_duration('01:02:03.05'), 3723.05) |
6a68bb57 | 455 | self.assertEqual(parse_duration('T30M38S'), 1838) |
e8df5cee PH |
456 | self.assertEqual(parse_duration('5 s'), 5) |
457 | self.assertEqual(parse_duration('3 min'), 180) | |
458 | self.assertEqual(parse_duration('2.5 hours'), 9000) | |
8f4b58d7 PH |
459 | self.assertEqual(parse_duration('02:03:04'), 7384) |
460 | self.assertEqual(parse_duration('01:02:03:04'), 93784) | |
3e675fab | 461 | self.assertEqual(parse_duration('1 hour 3 minutes'), 3780) |
9c29bc69 | 462 | self.assertEqual(parse_duration('87 Min.'), 5220) |
acaff495 | 463 | self.assertEqual(parse_duration('PT1H0.040S'), 3600.04) |
608d11f5 | 464 | |
5aafe895 PH |
465 | def test_fix_xml_ampersands(self): |
466 | self.assertEqual( | |
467 | fix_xml_ampersands('"&x=y&z=a'), '"&x=y&z=a') | |
468 | self.assertEqual( | |
469 | fix_xml_ampersands('"&x=y&wrong;&z=a'), | |
470 | '"&x=y&wrong;&z=a') | |
471 | self.assertEqual( | |
472 | fix_xml_ampersands('&'><"'), | |
473 | '&'><"') | |
474 | self.assertEqual( | |
475 | fix_xml_ampersands('Ӓ᪼'), 'Ӓ᪼') | |
476 | self.assertEqual(fix_xml_ampersands('&#&#'), '&#&#') | |
477 | ||
b7ab0590 PH |
478 | def test_paged_list(self): |
479 | def testPL(size, pagesize, sliceargs, expected): | |
480 | def get_page(pagenum): | |
481 | firstid = pagenum * pagesize | |
482 | upto = min(size, pagenum * pagesize + pagesize) | |
483 | for i in range(firstid, upto): | |
484 | yield i | |
485 | ||
9c44d242 | 486 | pl = OnDemandPagedList(get_page, pagesize) |
b7ab0590 PH |
487 | got = pl.getslice(*sliceargs) |
488 | self.assertEqual(got, expected) | |
489 | ||
9c44d242 PH |
490 | iapl = InAdvancePagedList(get_page, size // pagesize + 1, pagesize) |
491 | got = iapl.getslice(*sliceargs) | |
492 | self.assertEqual(got, expected) | |
493 | ||
b7ab0590 PH |
494 | testPL(5, 2, (), [0, 1, 2, 3, 4]) |
495 | testPL(5, 2, (1,), [1, 2, 3, 4]) | |
496 | testPL(5, 2, (2,), [2, 3, 4]) | |
497 | testPL(5, 2, (4,), [4]) | |
498 | testPL(5, 2, (0, 3), [0, 1, 2]) | |
499 | testPL(5, 2, (1, 4), [1, 2, 3]) | |
500 | testPL(5, 2, (2, 99), [2, 3, 4]) | |
501 | testPL(5, 2, (20, 99), []) | |
502 | ||
62e609ab | 503 | def test_read_batch_urls(self): |
4e408e47 | 504 | f = io.StringIO('''\xef\xbb\xbf foo |
62e609ab PH |
505 | bar\r |
506 | baz | |
507 | # More after this line\r | |
508 | ; or after this | |
509 | bam''') | |
4e408e47 | 510 | self.assertEqual(read_batch_urls(f), ['foo', 'bar', 'baz', 'bam']) |
62e609ab | 511 | |
b74fa8cd JMF |
512 | def test_urlencode_postdata(self): |
513 | data = urlencode_postdata({'username': 'foo@bar.com', 'password': '1234'}) | |
514 | self.assertTrue(isinstance(data, bytes)) | |
515 | ||
fb640d0a | 516 | def test_update_url_query(self): |
517 | def query_dict(url): | |
518 | return compat_parse_qs(compat_urlparse.urlparse(url).query) | |
519 | self.assertEqual(query_dict(update_url_query( | |
520 | 'http://example.com/path', {'quality': ['HD'], 'format': ['mp4']})), | |
521 | query_dict('http://example.com/path?quality=HD&format=mp4')) | |
522 | self.assertEqual(query_dict(update_url_query( | |
523 | 'http://example.com/path', {'system': ['LINUX', 'WINDOWS']})), | |
524 | query_dict('http://example.com/path?system=LINUX&system=WINDOWS')) | |
525 | self.assertEqual(query_dict(update_url_query( | |
526 | 'http://example.com/path', {'fields': 'id,formats,subtitles'})), | |
527 | query_dict('http://example.com/path?fields=id,formats,subtitles')) | |
528 | self.assertEqual(query_dict(update_url_query( | |
529 | 'http://example.com/path', {'fields': ('id,formats,subtitles', 'thumbnails')})), | |
530 | query_dict('http://example.com/path?fields=id,formats,subtitles&fields=thumbnails')) | |
531 | self.assertEqual(query_dict(update_url_query( | |
532 | 'http://example.com/path?manifest=f4m', {'manifest': []})), | |
533 | query_dict('http://example.com/path')) | |
534 | self.assertEqual(query_dict(update_url_query( | |
535 | 'http://example.com/path?system=LINUX&system=WINDOWS', {'system': 'LINUX'})), | |
536 | query_dict('http://example.com/path?system=LINUX')) | |
537 | self.assertEqual(query_dict(update_url_query( | |
538 | 'http://example.com/path', {'fields': b'id,formats,subtitles'})), | |
539 | query_dict('http://example.com/path?fields=id,formats,subtitles')) | |
3201a67f | 540 | self.assertEqual(query_dict(update_url_query( |
541 | 'http://example.com/path', {'width': 1080, 'height': 720})), | |
542 | query_dict('http://example.com/path?width=1080&height=720')) | |
543 | self.assertEqual(query_dict(update_url_query( | |
544 | 'http://example.com/path', {'bitrate': 5020.43})), | |
545 | query_dict('http://example.com/path?bitrate=5020.43')) | |
546 | self.assertEqual(query_dict(update_url_query( | |
547 | 'http://example.com/path', {'test': '第二行тест'})), | |
548 | query_dict('http://example.com/path?test=%E7%AC%AC%E4%BA%8C%E8%A1%8C%D1%82%D0%B5%D1%81%D1%82')) | |
fb640d0a | 549 | |
cbecc9b9 | 550 | def test_dict_get(self): |
86296ad2 S |
551 | FALSE_VALUES = { |
552 | 'none': None, | |
553 | 'false': False, | |
554 | 'zero': 0, | |
555 | 'empty_string': '', | |
556 | 'empty_list': [], | |
cbecc9b9 | 557 | } |
86296ad2 S |
558 | d = FALSE_VALUES.copy() |
559 | d['a'] = 42 | |
cbecc9b9 S |
560 | self.assertEqual(dict_get(d, 'a'), 42) |
561 | self.assertEqual(dict_get(d, 'b'), None) | |
562 | self.assertEqual(dict_get(d, 'b', 42), 42) | |
563 | self.assertEqual(dict_get(d, ('a', )), 42) | |
564 | self.assertEqual(dict_get(d, ('b', 'a', )), 42) | |
565 | self.assertEqual(dict_get(d, ('b', 'c', 'a', 'd', )), 42) | |
566 | self.assertEqual(dict_get(d, ('b', 'c', )), None) | |
567 | self.assertEqual(dict_get(d, ('b', 'c', ), 42), 42) | |
86296ad2 S |
568 | for key, false_value in FALSE_VALUES.items(): |
569 | self.assertEqual(dict_get(d, ('b', 'c', key, )), None) | |
570 | self.assertEqual(dict_get(d, ('b', 'c', key, ), skip_false_values=False), false_value) | |
cbecc9b9 | 571 | |
6b77d52b S |
572 | def test_encode_compat_str(self): |
573 | self.assertEqual(encode_compat_str(b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82', 'utf-8'), 'тест') | |
574 | self.assertEqual(encode_compat_str('тест', 'utf-8'), 'тест') | |
575 | ||
912b38b4 PH |
576 | def test_parse_iso8601(self): |
577 | self.assertEqual(parse_iso8601('2014-03-23T23:04:26+0100'), 1395612266) | |
578 | self.assertEqual(parse_iso8601('2014-03-23T22:04:26+0000'), 1395612266) | |
579 | self.assertEqual(parse_iso8601('2014-03-23T22:04:26Z'), 1395612266) | |
6ad4013d | 580 | self.assertEqual(parse_iso8601('2014-03-23T22:04:26.1234Z'), 1395612266) |
52c3a6e4 S |
581 | self.assertEqual(parse_iso8601('2015-09-29T08:27:31.727'), 1443515251) |
582 | self.assertEqual(parse_iso8601('2015-09-29T08-27-31.727'), None) | |
912b38b4 | 583 | |
fac55558 PH |
584 | def test_strip_jsonp(self): |
585 | stripped = strip_jsonp('cb ([ {"id":"532cb",\n\n\n"x":\n3}\n]\n);') | |
586 | d = json.loads(stripped) | |
587 | self.assertEqual(d, [{"id": "532cb", "x": 3}]) | |
588 | ||
609a61e3 PH |
589 | stripped = strip_jsonp('parseMetadata({"STATUS":"OK"})\n\n\n//epc') |
590 | d = json.loads(stripped) | |
591 | self.assertEqual(d, {'STATUS': 'OK'}) | |
592 | ||
8411229b S |
593 | stripped = strip_jsonp('ps.embedHandler({"status": "success"});') |
594 | d = json.loads(stripped) | |
595 | self.assertEqual(d, {'status': 'success'}) | |
596 | ||
173a7026 | 597 | def test_uppercase_escape(self): |
4e408e47 PH |
598 | self.assertEqual(uppercase_escape('aä'), 'aä') |
599 | self.assertEqual(uppercase_escape('\\U0001d550'), '𝕐') | |
fac55558 | 600 | |
0fe2ff78 YCH |
601 | def test_lowercase_escape(self): |
602 | self.assertEqual(lowercase_escape('aä'), 'aä') | |
603 | self.assertEqual(lowercase_escape('\\u0026'), '&') | |
604 | ||
a020a0dc PH |
605 | def test_limit_length(self): |
606 | self.assertEqual(limit_length(None, 12), None) | |
607 | self.assertEqual(limit_length('foo', 12), 'foo') | |
608 | self.assertTrue( | |
609 | limit_length('foo bar baz asd', 12).startswith('foo bar')) | |
610 | self.assertTrue('...' in limit_length('foo bar baz asd', 12)) | |
611 | ||
4f3c5e06 | 612 | def test_parse_codecs(self): |
613 | self.assertEqual(parse_codecs(''), {}) | |
614 | self.assertEqual(parse_codecs('avc1.77.30, mp4a.40.2'), { | |
615 | 'vcodec': 'avc1.77.30', | |
616 | 'acodec': 'mp4a.40.2', | |
617 | }) | |
618 | self.assertEqual(parse_codecs('mp4a.40.2'), { | |
619 | 'vcodec': 'none', | |
620 | 'acodec': 'mp4a.40.2', | |
621 | }) | |
622 | self.assertEqual(parse_codecs('mp4a.40.5,avc1.42001e'), { | |
623 | 'vcodec': 'avc1.42001e', | |
624 | 'acodec': 'mp4a.40.5', | |
625 | }) | |
626 | self.assertEqual(parse_codecs('avc3.640028'), { | |
627 | 'vcodec': 'avc3.640028', | |
628 | 'acodec': 'none', | |
629 | }) | |
630 | self.assertEqual(parse_codecs(', h264,,newcodec,aac'), { | |
631 | 'vcodec': 'h264', | |
632 | 'acodec': 'aac', | |
633 | }) | |
634 | ||
d05cfe06 S |
635 | def test_escape_rfc3986(self): |
636 | reserved = "!*'();:@&=+$,/?#[]" | |
637 | unreserved = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~' | |
638 | self.assertEqual(escape_rfc3986(reserved), reserved) | |
639 | self.assertEqual(escape_rfc3986(unreserved), unreserved) | |
640 | self.assertEqual(escape_rfc3986('тест'), '%D1%82%D0%B5%D1%81%D1%82') | |
641 | self.assertEqual(escape_rfc3986('%D1%82%D0%B5%D1%81%D1%82'), '%D1%82%D0%B5%D1%81%D1%82') | |
642 | self.assertEqual(escape_rfc3986('foo bar'), 'foo%20bar') | |
643 | self.assertEqual(escape_rfc3986('foo%20bar'), 'foo%20bar') | |
644 | ||
645 | def test_escape_url(self): | |
646 | self.assertEqual( | |
647 | escape_url('http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavré_FD.mp4'), | |
648 | 'http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavre%CC%81_FD.mp4' | |
649 | ) | |
650 | self.assertEqual( | |
651 | escape_url('http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erklärt/Das-Erste/Video?documentId=22673108&bcastId=5290'), | |
652 | 'http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erkl%C3%A4rt/Das-Erste/Video?documentId=22673108&bcastId=5290' | |
653 | ) | |
654 | self.assertEqual( | |
655 | escape_url('http://тест.рф/фрагмент'), | |
2d60465e | 656 | 'http://xn--e1aybc.xn--p1ai/%D1%84%D1%80%D0%B0%D0%B3%D0%BC%D0%B5%D0%BD%D1%82' |
d05cfe06 S |
657 | ) |
658 | self.assertEqual( | |
659 | escape_url('http://тест.рф/абв?абв=абв#абв'), | |
81f36eba | 660 | 'http://xn--e1aybc.xn--p1ai/%D0%B0%D0%B1%D0%B2?%D0%B0%D0%B1%D0%B2=%D0%B0%D0%B1%D0%B2#%D0%B0%D0%B1%D0%B2' |
d05cfe06 S |
661 | ) |
662 | self.assertEqual(escape_url('http://vimeo.com/56015672#at=0'), 'http://vimeo.com/56015672#at=0') | |
663 | ||
e7b6d122 | 664 | def test_js_to_json_realworld(self): |
410f3e73 | 665 | inp = '''{ |
e7b6d122 | 666 | 'clip':{'provider':'pseudo'} |
410f3e73 PH |
667 | }''' |
668 | self.assertEqual(js_to_json(inp), '''{ | |
e7b6d122 | 669 | "clip":{"provider":"pseudo"} |
410f3e73 PH |
670 | }''') |
671 | json.loads(js_to_json(inp)) | |
672 | ||
e7b6d122 PH |
673 | inp = '''{ |
674 | 'playlist':[{'controls':{'all':null}}] | |
675 | }''' | |
676 | self.assertEqual(js_to_json(inp), '''{ | |
677 | "playlist":[{"controls":{"all":null}}] | |
678 | }''') | |
679 | ||
d01949dc S |
680 | inp = '''"The CW\\'s \\'Crazy Ex-Girlfriend\\'"''' |
681 | self.assertEqual(js_to_json(inp), '''"The CW's 'Crazy Ex-Girlfriend'"''') | |
682 | ||
d305dd73 PH |
683 | inp = '"SAND Number: SAND 2013-7800P\\nPresenter: Tom Russo\\nHabanero Software Training - Xyce Software\\nXyce, Sandia\\u0027s"' |
684 | json_code = js_to_json(inp) | |
685 | self.assertEqual(json.loads(json_code), json.loads(inp)) | |
686 | ||
89ac4a19 S |
687 | inp = '''{ |
688 | 0:{src:'skipped', type: 'application/dash+xml'}, | |
689 | 1:{src:'skipped', type: 'application/vnd.apple.mpegURL'}, | |
690 | }''' | |
691 | self.assertEqual(js_to_json(inp), '''{ | |
692 | "0":{"src":"skipped", "type": "application/dash+xml"}, | |
693 | "1":{"src":"skipped", "type": "application/vnd.apple.mpegURL"} | |
694 | }''') | |
695 | ||
47212f7b YCH |
696 | inp = '''{"foo":101}''' |
697 | self.assertEqual(js_to_json(inp), '''{"foo":101}''') | |
698 | ||
e7b6d122 PH |
699 | def test_js_to_json_edgecases(self): |
700 | on = js_to_json("{abc_def:'1\\'\\\\2\\\\\\'3\"4'}") | |
701 | self.assertEqual(json.loads(on), {"abc_def": "1'\\2\\'3\"4"}) | |
702 | ||
703 | on = js_to_json('{"abc": true}') | |
704 | self.assertEqual(json.loads(on), {'abc': True}) | |
705 | ||
8f4b58d7 PH |
706 | # Ignore JavaScript code as well |
707 | on = js_to_json('''{ | |
708 | "x": 1, | |
709 | y: "a", | |
710 | z: some.code | |
711 | }''') | |
712 | d = json.loads(on) | |
713 | self.assertEqual(d['x'], 1) | |
714 | self.assertEqual(d['y'], 'a') | |
715 | ||
ba9e68f4 S |
716 | on = js_to_json('["abc", "def",]') |
717 | self.assertEqual(json.loads(on), ['abc', 'def']) | |
718 | ||
719 | on = js_to_json('{"abc": "def",}') | |
720 | self.assertEqual(json.loads(on), {'abc': 'def'}) | |
721 | ||
bd1e4844 | 722 | on = js_to_json('{ 0: /* " \n */ ",]" , }') |
723 | self.assertEqual(json.loads(on), {'0': ',]'}) | |
724 | ||
725 | on = js_to_json(r'["<p>x<\/p>"]') | |
726 | self.assertEqual(json.loads(on), ['<p>x</p>']) | |
727 | ||
728 | on = js_to_json(r'["\xaa"]') | |
729 | self.assertEqual(json.loads(on), ['\u00aa']) | |
730 | ||
731 | on = js_to_json("['a\\\nb']") | |
732 | self.assertEqual(json.loads(on), ['ab']) | |
733 | ||
89ac4a19 S |
734 | on = js_to_json('{0xff:0xff}') |
735 | self.assertEqual(json.loads(on), {'255': 255}) | |
736 | ||
737 | on = js_to_json('{077:077}') | |
738 | self.assertEqual(json.loads(on), {'63': 63}) | |
739 | ||
740 | on = js_to_json('{42:42}') | |
741 | self.assertEqual(json.loads(on), {'42': 42}) | |
742 | ||
8bb56eee BF |
743 | def test_extract_attributes(self): |
744 | self.assertEqual(extract_attributes('<e x="y">'), {'x': 'y'}) | |
745 | self.assertEqual(extract_attributes("<e x='y'>"), {'x': 'y'}) | |
746 | self.assertEqual(extract_attributes('<e x=y>'), {'x': 'y'}) | |
747 | self.assertEqual(extract_attributes('<e x="a \'b\' c">'), {'x': "a 'b' c"}) | |
748 | self.assertEqual(extract_attributes('<e x=\'a "b" c\'>'), {'x': 'a "b" c'}) | |
749 | self.assertEqual(extract_attributes('<e x="y">'), {'x': 'y'}) | |
750 | self.assertEqual(extract_attributes('<e x="y">'), {'x': 'y'}) | |
751 | self.assertEqual(extract_attributes('<e x="&">'), {'x': '&'}) # XML | |
752 | self.assertEqual(extract_attributes('<e x=""">'), {'x': '"'}) | |
c5229f39 S |
753 | self.assertEqual(extract_attributes('<e x="£">'), {'x': '£'}) # HTML 3.2 |
754 | self.assertEqual(extract_attributes('<e x="λ">'), {'x': 'λ'}) # HTML 4.0 | |
8bb56eee BF |
755 | self.assertEqual(extract_attributes('<e x="&foo">'), {'x': '&foo'}) |
756 | self.assertEqual(extract_attributes('<e x="\'">'), {'x': "'"}) | |
757 | self.assertEqual(extract_attributes('<e x=\'"\'>'), {'x': '"'}) | |
758 | self.assertEqual(extract_attributes('<e x >'), {'x': None}) | |
759 | self.assertEqual(extract_attributes('<e x=y a>'), {'x': 'y', 'a': None}) | |
760 | self.assertEqual(extract_attributes('<e x= y>'), {'x': 'y'}) | |
761 | self.assertEqual(extract_attributes('<e x=1 y=2 x=3>'), {'y': '2', 'x': '3'}) | |
762 | self.assertEqual(extract_attributes('<e \nx=\ny\n>'), {'x': 'y'}) | |
763 | self.assertEqual(extract_attributes('<e \nx=\n"y"\n>'), {'x': 'y'}) | |
764 | self.assertEqual(extract_attributes("<e \nx=\n'y'\n>"), {'x': 'y'}) | |
765 | self.assertEqual(extract_attributes('<e \nx="\ny\n">'), {'x': '\ny\n'}) | |
c5229f39 | 766 | self.assertEqual(extract_attributes('<e CAPS=x>'), {'caps': 'x'}) # Names lowercased |
8bb56eee BF |
767 | self.assertEqual(extract_attributes('<e x=1 X=2>'), {'x': '2'}) |
768 | self.assertEqual(extract_attributes('<e X=1 x=2>'), {'x': '2'}) | |
769 | self.assertEqual(extract_attributes('<e _:funny-name1=1>'), {'_:funny-name1': '1'}) | |
770 | self.assertEqual(extract_attributes('<e x="Fáilte 世界 \U0001f600">'), {'x': 'Fáilte 世界 \U0001f600'}) | |
771 | self.assertEqual(extract_attributes('<e x="décomposé">'), {'x': 'décompose\u0301'}) | |
772 | # "Narrow" Python builds don't support unicode code points outside BMP. | |
773 | try: | |
774 | compat_chr(0x10000) | |
775 | supports_outside_bmp = True | |
776 | except ValueError: | |
777 | supports_outside_bmp = False | |
778 | if supports_outside_bmp: | |
779 | self.assertEqual(extract_attributes('<e x="Smile 😀!">'), {'x': 'Smile \U0001f600!'}) | |
780 | ||
e4bdb37e PH |
781 | def test_clean_html(self): |
782 | self.assertEqual(clean_html('a:\nb'), 'a: b') | |
783 | self.assertEqual(clean_html('a:\n "b"'), 'a: "b"') | |
784 | ||
4c0924bb PH |
785 | def test_intlist_to_bytes(self): |
786 | self.assertEqual( | |
787 | intlist_to_bytes([0, 1, 127, 128, 255]), | |
788 | b'\x00\x01\x7f\x80\xff') | |
789 | ||
7d4111ed PH |
790 | def test_args_to_str(self): |
791 | self.assertEqual( | |
792 | args_to_str(['foo', 'ba/r', '-baz', '2 be', '']), | |
793 | 'foo ba/r -baz \'2 be\' \'\'' | |
794 | ) | |
795 | ||
be64b5b0 PH |
796 | def test_parse_filesize(self): |
797 | self.assertEqual(parse_filesize(None), None) | |
798 | self.assertEqual(parse_filesize(''), None) | |
799 | self.assertEqual(parse_filesize('91 B'), 91) | |
800 | self.assertEqual(parse_filesize('foobar'), None) | |
801 | self.assertEqual(parse_filesize('2 MiB'), 2097152) | |
802 | self.assertEqual(parse_filesize('5 GB'), 5000000000) | |
803 | self.assertEqual(parse_filesize('1.2Tb'), 1200000000000) | |
4349c07d | 804 | self.assertEqual(parse_filesize('1,24 KB'), 1240) |
be64b5b0 | 805 | |
fb47597b S |
806 | def test_parse_count(self): |
807 | self.assertEqual(parse_count(None), None) | |
808 | self.assertEqual(parse_count(''), None) | |
809 | self.assertEqual(parse_count('0'), 0) | |
810 | self.assertEqual(parse_count('1000'), 1000) | |
811 | self.assertEqual(parse_count('1.000'), 1000) | |
812 | self.assertEqual(parse_count('1.1k'), 1100) | |
813 | self.assertEqual(parse_count('1.1kk'), 1100000) | |
782b1b5b JMF |
814 | self.assertEqual(parse_count('1.1kk '), 1100000) |
815 | self.assertEqual(parse_count('1.1kk views'), 1100000) | |
fb47597b | 816 | |
5f9b8394 PH |
817 | def test_version_tuple(self): |
818 | self.assertEqual(version_tuple('1'), (1,)) | |
819 | self.assertEqual(version_tuple('10.23.344'), (10, 23, 344)) | |
47d7c642 | 820 | self.assertEqual(version_tuple('10.1-6'), (10, 1, 6)) # avconv style |
5f9b8394 | 821 | |
cae97f65 PH |
822 | def test_detect_exe_version(self): |
823 | self.assertEqual(detect_exe_version('''ffmpeg version 1.2.1 | |
824 | built on May 27 2013 08:37:26 with gcc 4.7 (Debian 4.7.3-4) | |
825 | configuration: --prefix=/usr --extra-'''), '1.2.1') | |
826 | self.assertEqual(detect_exe_version('''ffmpeg version N-63176-g1fb4685 | |
827 | built on May 15 2014 22:09:06 with gcc 4.8.2 (GCC)'''), 'N-63176-g1fb4685') | |
828 | self.assertEqual(detect_exe_version('''X server found. dri2 connection failed! | |
829 | Trying to open render node... | |
830 | Success at /dev/dri/renderD128. | |
831 | ffmpeg version 2.4.4 Copyright (c) 2000-2014 the FFmpeg ...'''), '2.4.4') | |
832 | ||
05900629 PH |
833 | def test_age_restricted(self): |
834 | self.assertFalse(age_restricted(None, 10)) # unrestricted content | |
835 | self.assertFalse(age_restricted(1, None)) # unrestricted policy | |
836 | self.assertFalse(age_restricted(8, 10)) | |
837 | self.assertTrue(age_restricted(18, 14)) | |
838 | self.assertFalse(age_restricted(18, 18)) | |
839 | ||
61ca9a80 PH |
840 | def test_is_html(self): |
841 | self.assertFalse(is_html(b'\x49\x44\x43<html')) | |
842 | self.assertTrue(is_html(b'<!DOCTYPE foo>\xaaa')) | |
843 | self.assertTrue(is_html( # UTF-8 with BOM | |
844 | b'\xef\xbb\xbf<!DOCTYPE foo>\xaaa')) | |
845 | self.assertTrue(is_html( # UTF-16-LE | |
846 | b'\xff\xfe<\x00h\x00t\x00m\x00l\x00>\x00\xe4\x00' | |
847 | )) | |
848 | self.assertTrue(is_html( # UTF-16-BE | |
849 | b'\xfe\xff\x00<\x00h\x00t\x00m\x00l\x00>\x00\xe4' | |
850 | )) | |
851 | self.assertTrue(is_html( # UTF-32-BE | |
852 | b'\x00\x00\xFE\xFF\x00\x00\x00<\x00\x00\x00h\x00\x00\x00t\x00\x00\x00m\x00\x00\x00l\x00\x00\x00>\x00\x00\x00\xe4')) | |
853 | self.assertTrue(is_html( # UTF-32-LE | |
854 | b'\xFF\xFE\x00\x00<\x00\x00\x00h\x00\x00\x00t\x00\x00\x00m\x00\x00\x00l\x00\x00\x00>\x00\x00\x00\xe4\x00\x00\x00')) | |
855 | ||
cfb56d1a PH |
856 | def test_render_table(self): |
857 | self.assertEqual( | |
858 | render_table( | |
859 | ['a', 'bcd'], | |
860 | [[123, 4], [9999, 51]]), | |
861 | 'a bcd\n' | |
862 | '123 4\n' | |
863 | '9999 51') | |
864 | ||
347de493 PH |
865 | def test_match_str(self): |
866 | self.assertRaises(ValueError, match_str, 'xy>foobar', {}) | |
867 | self.assertFalse(match_str('xy', {'x': 1200})) | |
868 | self.assertTrue(match_str('!xy', {'x': 1200})) | |
869 | self.assertTrue(match_str('x', {'x': 1200})) | |
870 | self.assertFalse(match_str('!x', {'x': 1200})) | |
871 | self.assertTrue(match_str('x', {'x': 0})) | |
872 | self.assertFalse(match_str('x>0', {'x': 0})) | |
873 | self.assertFalse(match_str('x>0', {})) | |
874 | self.assertTrue(match_str('x>?0', {})) | |
875 | self.assertTrue(match_str('x>1K', {'x': 1200})) | |
876 | self.assertFalse(match_str('x>2K', {'x': 1200})) | |
877 | self.assertTrue(match_str('x>=1200 & x < 1300', {'x': 1200})) | |
878 | self.assertFalse(match_str('x>=1100 & x < 1200', {'x': 1200})) | |
879 | self.assertFalse(match_str('y=a212', {'y': 'foobar42'})) | |
880 | self.assertTrue(match_str('y=foobar42', {'y': 'foobar42'})) | |
881 | self.assertFalse(match_str('y!=foobar42', {'y': 'foobar42'})) | |
882 | self.assertTrue(match_str('y!=foobar2', {'y': 'foobar42'})) | |
883 | self.assertFalse(match_str( | |
884 | 'like_count > 100 & dislike_count <? 50 & description', | |
885 | {'like_count': 90, 'description': 'foo'})) | |
886 | self.assertTrue(match_str( | |
887 | 'like_count > 100 & dislike_count <? 50 & description', | |
888 | {'like_count': 190, 'description': 'foo'})) | |
889 | self.assertFalse(match_str( | |
890 | 'like_count > 100 & dislike_count <? 50 & description', | |
891 | {'like_count': 190, 'dislike_count': 60, 'description': 'foo'})) | |
892 | self.assertFalse(match_str( | |
893 | 'like_count > 100 & dislike_count <? 50 & description', | |
894 | {'like_count': 190, 'dislike_count': 10})) | |
895 | ||
bf6427d2 | 896 | def test_parse_dfxp_time_expr(self): |
d631d5f9 YCH |
897 | self.assertEqual(parse_dfxp_time_expr(None), None) |
898 | self.assertEqual(parse_dfxp_time_expr(''), None) | |
bf6427d2 YCH |
899 | self.assertEqual(parse_dfxp_time_expr('0.1'), 0.1) |
900 | self.assertEqual(parse_dfxp_time_expr('0.1s'), 0.1) | |
901 | self.assertEqual(parse_dfxp_time_expr('00:00:01'), 1.0) | |
902 | self.assertEqual(parse_dfxp_time_expr('00:00:01.100'), 1.1) | |
db2fe38b | 903 | self.assertEqual(parse_dfxp_time_expr('00:00:01:100'), 1.1) |
bf6427d2 YCH |
904 | |
905 | def test_dfxp2srt(self): | |
906 | dfxp_data = '''<?xml version="1.0" encoding="UTF-8"?> | |
907 | <tt xmlns="http://www.w3.org/ns/ttml" xml:lang="en" xmlns:tts="http://www.w3.org/ns/ttml#parameter"> | |
908 | <body> | |
909 | <div xml:lang="en"> | |
910 | <p begin="0" end="1">The following line contains Chinese characters and special symbols</p> | |
911 | <p begin="1" end="2">第二行<br/>♪♪</p> | |
7dff0363 | 912 | <p begin="2" dur="1"><span>Third<br/>Line</span></p> |
d631d5f9 YCH |
913 | <p begin="3" end="-1">Lines with invalid timestamps are ignored</p> |
914 | <p begin="-1" end="-1">Ignore, two</p> | |
915 | <p begin="3" dur="-1">Ignored, three</p> | |
bf6427d2 YCH |
916 | </div> |
917 | </body> | |
918 | </tt>''' | |
919 | srt_data = '''1 | |
920 | 00:00:00,000 --> 00:00:01,000 | |
921 | The following line contains Chinese characters and special symbols | |
922 | ||
923 | 2 | |
924 | 00:00:01,000 --> 00:00:02,000 | |
925 | 第二行 | |
926 | ♪♪ | |
927 | ||
928 | 3 | |
929 | 00:00:02,000 --> 00:00:03,000 | |
930 | Third | |
931 | Line | |
932 | ||
933 | ''' | |
934 | self.assertEqual(dfxp2srt(dfxp_data), srt_data) | |
935 | ||
1b0427e6 YCH |
936 | dfxp_data_no_default_namespace = '''<?xml version="1.0" encoding="UTF-8"?> |
937 | <tt xml:lang="en" xmlns:tts="http://www.w3.org/ns/ttml#parameter"> | |
938 | <body> | |
939 | <div xml:lang="en"> | |
940 | <p begin="0" end="1">The first line</p> | |
941 | </div> | |
942 | </body> | |
943 | </tt>''' | |
944 | srt_data = '''1 | |
945 | 00:00:00,000 --> 00:00:01,000 | |
946 | The first line | |
947 | ||
948 | ''' | |
949 | self.assertEqual(dfxp2srt(dfxp_data_no_default_namespace), srt_data) | |
950 | ||
f7126449 S |
951 | def test_cli_option(self): |
952 | self.assertEqual(cli_option({'proxy': '127.0.0.1:3128'}, '--proxy', 'proxy'), ['--proxy', '127.0.0.1:3128']) | |
953 | self.assertEqual(cli_option({'proxy': None}, '--proxy', 'proxy'), []) | |
954 | self.assertEqual(cli_option({}, '--proxy', 'proxy'), []) | |
955 | ||
956 | def test_cli_valueless_option(self): | |
957 | self.assertEqual(cli_valueless_option( | |
958 | {'downloader': 'external'}, '--external-downloader', 'downloader', 'external'), ['--external-downloader']) | |
959 | self.assertEqual(cli_valueless_option( | |
960 | {'downloader': 'internal'}, '--external-downloader', 'downloader', 'external'), []) | |
961 | self.assertEqual(cli_valueless_option( | |
962 | {'nocheckcertificate': True}, '--no-check-certificate', 'nocheckcertificate'), ['--no-check-certificate']) | |
963 | self.assertEqual(cli_valueless_option( | |
964 | {'nocheckcertificate': False}, '--no-check-certificate', 'nocheckcertificate'), []) | |
965 | self.assertEqual(cli_valueless_option( | |
966 | {'checkcertificate': True}, '--no-check-certificate', 'checkcertificate', False), []) | |
967 | self.assertEqual(cli_valueless_option( | |
968 | {'checkcertificate': False}, '--no-check-certificate', 'checkcertificate', False), ['--no-check-certificate']) | |
969 | ||
970 | def test_cli_bool_option(self): | |
971 | self.assertEqual( | |
972 | cli_bool_option( | |
973 | {'nocheckcertificate': True}, '--no-check-certificate', 'nocheckcertificate'), | |
974 | ['--no-check-certificate', 'true']) | |
975 | self.assertEqual( | |
976 | cli_bool_option( | |
977 | {'nocheckcertificate': True}, '--no-check-certificate', 'nocheckcertificate', separator='='), | |
978 | ['--no-check-certificate=true']) | |
979 | self.assertEqual( | |
980 | cli_bool_option( | |
981 | {'nocheckcertificate': True}, '--check-certificate', 'nocheckcertificate', 'false', 'true'), | |
982 | ['--check-certificate', 'false']) | |
983 | self.assertEqual( | |
984 | cli_bool_option( | |
985 | {'nocheckcertificate': True}, '--check-certificate', 'nocheckcertificate', 'false', 'true', '='), | |
986 | ['--check-certificate=false']) | |
987 | self.assertEqual( | |
988 | cli_bool_option( | |
989 | {'nocheckcertificate': False}, '--check-certificate', 'nocheckcertificate', 'false', 'true'), | |
990 | ['--check-certificate', 'true']) | |
991 | self.assertEqual( | |
992 | cli_bool_option( | |
993 | {'nocheckcertificate': False}, '--check-certificate', 'nocheckcertificate', 'false', 'true', '='), | |
994 | ['--check-certificate=true']) | |
995 | ||
5bc880b9 YCH |
996 | def test_ohdave_rsa_encrypt(self): |
997 | N = 0xab86b6371b5318aaa1d3c9e612a9f1264f372323c8c0f19875b5fc3b3fd3afcc1e5bec527aa94bfa85bffc157e4245aebda05389a5357b75115ac94f074aefcd | |
998 | e = 65537 | |
999 | ||
1000 | self.assertEqual( | |
1001 | ohdave_rsa_encrypt(b'aa111222', e, N), | |
1002 | '726664bd9a23fd0c70f9f1b84aab5e3905ce1e45a584e9cbcf9bcc7510338fc1986d6c599ff990d923aa43c51c0d9013cd572e13bc58f4ae48f2ed8c0b0ba881') | |
cfb56d1a | 1003 | |
5eb6bdce YCH |
1004 | def test_encode_base_n(self): |
1005 | self.assertEqual(encode_base_n(0, 30), '0') | |
1006 | self.assertEqual(encode_base_n(80, 30), '2k') | |
1007 | ||
1008 | custom_table = '9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA' | |
1009 | self.assertEqual(encode_base_n(0, 30, custom_table), '9') | |
1010 | self.assertEqual(encode_base_n(80, 30, custom_table), '7P') | |
1011 | ||
1012 | self.assertRaises(ValueError, encode_base_n, 0, 70) | |
1013 | self.assertRaises(ValueError, encode_base_n, 0, 60, custom_table) | |
1014 | ||
1143535d YCH |
1015 | def test_urshift(self): |
1016 | self.assertEqual(urshift(3, 1), 1) | |
1017 | self.assertEqual(urshift(-3, 1), 2147483646) | |
1018 | ||
84c237fb YCH |
1019 | def test_get_element_by_class(self): |
1020 | html = ''' | |
1021 | <span class="foo bar">nice</span> | |
1022 | ''' | |
1023 | ||
1024 | self.assertEqual(get_element_by_class('foo', html), 'nice') | |
1025 | self.assertEqual(get_element_by_class('no-such-class', html), None) | |
1026 | ||
dae7c920 | 1027 | if __name__ == '__main__': |
59ae15a5 | 1028 | unittest.main() |