diff options
author | Andrey Petrov <andrey.petrov@shazow.net> | 2013-10-04 17:49:51 +0200 |
---|---|---|
committer | Andrey Petrov <andrey.petrov@shazow.net> | 2013-10-16 11:50:48 +0200 |
commit | 3b6d57e316bc932b6b1049b279ef849be32a8334 (patch) | |
tree | 0563e38526bff40919edf068893c2d743357152e | |
parent | f07dac4e6c404506c17cf58b50ea086104abc7b8 (diff) | |
download | urllib3-3b6d57e316bc932b6b1049b279ef849be32a8334.tar.gz |
Factor out HTTP(S)Connection -> ConnectionCls, and cleanup.
Also removed the need to None-ify global `ssl` module in tests.
-rw-r--r-- | test/with_dummyserver/test_https.py | 52 | ||||
-rw-r--r-- | urllib3/connectionpool.py | 43 |
2 files changed, 41 insertions, 54 deletions
diff --git a/test/with_dummyserver/test_https.py b/test/with_dummyserver/test_https.py index 8e07f2f1..b99cb0d0 100644 --- a/test/with_dummyserver/test_https.py +++ b/test/with_dummyserver/test_https.py @@ -72,20 +72,14 @@ class TestHTTPS(HTTPSDummyServerTestCase): self.assertTrue("doesn't match" in str(e)) def test_no_ssl(self): - import urllib3.connectionpool - OriginalHTTPSConnection = urllib3.connectionpool.HTTPSConnection - OriginalSSL = urllib3.connectionpool.ssl - - urllib3.connectionpool.HTTPSConnection = None - urllib3.connectionpool.ssl = None + OriginalConnectionCls = self._pool.ConnectionCls + self._pool.ConnectionCls = None self.assertRaises(SSLError, self._pool._new_conn) - self.assertRaises(SSLError, self._pool.request, 'GET', '/') # Undo - urllib3.HTTPSConnection = OriginalHTTPSConnection - urllib3.connectionpool.ssl = OriginalSSL + self._pool.ConnectionCls = OriginalConnectionCls def test_cert_reqs_as_constant(self): https_pool = HTTPSConnectionPool(self.host, self.port, @@ -223,44 +217,35 @@ class TestHTTPS(HTTPSDummyServerTestCase): def test_enhanced_timeout(self): - import urllib3.connectionpool - OriginalHTTPSConnection = urllib3.connectionpool.HTTPSConnection - OriginalSSL = urllib3.connectionpool.ssl - - urllib3.connectionpool.ssl = None + def new_pool(timeout, cert_reqs='CERT_REQUIRED'): + https_pool = HTTPSConnectionPool(TARPIT_HOST, self.port, + timeout=timeout, + cert_reqs=cert_reqs) + return https_pool - timeout = Timeout(connect=0.001) - https_pool = HTTPSConnectionPool(TARPIT_HOST, self.port, - timeout=timeout, - cert_reqs='CERT_REQUIRED') + https_pool = new_pool(Timeout(connect=0.001)) conn = https_pool._new_conn() - self.assertEqual(conn.__class__, HTTPSConnection) self.assertRaises(ConnectTimeoutError, https_pool.request, 'GET', '/') self.assertRaises(ConnectTimeoutError, https_pool._make_request, conn, 'GET', '/') - timeout = Timeout(connect=5) - https_pool = HTTPSConnectionPool(TARPIT_HOST, self.port, - timeout=timeout, - cert_reqs='CERT_REQUIRED') + https_pool = new_pool(Timeout(connect=5)) self.assertRaises(ConnectTimeoutError, https_pool.request, 'GET', '/', timeout=Timeout(connect=0.001)) - timeout = Timeout(total=None) - https_pool = HTTPSConnectionPool(TARPIT_HOST, self.port, - timeout=timeout, - cert_reqs='CERT_REQUIRED') + t = Timeout(total=None) + https_pool = new_pool(t) conn = https_pool._new_conn() self.assertRaises(ConnectTimeoutError, https_pool.request, 'GET', '/', timeout=Timeout(total=None, connect=0.001)) - https_pool = HTTPSConnectionPool(self.host, self.port, - timeout=timeout, - cert_reqs='CERT_NONE') + # FIXME(kevinburke): What is this testing? It's currently broken. + """ + https_pool = new_pool(t, cert_reqs='CERT_NONE') conn = https_pool._new_conn() try: conn.set_tunnel(self.host, self.port) - except AttributeError: # python 2.6 + except AttributeError: # Python 2.6 conn._set_tunnel(self.host, self.port) conn._tunnel = mock.Mock() try: @@ -269,10 +254,7 @@ class TestHTTPS(HTTPSDummyServerTestCase): # wrap_socket unavailable when you mock out ssl pass conn._tunnel.assert_called_once_with() - - # Undo - urllib3.HTTPSConnection = OriginalHTTPSConnection - urllib3.connectionpool.ssl = OriginalSSL + """ def test_enhanced_ssl_connection(self): conn = VerifiedHTTPSConnection(self.host, self.port) diff --git a/urllib3/connectionpool.py b/urllib3/connectionpool.py index 1ad20a24..0c03f576 100644 --- a/urllib3/connectionpool.py +++ b/urllib3/connectionpool.py @@ -24,14 +24,17 @@ except ImportError: import Queue as _ # Platform-specific: Windows +class DummyConnection(object): + "Used to detect a failed ConnectionCls import." + pass + try: # Compiled with SSL? - HTTPSConnection = object + ssl = None + HTTPSConnection = DummyConnection class BaseSSLError(Exception): pass - ssl = None - try: # Python 3 from http.client import HTTPSConnection except ImportError: @@ -218,6 +221,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods): """ scheme = 'http' + ConnectionCls = HTTPConnection def __init__(self, host, port=None, strict=False, timeout=Timeout.DEFAULT_TIMEOUT, maxsize=1, block=False, @@ -259,9 +263,9 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods): if not six.PY3: # Python 2 extra_params['strict'] = self.strict - return HTTPConnection(host=self.host, port=self.port, - timeout=self.timeout.connect_timeout, - **extra_params) + return self.ConnectionCls(host=self.host, port=self.port, + timeout=self.timeout.connect_timeout, + **extra_params) def _get_conn(self, timeout=None): @@ -362,7 +366,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods): timeout_obj.start_connect() conn.timeout = timeout_obj.connect_timeout # conn.request() calls httplib.*.request, not the method in - # request.py. It also calls makefile (recv) on the socket + # urllib3.request. It also calls makefile (recv) on the socket. conn.request(method, url, **httplib_request_kw) except SocketTimeout: raise ConnectTimeoutError( @@ -371,7 +375,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods): # Reset the timeout for the recv() on the socket read_timeout = timeout_obj.read_timeout - log.debug("Setting read timeout to %s" % read_timeout) + # App Engine doesn't have a sock attr if hasattr(conn, 'sock') and \ read_timeout is not None and \ @@ -639,6 +643,9 @@ class HTTPSConnectionPool(HTTPConnectionPool): """ scheme = 'https' + ConnectionCls = HTTPSConnection + if ssl: + ConnectionCls = VerifiedHTTPSConnection def __init__(self, host, port=None, strict=False, timeout=None, maxsize=1, @@ -694,26 +701,24 @@ class HTTPSConnectionPool(HTTPConnectionPool): log.info("Starting new HTTPS connection (%d): %s" % (self.num_connections, self.host)) + if not self.ConnectionCls or self.ConnectionCls is DummyConnection: + # Platform-specific: Python without ssl + raise SSLError("Can't connect to HTTPS URL because the SSL " + "module is not available.") + actual_host = self.host actual_port = self.port if self.proxy is not None: actual_host = self.proxy.host actual_port = self.proxy.port - if not ssl: # Platform-specific: Python compiled without +ssl - if not HTTPSConnection or HTTPSConnection is object: - raise SSLError("Can't connect to HTTPS URL because the SSL " - "module is not available.") - connection_class = HTTPSConnection - else: - connection_class = VerifiedHTTPSConnection - extra_params = {} if not six.PY3: # Python 2 extra_params['strict'] = self.strict - conn = connection_class(host=actual_host, port=actual_port, - timeout=self.timeout.connect_timeout, - **extra_params) + + conn = self.ConnectionCls(host=actual_host, port=actual_port, + timeout=self.timeout.connect_timeout, + **extra_params) return self._prepare_conn(conn) |