]>
Raphaël G. Git Repositories - youtubedl/blob - test/test_http.py
7a7a3510ffb46e2791153dff5e4157bb21433056
   3 from __future__ 
import unicode_literals
 
   5 # Allow direct execution 
   9 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))) 
  11 from youtube_dl 
import YoutubeDL
 
  12 from youtube_dl
.compat 
import compat_http_server
, compat_urllib_request
 
  16 TEST_DIR 
= os
.path
.dirname(os
.path
.abspath(__file__
)) 
  19 def http_server_port(httpd
): 
  20     if os
.name 
== 'java' and isinstance(httpd
.socket
, ssl
.SSLSocket
): 
  21         # In Jython SSLSocket is not a subclass of socket.socket 
  22         sock 
= httpd
.socket
.sock
 
  25     return sock
.getsockname()[1] 
  28 class HTTPTestRequestHandler(compat_http_server
.BaseHTTPRequestHandler
): 
  29     def log_message(self
, format
, *args
): 
  33         if self
.path 
== '/video.html': 
  34             self
.send_response(200) 
  35             self
.send_header('Content-Type', 'text/html; charset=utf-8') 
  37             self
.wfile
.write(b
'<html><video src="/vid.mp4" /></html>') 
  38         elif self
.path 
== '/vid.mp4': 
  39             self
.send_response(200) 
  40             self
.send_header('Content-Type', 'video/mp4') 
  42             self
.wfile
.write(b
'\x00\x00\x00\x00\x20\x66\x74[video]') 
  43         elif self
.path 
== '/302': 
  44             if sys
.version_info
[0] == 3: 
  45                 # XXX: Python 3 http server does not allow non-ASCII header values 
  46                 self
.send_response(404) 
  50             new_url 
= 'http://localhost:%d/中文.html' % http_server_port(self
.server
) 
  51             self
.send_response(302) 
  52             self
.send_header(b
'Location', new_url
.encode('utf-8')) 
  54         elif self
.path 
== '/%E4%B8%AD%E6%96%87.html': 
  55             self
.send_response(200) 
  56             self
.send_header('Content-Type', 'text/html; charset=utf-8') 
  58             self
.wfile
.write(b
'<html><video src="/vid.mp4" /></html>') 
  63 class FakeLogger(object): 
  67     def warning(self
, msg
): 
  74 class TestHTTP(unittest
.TestCase
): 
  76         self
.httpd 
= compat_http_server
.HTTPServer( 
  77             ('localhost', 0), HTTPTestRequestHandler
) 
  78         self
.port 
= http_server_port(self
.httpd
) 
  79         self
.server_thread 
= threading
.Thread(target
=self
.httpd
.serve_forever
) 
  80         self
.server_thread
.daemon 
= True 
  81         self
.server_thread
.start() 
  83     def test_unicode_path_redirection(self
): 
  84         # XXX: Python 3 http server does not allow non-ASCII header values 
  85         if sys
.version_info
[0] == 3: 
  88         ydl 
= YoutubeDL({'logger': FakeLogger()}) 
  89         r 
= ydl
.extract_info('http://localhost:%d/302' % self
.port
) 
  90         self
.assertEqual(r
['entries'][0]['url'], 'http://localhost:%d/vid.mp4' % self
.port
) 
  93 class TestHTTPS(unittest
.TestCase
): 
  95         certfn 
= os
.path
.join(TEST_DIR
, 'testcert.pem') 
  96         self
.httpd 
= compat_http_server
.HTTPServer( 
  97             ('localhost', 0), HTTPTestRequestHandler
) 
  98         self
.httpd
.socket 
= ssl
.wrap_socket( 
  99             self
.httpd
.socket
, certfile
=certfn
, server_side
=True) 
 100         self
.port 
= http_server_port(self
.httpd
) 
 101         self
.server_thread 
= threading
.Thread(target
=self
.httpd
.serve_forever
) 
 102         self
.server_thread
.daemon 
= True 
 103         self
.server_thread
.start() 
 105     def test_nocheckcertificate(self
): 
 106         if sys
.version_info 
>= (2, 7, 9):  # No certificate checking anyways 
 107             ydl 
= YoutubeDL({'logger': FakeLogger()}) 
 110                 ydl
.extract_info
, 'https://localhost:%d/video.html' % self
.port
) 
 112         ydl 
= YoutubeDL({'logger': FakeLogger(), 'nocheckcertificate': True}) 
 113         r 
= ydl
.extract_info('https://localhost:%d/video.html' % self
.port
) 
 114         self
.assertEqual(r
['entries'][0]['url'], 'https://localhost:%d/vid.mp4' % self
.port
) 
 117 def _build_proxy_handler(name
): 
 118     class HTTPTestRequestHandler(compat_http_server
.BaseHTTPRequestHandler
): 
 121         def log_message(self
, format
, *args
): 
 125             self
.send_response(200) 
 126             self
.send_header('Content-Type', 'text/plain; charset=utf-8') 
 128             self
.wfile
.write('{self.proxy_name}: {self.path}'.format(self
=self
).encode('utf-8')) 
 129     return HTTPTestRequestHandler
 
 132 class TestProxy(unittest
.TestCase
): 
 134         self
.proxy 
= compat_http_server
.HTTPServer( 
 135             ('localhost', 0), _build_proxy_handler('normal')) 
 136         self
.port 
= http_server_port(self
.proxy
) 
 137         self
.proxy_thread 
= threading
.Thread(target
=self
.proxy
.serve_forever
) 
 138         self
.proxy_thread
.daemon 
= True 
 139         self
.proxy_thread
.start() 
 141         self
.geo_proxy 
= compat_http_server
.HTTPServer( 
 142             ('localhost', 0), _build_proxy_handler('geo')) 
 143         self
.geo_port 
= http_server_port(self
.geo_proxy
) 
 144         self
.geo_proxy_thread 
= threading
.Thread(target
=self
.geo_proxy
.serve_forever
) 
 145         self
.geo_proxy_thread
.daemon 
= True 
 146         self
.geo_proxy_thread
.start() 
 148     def test_proxy(self
): 
 149         geo_proxy 
= 'localhost:{0}'.format(self
.geo_port
) 
 151             'proxy': 'localhost:{0}'.format(self
.port
), 
 152             'geo_verification_proxy': geo_proxy
, 
 154         url 
= 'http://foo.com/bar' 
 155         response 
= ydl
.urlopen(url
).read().decode('utf-8') 
 156         self
.assertEqual(response
, 'normal: {0}'.format(url
)) 
 158         req 
= compat_urllib_request
.Request(url
) 
 159         req
.add_header('Ytdl-request-proxy', geo_proxy
) 
 160         response 
= ydl
.urlopen(req
).read().decode('utf-8') 
 161         self
.assertEqual(response
, 'geo: {0}'.format(url
)) 
 163     def test_proxy_with_idn(self
): 
 165             'proxy': 'localhost:{0}'.format(self
.port
), 
 167         url 
= 'http://中文.tw/' 
 168         response 
= ydl
.urlopen(url
).read().decode('utf-8') 
 169         # b'xn--fiq228c' is '中文'.encode('idna') 
 170         self
.assertEqual(response
, 'normal: http://xn--fiq228c.tw/') 
 173 if __name__ 
== '__main__':