test_http.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. # Allow direct execution
  5. import os
  6. import sys
  7. import unittest
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. import contextlib
  10. import gzip
  11. import io
  12. import ssl
  13. import tempfile
  14. import threading
  15. import zlib
  16. # avoid deprecated alias assertRaisesRegexp
  17. if hasattr(unittest.TestCase, 'assertRaisesRegex'):
  18. unittest.TestCase.assertRaisesRegexp = unittest.TestCase.assertRaisesRegex
  19. try:
  20. import brotli
  21. except ImportError:
  22. brotli = None
  23. try:
  24. from urllib.request import pathname2url
  25. except ImportError:
  26. from urllib import pathname2url
  27. from youtube_dl.compat import (
  28. compat_http_cookiejar_Cookie,
  29. compat_http_server,
  30. compat_str as str,
  31. compat_urllib_error,
  32. compat_urllib_HTTPError,
  33. compat_urllib_parse,
  34. compat_urllib_request,
  35. )
  36. from youtube_dl.utils import (
  37. sanitized_Request,
  38. update_Request,
  39. urlencode_postdata,
  40. )
  41. from test.helper import (
  42. expectedFailureIf,
  43. FakeYDL,
  44. FakeLogger,
  45. http_server_port,
  46. )
  47. from youtube_dl import YoutubeDL
  48. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  49. class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler):
  50. protocol_version = 'HTTP/1.1'
  51. # work-around old/new -style class inheritance
  52. def super(self, meth_name, *args, **kwargs):
  53. from types import MethodType
  54. try:
  55. super()
  56. fn = lambda s, m, *a, **k: getattr(super(), m)(*a, **k)
  57. except TypeError:
  58. fn = lambda s, m, *a, **k: getattr(compat_http_server.BaseHTTPRequestHandler, m)(s, *a, **k)
  59. self.super = MethodType(fn, self)
  60. return self.super(meth_name, *args, **kwargs)
  61. def log_message(self, format, *args):
  62. pass
  63. def _headers(self):
  64. payload = str(self.headers).encode('utf-8')
  65. self.send_response(200)
  66. self.send_header('Content-Type', 'application/json')
  67. self.send_header('Content-Length', str(len(payload)))
  68. self.end_headers()
  69. self.wfile.write(payload)
  70. def _redirect(self):
  71. self.send_response(int(self.path[len('/redirect_'):]))
  72. self.send_header('Location', '/method')
  73. self.send_header('Content-Length', '0')
  74. self.end_headers()
  75. def _method(self, method, payload=None):
  76. self.send_response(200)
  77. self.send_header('Content-Length', str(len(payload or '')))
  78. self.send_header('Method', method)
  79. self.end_headers()
  80. if payload:
  81. self.wfile.write(payload)
  82. def _status(self, status):
  83. payload = '<html>{0} NOT FOUND</html>'.format(status).encode('utf-8')
  84. self.send_response(int(status))
  85. self.send_header('Content-Type', 'text/html; charset=utf-8')
  86. self.send_header('Content-Length', str(len(payload)))
  87. self.end_headers()
  88. self.wfile.write(payload)
  89. def _read_data(self):
  90. if 'Content-Length' in self.headers:
  91. return self.rfile.read(int(self.headers['Content-Length']))
  92. def _test_url(self, path, host='127.0.0.1', scheme='http', port=None):
  93. return '{0}://{1}:{2}/{3}'.format(
  94. scheme, host,
  95. port if port is not None
  96. else http_server_port(self.server), path)
  97. def do_POST(self):
  98. data = self._read_data()
  99. if self.path.startswith('/redirect_'):
  100. self._redirect()
  101. elif self.path.startswith('/method'):
  102. self._method('POST', data)
  103. elif self.path.startswith('/headers'):
  104. self._headers()
  105. else:
  106. self._status(404)
  107. def do_HEAD(self):
  108. if self.path.startswith('/redirect_'):
  109. self._redirect()
  110. elif self.path.startswith('/method'):
  111. self._method('HEAD')
  112. else:
  113. self._status(404)
  114. def do_PUT(self):
  115. data = self._read_data()
  116. if self.path.startswith('/redirect_'):
  117. self._redirect()
  118. elif self.path.startswith('/method'):
  119. self._method('PUT', data)
  120. else:
  121. self._status(404)
  122. def do_GET(self):
  123. def respond(payload=b'<html><video src="/vid.mp4" /></html>',
  124. payload_type='text/html; charset=utf-8',
  125. payload_encoding=None,
  126. resp_code=200):
  127. self.send_response(resp_code)
  128. self.send_header('Content-Type', payload_type)
  129. if payload_encoding:
  130. self.send_header('Content-Encoding', payload_encoding)
  131. self.send_header('Content-Length', str(len(payload))) # required for persistent connections
  132. self.end_headers()
  133. self.wfile.write(payload)
  134. def gzip_compress(p):
  135. buf = io.BytesIO()
  136. with contextlib.closing(gzip.GzipFile(fileobj=buf, mode='wb')) as f:
  137. f.write(p)
  138. return buf.getvalue()
  139. if self.path == '/video.html':
  140. respond()
  141. elif self.path == '/vid.mp4':
  142. respond(b'\x00\x00\x00\x00\x20\x66\x74[video]', 'video/mp4')
  143. elif self.path == '/302':
  144. if sys.version_info[0] == 3:
  145. # XXX: Python 3 http server does not allow non-ASCII header values
  146. self.send_response(404)
  147. self.end_headers()
  148. return
  149. new_url = self._test_url('中文.html')
  150. self.send_response(302)
  151. self.send_header(b'Location', new_url.encode('utf-8'))
  152. self.end_headers()
  153. elif self.path == '/%E4%B8%AD%E6%96%87.html':
  154. respond()
  155. elif self.path == '/%c7%9f':
  156. respond()
  157. elif self.path.startswith('/redirect_'):
  158. self._redirect()
  159. elif self.path.startswith('/method'):
  160. self._method('GET')
  161. elif self.path.startswith('/headers'):
  162. self._headers()
  163. elif self.path.startswith('/308-to-headers'):
  164. self.send_response(308)
  165. self.send_header('Location', '/headers')
  166. self.send_header('Content-Length', '0')
  167. self.end_headers()
  168. elif self.path == '/trailing_garbage':
  169. payload = b'<html><video src="/vid.mp4" /></html>'
  170. compressed = gzip_compress(payload) + b'trailing garbage'
  171. respond(compressed, payload_encoding='gzip')
  172. elif self.path == '/302-non-ascii-redirect':
  173. new_url = self._test_url('中文.html')
  174. # actually respond with permanent redirect
  175. self.send_response(301)
  176. self.send_header('Location', new_url)
  177. self.send_header('Content-Length', '0')
  178. self.end_headers()
  179. elif self.path == '/content-encoding':
  180. encodings = self.headers.get('ytdl-encoding', '')
  181. payload = b'<html><video src="/vid.mp4" /></html>'
  182. for encoding in filter(None, (e.strip() for e in encodings.split(','))):
  183. if encoding == 'br' and brotli:
  184. payload = brotli.compress(payload)
  185. elif encoding == 'gzip':
  186. payload = gzip_compress(payload)
  187. elif encoding == 'deflate':
  188. payload = zlib.compress(payload)
  189. elif encoding == 'unsupported':
  190. payload = b'raw'
  191. break
  192. else:
  193. self._status(415)
  194. return
  195. respond(payload, payload_encoding=encodings)
  196. else:
  197. self._status(404)
  198. def send_header(self, keyword, value):
  199. """
  200. Forcibly allow HTTP server to send non percent-encoded non-ASCII characters in headers.
  201. This is against what is defined in RFC 3986: but we need to test that we support this
  202. since some sites incorrectly do this.
  203. """
  204. if keyword.lower() == 'connection':
  205. return self.super('send_header', keyword, value)
  206. if not hasattr(self, '_headers_buffer'):
  207. self._headers_buffer = []
  208. self._headers_buffer.append('{0}: {1}\r\n'.format(keyword, value).encode('utf-8'))
  209. def end_headers(self):
  210. if hasattr(self, '_headers_buffer'):
  211. self.wfile.write(b''.join(self._headers_buffer))
  212. self._headers_buffer = []
  213. self.super('end_headers')
  214. class TestHTTP(unittest.TestCase):
  215. # when does it make sense to check the SSL certificate?
  216. _check_cert = (
  217. sys.version_info >= (3, 2)
  218. or (sys.version_info[0] == 2 and sys.version_info[1:] >= (7, 19)))
  219. def setUp(self):
  220. # HTTP server
  221. self.http_httpd = compat_http_server.HTTPServer(
  222. ('127.0.0.1', 0), HTTPTestRequestHandler)
  223. self.http_port = http_server_port(self.http_httpd)
  224. self.http_server_thread = threading.Thread(target=self.http_httpd.serve_forever)
  225. self.http_server_thread.daemon = True
  226. self.http_server_thread.start()
  227. try:
  228. from http.server import ThreadingHTTPServer
  229. except ImportError:
  230. try:
  231. from socketserver import ThreadingMixIn
  232. except ImportError:
  233. from SocketServer import ThreadingMixIn
  234. class ThreadingHTTPServer(ThreadingMixIn, compat_http_server.HTTPServer):
  235. pass
  236. # HTTPS server
  237. certfn = os.path.join(TEST_DIR, 'testcert.pem')
  238. self.https_httpd = ThreadingHTTPServer(
  239. ('127.0.0.1', 0), HTTPTestRequestHandler)
  240. try:
  241. sslctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
  242. sslctx.verify_mode = ssl.CERT_NONE
  243. sslctx.check_hostname = False
  244. sslctx.load_cert_chain(certfn, None)
  245. self.https_httpd.socket = sslctx.wrap_socket(
  246. self.https_httpd.socket, server_side=True)
  247. except AttributeError:
  248. self.https_httpd.socket = ssl.wrap_socket(
  249. self.https_httpd.socket, certfile=certfn, server_side=True)
  250. self.https_port = http_server_port(self.https_httpd)
  251. self.https_server_thread = threading.Thread(target=self.https_httpd.serve_forever)
  252. self.https_server_thread.daemon = True
  253. self.https_server_thread.start()
  254. def tearDown(self):
  255. def closer(svr):
  256. def _closer():
  257. svr.shutdown()
  258. svr.server_close()
  259. return _closer
  260. shutdown_thread = threading.Thread(target=closer(self.http_httpd))
  261. shutdown_thread.start()
  262. self.http_server_thread.join(2.0)
  263. shutdown_thread = threading.Thread(target=closer(self.https_httpd))
  264. shutdown_thread.start()
  265. self.https_server_thread.join(2.0)
  266. def _test_url(self, path, host='127.0.0.1', scheme='http', port=None):
  267. return '{0}://{1}:{2}/{3}'.format(
  268. scheme, host,
  269. port if port is not None
  270. else self.https_port if scheme == 'https'
  271. else self.http_port, path)
  272. @unittest.skipUnless(_check_cert, 'No support for certificate check in SSL')
  273. def test_nocheckcertificate(self):
  274. with FakeYDL({'logger': FakeLogger()}) as ydl:
  275. with self.assertRaises(compat_urllib_error.URLError):
  276. ydl.urlopen(sanitized_Request(self._test_url('headers', scheme='https')))
  277. with FakeYDL({'logger': FakeLogger(), 'nocheckcertificate': True}) as ydl:
  278. r = ydl.urlopen(sanitized_Request(self._test_url('headers', scheme='https')))
  279. self.assertEqual(r.getcode(), 200)
  280. r.close()
  281. def test_percent_encode(self):
  282. with FakeYDL() as ydl:
  283. # Unicode characters should be encoded with uppercase percent-encoding
  284. res = ydl.urlopen(sanitized_Request(self._test_url('中文.html')))
  285. self.assertEqual(res.getcode(), 200)
  286. res.close()
  287. # don't normalize existing percent encodings
  288. res = ydl.urlopen(sanitized_Request(self._test_url('%c7%9f')))
  289. self.assertEqual(res.getcode(), 200)
  290. res.close()
  291. def test_unicode_path_redirection(self):
  292. with FakeYDL() as ydl:
  293. r = ydl.urlopen(sanitized_Request(self._test_url('302-non-ascii-redirect')))
  294. self.assertEqual(r.url, self._test_url('%E4%B8%AD%E6%96%87.html'))
  295. r.close()
  296. def test_redirect(self):
  297. with FakeYDL() as ydl:
  298. def do_req(redirect_status, method, check_no_content=False):
  299. data = b'testdata' if method in ('POST', 'PUT') else None
  300. res = ydl.urlopen(sanitized_Request(
  301. self._test_url('redirect_{0}'.format(redirect_status)),
  302. method=method, data=data))
  303. if check_no_content:
  304. self.assertNotIn('Content-Type', res.headers)
  305. return res.read().decode('utf-8'), res.headers.get('method', '')
  306. # A 303 must either use GET or HEAD for subsequent request
  307. self.assertEqual(do_req(303, 'POST'), ('', 'GET'))
  308. self.assertEqual(do_req(303, 'HEAD'), ('', 'HEAD'))
  309. self.assertEqual(do_req(303, 'PUT'), ('', 'GET'))
  310. # 301 and 302 turn POST only into a GET, with no Content-Type
  311. self.assertEqual(do_req(301, 'POST', True), ('', 'GET'))
  312. self.assertEqual(do_req(301, 'HEAD'), ('', 'HEAD'))
  313. self.assertEqual(do_req(302, 'POST', True), ('', 'GET'))
  314. self.assertEqual(do_req(302, 'HEAD'), ('', 'HEAD'))
  315. self.assertEqual(do_req(301, 'PUT'), ('testdata', 'PUT'))
  316. self.assertEqual(do_req(302, 'PUT'), ('testdata', 'PUT'))
  317. # 307 and 308 should not change method
  318. for m in ('POST', 'PUT'):
  319. self.assertEqual(do_req(307, m), ('testdata', m))
  320. self.assertEqual(do_req(308, m), ('testdata', m))
  321. self.assertEqual(do_req(307, 'HEAD'), ('', 'HEAD'))
  322. self.assertEqual(do_req(308, 'HEAD'), ('', 'HEAD'))
  323. # These should not redirect and instead raise an HTTPError
  324. for code in (300, 304, 305, 306):
  325. with self.assertRaises(compat_urllib_HTTPError):
  326. do_req(code, 'GET')
  327. # Jython 2.7.1 times out for some reason
  328. @expectedFailureIf(sys.platform.startswith('java') and sys.version_info < (2, 7, 2))
  329. def test_content_type(self):
  330. # https://github.com/yt-dlp/yt-dlp/commit/379a4f161d4ad3e40932dcf5aca6e6fb9715ab28
  331. with FakeYDL({'nocheckcertificate': True}) as ydl:
  332. # method should be auto-detected as POST
  333. r = sanitized_Request(self._test_url('headers', scheme='https'), data=urlencode_postdata({'test': 'test'}))
  334. headers = ydl.urlopen(r).read().decode('utf-8')
  335. self.assertIn('Content-Type: application/x-www-form-urlencoded', headers)
  336. # test http
  337. r = sanitized_Request(self._test_url('headers'), data=urlencode_postdata({'test': 'test'}))
  338. headers = ydl.urlopen(r).read().decode('utf-8')
  339. self.assertIn('Content-Type: application/x-www-form-urlencoded', headers)
  340. def test_update_req(self):
  341. req = sanitized_Request('http://example.com')
  342. assert req.data is None
  343. assert req.get_method() == 'GET'
  344. assert not req.has_header('Content-Type')
  345. # Test that zero-byte payloads will be sent
  346. req = update_Request(req, data=b'')
  347. assert req.data == b''
  348. assert req.get_method() == 'POST'
  349. # yt-dl expects data to be encoded and Content-Type to be added by sender
  350. # assert req.get_header('Content-Type') == 'application/x-www-form-urlencoded'
  351. def test_cookiejar(self):
  352. with FakeYDL() as ydl:
  353. ydl.cookiejar.set_cookie(compat_http_cookiejar_Cookie(
  354. 0, 'test', 'ytdl', None, False, '127.0.0.1', True,
  355. False, '/headers', True, False, None, False, None, None, {}))
  356. data = ydl.urlopen(sanitized_Request(
  357. self._test_url('headers'))).read().decode('utf-8')
  358. self.assertIn('Cookie: test=ytdl', data)
  359. def test_passed_cookie_header(self):
  360. # We should accept a Cookie header being passed as in normal headers and handle it appropriately.
  361. with FakeYDL() as ydl:
  362. # Specified Cookie header should be used
  363. res = ydl.urlopen(sanitized_Request(
  364. self._test_url('headers'), headers={'Cookie': 'test=test'})).read().decode('utf-8')
  365. self.assertIn('Cookie: test=test', res)
  366. # Specified Cookie header should be removed on any redirect
  367. res = ydl.urlopen(sanitized_Request(
  368. self._test_url('308-to-headers'), headers={'Cookie': 'test=test'})).read().decode('utf-8')
  369. self.assertNotIn('Cookie: test=test', res)
  370. # Specified Cookie header should override global cookiejar for that request
  371. ydl.cookiejar.set_cookie(compat_http_cookiejar_Cookie(
  372. 0, 'test', 'ytdlp', None, False, '127.0.0.1', True,
  373. False, '/headers', True, False, None, False, None, None, {}))
  374. data = ydl.urlopen(sanitized_Request(
  375. self._test_url('headers'), headers={'Cookie': 'test=test'})).read().decode('utf-8')
  376. self.assertNotIn('Cookie: test=ytdlp', data)
  377. self.assertIn('Cookie: test=test', data)
  378. def test_no_compression_compat_header(self):
  379. with FakeYDL() as ydl:
  380. data = ydl.urlopen(
  381. sanitized_Request(
  382. self._test_url('headers'),
  383. headers={'Youtubedl-no-compression': True})).read()
  384. self.assertIn(b'Accept-Encoding: identity', data)
  385. self.assertNotIn(b'youtubedl-no-compression', data.lower())
  386. def test_gzip_trailing_garbage(self):
  387. # https://github.com/ytdl-org/youtube-dl/commit/aa3e950764337ef9800c936f4de89b31c00dfcf5
  388. # https://github.com/ytdl-org/youtube-dl/commit/6f2ec15cee79d35dba065677cad9da7491ec6e6f
  389. with FakeYDL() as ydl:
  390. data = ydl.urlopen(sanitized_Request(self._test_url('trailing_garbage'))).read().decode('utf-8')
  391. self.assertEqual(data, '<html><video src="/vid.mp4" /></html>')
  392. def __test_compression(self, encoding):
  393. with FakeYDL() as ydl:
  394. res = ydl.urlopen(
  395. sanitized_Request(
  396. self._test_url('content-encoding'),
  397. headers={'ytdl-encoding': encoding}))
  398. self.assertEqual(res.headers.get('Content-Encoding'), encoding)
  399. self.assertEqual(res.read(), b'<html><video src="/vid.mp4" /></html>')
  400. @unittest.skipUnless(brotli, 'brotli support is not installed')
  401. @unittest.expectedFailure
  402. def test_brotli(self):
  403. self.__test_compression('br')
  404. @unittest.expectedFailure
  405. def test_deflate(self):
  406. self.__test_compression('deflate')
  407. @unittest.expectedFailure
  408. def test_gzip(self):
  409. self.__test_compression('gzip')
  410. @unittest.expectedFailure # not yet implemented
  411. def test_multiple_encodings(self):
  412. # https://www.rfc-editor.org/rfc/rfc9110.html#section-8.4
  413. with FakeYDL() as ydl:
  414. for pair in ('gzip,deflate', 'deflate, gzip', 'gzip, gzip', 'deflate, deflate'):
  415. res = ydl.urlopen(
  416. sanitized_Request(
  417. self._test_url('content-encoding'),
  418. headers={'ytdl-encoding': pair}))
  419. self.assertEqual(res.headers.get('Content-Encoding'), pair)
  420. self.assertEqual(res.read(), b'<html><video src="/vid.mp4" /></html>')
  421. def test_unsupported_encoding(self):
  422. # it should return the raw content
  423. with FakeYDL() as ydl:
  424. res = ydl.urlopen(
  425. sanitized_Request(
  426. self._test_url('content-encoding'),
  427. headers={'ytdl-encoding': 'unsupported'}))
  428. self.assertEqual(res.headers.get('Content-Encoding'), 'unsupported')
  429. self.assertEqual(res.read(), b'raw')
  430. def _build_proxy_handler(name):
  431. class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler):
  432. proxy_name = name
  433. def log_message(self, format, *args):
  434. pass
  435. def do_GET(self):
  436. self.send_response(200)
  437. self.send_header('Content-Type', 'text/plain; charset=utf-8')
  438. self.end_headers()
  439. self.wfile.write('{0}: {1}'.format(self.proxy_name, self.path).encode('utf-8'))
  440. return HTTPTestRequestHandler
  441. class TestProxy(unittest.TestCase):
  442. def setUp(self):
  443. self.proxy = compat_http_server.HTTPServer(
  444. ('127.0.0.1', 0), _build_proxy_handler('normal'))
  445. self.port = http_server_port(self.proxy)
  446. self.proxy_thread = threading.Thread(target=self.proxy.serve_forever)
  447. self.proxy_thread.daemon = True
  448. self.proxy_thread.start()
  449. self.geo_proxy = compat_http_server.HTTPServer(
  450. ('127.0.0.1', 0), _build_proxy_handler('geo'))
  451. self.geo_port = http_server_port(self.geo_proxy)
  452. self.geo_proxy_thread = threading.Thread(target=self.geo_proxy.serve_forever)
  453. self.geo_proxy_thread.daemon = True
  454. self.geo_proxy_thread.start()
  455. def tearDown(self):
  456. def closer(svr):
  457. def _closer():
  458. svr.shutdown()
  459. svr.server_close()
  460. return _closer
  461. shutdown_thread = threading.Thread(target=closer(self.proxy))
  462. shutdown_thread.start()
  463. self.proxy_thread.join(2.0)
  464. shutdown_thread = threading.Thread(target=closer(self.geo_proxy))
  465. shutdown_thread.start()
  466. self.geo_proxy_thread.join(2.0)
  467. def _test_proxy(self, host='127.0.0.1', port=None):
  468. return '{0}:{1}'.format(
  469. host, port if port is not None else self.port)
  470. def test_proxy(self):
  471. geo_proxy = self._test_proxy(port=self.geo_port)
  472. ydl = YoutubeDL({
  473. 'proxy': self._test_proxy(),
  474. 'geo_verification_proxy': geo_proxy,
  475. })
  476. url = 'http://foo.com/bar'
  477. response = ydl.urlopen(url).read().decode('utf-8')
  478. self.assertEqual(response, 'normal: {0}'.format(url))
  479. req = compat_urllib_request.Request(url)
  480. req.add_header('Ytdl-request-proxy', geo_proxy)
  481. response = ydl.urlopen(req).read().decode('utf-8')
  482. self.assertEqual(response, 'geo: {0}'.format(url))
  483. def test_proxy_with_idn(self):
  484. ydl = YoutubeDL({
  485. 'proxy': self._test_proxy(),
  486. })
  487. url = 'http://中文.tw/'
  488. response = ydl.urlopen(url).read().decode('utf-8')
  489. # b'xn--fiq228c' is '中文'.encode('idna')
  490. self.assertEqual(response, 'normal: http://xn--fiq228c.tw/')
  491. class TestFileURL(unittest.TestCase):
  492. # See https://github.com/ytdl-org/youtube-dl/issues/8227
  493. def test_file_urls(self):
  494. tf = tempfile.NamedTemporaryFile(delete=False)
  495. tf.write(b'foobar')
  496. tf.close()
  497. url = compat_urllib_parse.urljoin('file://', pathname2url(tf.name))
  498. with FakeYDL() as ydl:
  499. self.assertRaisesRegexp(
  500. compat_urllib_error.URLError, 'file:// scheme is explicitly disabled in youtube-dl for security reasons', ydl.urlopen, url)
  501. # not yet implemented
  502. """
  503. with FakeYDL({'enable_file_urls': True}) as ydl:
  504. res = ydl.urlopen(url)
  505. self.assertEqual(res.read(), b'foobar')
  506. res.close()
  507. """
  508. os.unlink(tf.name)
  509. if __name__ == '__main__':
  510. unittest.main()